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:反射实例化/// <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 ;
}
}
object obj = System.Activator.CreateInstance(typeof(System.Text.StringBuilder),new object[]{"ABC"});
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 ;
为什么总是报异常?
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();
}
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 ;产生的异常
Project8.Interface1 Myinterface = Myobject as Project8.Interface1;
if (Myinterface != null)
button1.Text = Myinterface.Method1();
Myinterface 总是为null.
但是断在Project8.Interface1 Myinterface = Myobject as Project8.Interface1;上都能够看到Method1方法返回的值了
但是不取掉Interface1.cs转换就失败。