为了更快朋友们帮忙解决问题,我详细给出了以下的代码:
//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();
}
}
}
我这问题卡在这里了,到底什么原因呢?
在调用
Activator.CreateInstance
之前似乎应该先加载你的动态库吧?或者把你的动态库添加到项目的引用里面也可以。
CreateInstance 的第二个参数应该是 typeName 而不是 dll name
楼主看看
Activator.CreateInstance的重载吧