首页 新闻 会员 周边 捐助

利用反射机制不成功,希望专家能帮助!

0
悬赏园豆:10 [已解决问题] 解决于 2009-09-13 00:34

为了更快朋友们帮忙解决问题,我详细给出了以下的代码:

//IMedia接口:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace MediaLibrary
{
/// <summary>
/// Summary description for IMedia
/// </summary>
public interface IMedia
{
void Play();
}
}

//AudioMedia 抽象类:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace MediaLibrary {
/// <summary>
/// Summary description for AudioMedia
/// </summary>
public abstract class AudioMedia : IMedia//AudioMedia只是一个抽象化概念,并没有实际的使用情况
{
public abstract void Play();
}
}

//VideoMedia 抽象类:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace MediaLibrary {
/// <summary>
/// Summary description for VideoMedia
/// </summary>
public abstract class VideoMedia : IMedia
{
public abstract void Play();
}

}
//MP3 类:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace MediaLibrary {
/// <summary>
/// Summary description for MP3
/// </summary>
public class MP3 : AudioMedia
{
public override void Play() //重写Play()
{

}
}

}
//WAV类:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace MediaLibrary {
/// <summary>
/// Summary description for WAV
/// </summary>
public class WAV : AudioMedia //重写Play()
{
public override void Play()
{

}
}

}
//MP3Factory 工厂类
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace MediaLibrary {
/// <summary>
/// Summary description for MP3Factory
/// </summary>
public class MP3Factory : IMediaFactory
{
public IMedia CreateMedia()
{
return new MP3();
}
}

}
//WAVFactory 工厂类
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace MediaLibrary {
/// <summary>
/// Summary description for WAVFactory
/// </summary>
public class WAVFactory : IMediaFactory
{
public IMedia CreateMedia()
{
return new WAV();
}
}

}
//IMediaFactory接口
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace MediaLibrary {
/// <summary>
/// Summary description for IMediaFactory
/// </summary>
public interface IMediaFactory
{
IMedia CreateMedia();
}

}
//配置文件
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="mp3" value="MediaLibrary.MP3Factory"/>
<add key="wav" value="MediaLibrary.WAVFactory"/>
</appSettings>
<connectionStrings/>
</configuration>

//界面代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MediaLibrary;
using System.Reflection;
public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
InitMediaType();
}
private void InitMediaType()
{
DropDownList1.Items.Clear();
foreach (string key in ConfigurationSettings.AppSettings.AllKeys)
{
DropDownList1.Items.Add(key);
}
DropDownList1.SelectedIndex
= 0;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string mediaType = DropDownList1.SelectedItem.ToString().ToLower();
string factoryDllName = ConfigurationSettings.AppSettings[mediaType].ToString();
IMediaFactory factory
= (IMediaFactory)Activator.CreateInstance("MediaLibrary", factoryDllName).Unwrap();//此句提示无法载入文档或组件,找不到MediaLibrary或其相依性的其中之一,系统找不到指定的文档
if (factory != null)
{
IMedia media
= factory.CreateMedia();
media.Play();
}

}
}

我这问题卡在这里了,到底什么原因呢?
问题补充: factoryDllName是可以取得到值"MediaLibrary.MP3Factory",但是factory为null
aganqin的主页 aganqin | 初学一级 | 园豆:126
提问于:2009-09-07 10:13
< >
分享
最佳答案
0

在调用

Activator.CreateInstance
之前似乎应该先加载你的动态库吧?或者把你的动态库添加到项目的引用里面也可以。
CreateInstance 的第二个参数应该是
typeName 而不是 dll name






收获园豆:7
eaglet | 专家六级 |园豆:17139 | 2009-09-07 12:27
其他回答(1)
0

楼主看看

Activator.CreateInstance的重载吧
收获园豆:3
I,Robot | 园豆:9783 (大侠五级) | 2009-09-07 17:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册