OpenFileDialog dlg = new OpenFileDialog();
if(dlg.ShowDialog() == DialogResult.Cancel)
{
return;
}

Assembly ass = Assembly.LoadFile(dlg.FileName);
Type temp = null;
foreach(Type t in ass.GetExportedTypes())
{
if(t.IsClass && typeof(MyClass).IsAssignableFrom(t))
   temp = t;
}
IMyClass clas = (IMyClass) Activator.CreateInstance(temp);是模仿《框架设计》这本书中的内容来写的,结果失败了
那个程序集的源码:
namespace ClassLIbrary1
{
/// <summary>
/// Description of MyClass.
/// </summary>
///
public interface IMyClass
{
string Value
{
get;
set;
}
}

public class MyClass : IMyClass
{
public string Value
{
get
{
return _Value; 
}
set
{
_Value = value;
}
}
private string _Value;
}
}谢谢

解决方案 »

  1.   


          private static readonly string path = "CourseWeb.DAL";      public static IConfig CreateConfig()
            {
                string className = path + ".Config";
                return (IConfig)Assembly.Load(path).CreateInstance(className);
            }
      

  2.   

    试下这个
    IMyClass clas = (IMyClass) ass.CreateInstance(temp.FullName); 
      

  3.   


    // 动态调用Dll,实例化的方法 
    private void Page_Loaded(object sender, RoutedEventArgs e) 
            { 
                string path = System.IO.Path.GetDirectoryName(GetType().Assembly.Location); 
                try 
                { 
                    Assembly libDLL = Assembly.LoadFrom(System.IO.Path.Combine(path, "DllTest.dll")); 
                    if (libDLL != null) 
                    { 
                        Type dllType = libDLL.GetTypes()[0]; 
                        MethodInfo methodLib = dllType.GetMethod("GetResult"); 
                        object obj = Activator.CreateInstance(dllType); 
                        object[] args = {}; 
                        int result = (int) methodLib.Invoke(obj, args); 
                        MessageBox.Show(" 加载成功!\r\n计算结果:" + result.ToString()); 
                    } 
                    else 
                    { 
                        MessageBox.Show(" 加载失败!"); 
                    } 
                } 
                catch(Exception ex) 
                { 
                    MessageBox.Show(ex.Message); 
                } 
            } 
     
    // 动态调用Dll,静态方法 
    public int GetResult() 
            { 
                string path = System.IO.Path.GetDirectoryName(GetType().Assembly.Location); 
                try 
                { 
                    Assembly libDLL = Assembly.LoadFrom(System.IO.Path.Combine(path, "NumProvider.dll")); 
                    if (libDLL != null) 
                    { 
                        MethodInfo methodLib = libDLL.GetTypes()[0].GetMethod("AddTwoNum"); 
                        object[] args = { 1, 1 };                     int result = (int)methodLib.Invoke(null, args); 
                        return result; 
                    } 
                } 
                catch (Exception ex) 
                { 
                    Debug.WriteLine(ex.Message); 
                } 
                return 0; 
            } 
      

  4.   

    OpenFileDialog dlg = new OpenFileDialog(); 
    if(dlg.ShowDialog() == DialogResult.Cancel) 

    return; 
    } Assembly ass = Assembly.LoadFile(dlg.FileName); 
    Type temp = null; 
    foreach(Type t in ass.GetExportedTypes()) 

    if(t.IsClass && typeof(MyClass).IsAssignableFrom(t)) 
      temp = t; 

    IMyClass clas = (IMyClass) Activator.CreateInstance(temp); 红色部分改成:if (t.IsClass&&!t.IsAbstract)  ->保证t能被实例化
                 Object my = Activator.CreateInstance(temp);  ->my是实例化的对象
    即my是MyClass的对象,你去对my操作就行了