using System.Reflection; public class CreateByObjectName { public static object Create( string asmName, string typeName, object[] args ) { Assembly assemblyInstance = null; Type typeInstance = null; try { // use full assembly name to get assembly instance assemblyInstance = Assembly.Load( asmName ); } catch ( Exception e ) { throw new UIPException( Resource.ResourceManager.FormatMessage( "RES_ExceptionCantLoadAssembly", asmName ), e ); } // use type name to get type from assembly typeInstance = assemblyInstance.GetType( typeName , true, false); try { if( args != null ) { return Activator.CreateInstance( typeInstance, args); } else { return Activator.CreateInstance( typeInstance); } } catch( Exception e ) { throw new UIPException( Resource.ResourceManager.FormatMessage( "RES_ExceptionCantCreateInstanceUsingActivate", typeInstance ), e ); } }}
public class CreateByObjectName
{
public static object Create( string asmName, string typeName, object[] args )
{
Assembly assemblyInstance = null;
Type typeInstance = null; try
{
// use full assembly name to get assembly instance
assemblyInstance = Assembly.Load( asmName );
}
catch ( Exception e )
{
throw new UIPException( Resource.ResourceManager.FormatMessage( "RES_ExceptionCantLoadAssembly", asmName ), e );
} // use type name to get type from assembly
typeInstance = assemblyInstance.GetType( typeName , true, false); try
{
if( args != null )
{
return Activator.CreateInstance( typeInstance, args);
}
else
{
return Activator.CreateInstance( typeInstance);
}
}
catch( Exception e )
{
throw new UIPException( Resource.ResourceManager.FormatMessage( "RES_ExceptionCantCreateInstanceUsingActivate", typeInstance ), e );
}
}}
try
{
Assembly tempAssembly = Assembly.GetExecutingAssembly();
// if class is located in another DLL or EXE, use something like
// Assembly tempAssembly = Assembly.LoadFrom("myDLL.DLL");
// or
// Assembly tempAssembly = Assembly.LoadFrom("myEXE.exe");
Form frm1 = (Form) tempAssembly.CreateInstance(textBox1.Text);// as Form;
frm1.Show();
}
catch(Exception ex)
{
MessageBox.Show("Error creating: "+ textBox1.Text);
}
这个是简单得实现方法,呵呵谢谢以上得各位,给我解决了大问题
http://expert.csdn.net/Expert/topic/2214/2214138.xml?temp=.84128974、如何使用反射获取类型
首先我们来看如何获得类型信息。
获得类型信息有两种方法,一种是得到实例对象
这个时侯我仅仅是得到这个实例对象,得到的方式也许是一个object的引用,也许是一个接口的引用,但是我并不知道它的确切类型,我需要了解,那么就可以通过调用System.Object上声明的方法GetType来获取实例对象的类型对象,比如在某个方法内,我需要判断传递进来的参数是否实现了某个接口,如果实现了,则调用该接口的一个方法:
…
public void Process( object processObj )
{
Type t = processsObj.GetType();
if( t.GetInterface(“ITest”) !=null )
…
}
…
另外一种获取类型的方法是通过Type.GetType以及Assembly.GetType方法,如:
Type t = Type.GetType(“System.String”);
需要注意的是,前面我们讲到了命名空间和装配件的关系,要查找一个类,必须指定它所在的装配件,或者在已经获得的Assembly实例上面调用GetType。
本装配件中类型可以只写类型名称,另一个例外是mscorlib.dll,这个装配件中声明的类型也可以省略装配件名称(.Net装配件编译的时候,默认都引用了mscorlib.dll,除非在编译的时候明确指定不引用它),比如:
System.String是在mscorlib.dll中声明的,上面的Type t = Type.GetType(“System.String”)是正确的
System.Data.DataTable是在System.Data.dll中声明的,那么:
Type.GetType(“System.Data.DataTable”)就只能得到空引用。
必须:
Type t = Type.GetType("System.Data.DataTable,System.Data,Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
这样才可以,大家可以看下面这个帖子:
http://expert.csdn.net/Expert/topic/2210/2210762.xml?temp=.1919977
qqchen的回答很精彩
5、如何根据类型来动态创建对象
System.Activator提供了方法来根据类型动态创建对象,比如创建一个DataTable:Type t = Type.GetType("System.Data.DataTable,System.Data,Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");DataTable table = (DataTable)Activator.CreateInstance(t); 例二:根据有参数的构造器创建对象
namespace TestSpace {
public class TestClass
{
private string _value;
public TestClass(string value) {
_value=value;
}
}
}
…
Type t = Type.GetType(“TestSpace.TestClass”);
Object[] constructParms = new object[] {“hello”}; //构造器参数
TestClass obj = (TestClass)Activator.CreateInstance(t,constructParms);
…
把参数按照顺序放入一个Object数组中即可