如何才能从系统中动态获得支持某一个特定的intface的类并实例化它?

解决方案 »

  1.   

    1:dll文件搜索
    2:反射实例化/// <summary>
    /// TypeLoader 的摘要说明。
    /// </summary>
    public class TypeLoader
    {
    public TypeLoader()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }//t是一个接口类型,
    public static object CreateInstance( Type t )
    {
    try
    {
    return System.Activator.CreateInstance( t ) ; 
    }
    catch( Exception ex )
    {
    Log.WriteLog( ex ) ;
    }
    return null ;
    } public static ArrayList LoadObjects( Type t )
    {
    ArrayList types = LoadTypes( t ) ; ArrayList list = new ArrayList() ; foreach( Type tt in types )
    {
    object o = CreateInstance( tt ) ;
    if( o != null )
    list.Add( o ) ;
    } return list;
    } public static  ArrayList LoadTypes( Type t )
    {
    ArrayList list = new ArrayList(); DirectoryInfo di = new DirectoryInfo( System.AppDomain.CurrentDomain.BaseDirectory  ); //+ "/bin"
    FileInfo[] fis = di.GetFiles("*.dll"); foreach(FileInfo fi in fis)
    {
    Assembly a = Assembly.LoadFile(fi.FullName);

    Type[] types = a.GetTypes();
    foreach(Type type in types)
    {
    if( IsInheritFromInterface( type , t ) )
    list.Add( type ) ; 
    }
    } return list;
    } private static bool IsInheritFromInterface( Type child ,  Type parent )
    {
    Type ii = child.GetInterface( parent.FullName , false ) ; if( ii != null ) return true ;// Type[] ii = child.GetInterfaces() ;
    //
    // foreach( Type i in ii )
    // {
    // if( i == parent )
    // return true ;
    // }// Type p = child.BaseType ;
    // while( p != null )
    // {
    // if( p == parent ) return true ;
    //
    // p = p.BaseType ;
    // } return false ;
    }
    }
      

  2.   

    //有参数的构造函数
    object obj = System.Activator.CreateInstance(typeof(System.Text.StringBuilder),new object[]{"ABC"});
      

  3.   

    TypeLoader Objects =new  TypeLoader( t);
    for (int i = 0; i < Objects.ObjectCount; i++)
    {
    object Myobject = Objects[i];
    Project8.Interface1 Myinterface = (Project8.Interface1)Myobject ;
    button1.Text =Myinterface.Method1();
    }
    能够得到对象了,但是Project8.Interface1 Myinterface = (Project8.Interface1)Myobject ;
    为什么总是报异常?
      

  4.   

    什么异常啊???TypeLoader是为window form写的,在web下要修改:DirectoryInfo di = 
    new DirectoryInfo( System.AppDomain.CurrentDomain.BaseDirectory+ "/bin" ); //+ "/bin"
    //这样调用:
    //Interface1的实现要有一个无参构造函数,实现所在的dll要在bin目录中。
    ArrayList list = TypeLoader.LoadObjects( typeof(Project8.Interface1) );for (int i = 0; i < list .Count; i++)
    {
    object Myobject = TypeLoader.CreateInstance( list [i] );
    Project8.Interface1 Myinterface = (Project8.Interface1)Myobject ;
    button1.Text =Myinterface.Method1();
    }
      

  5.   

    ---------------------------
    Debugger Exception Notification
    ---------------------------
    Project Project10.exe encountered unhandled exception class System.InvalidCastException with message 'Specified cast is not valid.'.
    ---------------------------
    Break   Continue   Help   
    ---------------------------
    以上是执行到Project8.Interface1 Myinterface = (Project8.Interface1)Myobject ;产生的异常
      

  6.   

    改为这样后
    Project8.Interface1 Myinterface = Myobject as  Project8.Interface1;
    if (Myinterface != null)
    button1.Text = Myinterface.Method1();
    Myinterface 总是为null.
    但是断在Project8.Interface1 Myinterface = Myobject as  Project8.Interface1;上都能够看到Method1方法返回的值了
      

  7.   

    检查一下Interface1的实现类有无问题?直接实例化呢?
      

  8.   

    工程中加入Project8.dll后去掉Interface1.cs就可以实例化并转换为Interface1.
    但是不取掉Interface1.cs转换就失败。