如何通过反射实例化一个类

maliguo_net 2003-03-28 10:41:09
代码如下:
首先class1 提供接口
namespace ClassLibrary1
{
public interface yy
{
void test();
}
public class Class1:yy
{
public void test()
{
System.Console.WriteLine("hello word");

}

}
}

我想达到通过接口调用该类(该类我只知道它的类名,以及名称空间)
等同于
Class1 ss=new Class1();?????// 如何通过反射实例化该类
yy obj=(yy)ss;
obj.test();//接口是知道的

各位大侠,快来支援,我搞不定了







...全文
969 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
yarshray 2003-03-28
  • 打赏
  • 举报
回复
这是很早以前我写的一个例子,你参考一下;

using System;

public class SayName {

private const String SayName = "I’m Yarshray";



public SayMyName() {

}



public void OutPutName() {



Console.WriteLine(SayName);

}

}





using System;

using System.Reflection;

public class YarshrayReflection {

public static void Main() {



Assembly as = Assembly.Load("SayMyName");



Type t = as .GetType("SayMyName");



MethodInfo mi = t.GetMethod("OutPutName");



Object o = Activator.CreateInstance(t);



mi.Invoke(o);

}

}
maliguo_net 2003-03-28
  • 打赏
  • 举报
回复
好的,我再试试
maliguo_net 2003-03-28
  • 打赏
  • 举报
回复
这样还是提示“指定的转换无效”,并且必需添加引用?
能不能不添加引用?
timmy3310 2003-03-28
  • 打赏
  • 举报
回复
System.Reflection.Assembly a = System.Reflection.Assembly.LoadFrom(@"E:\代码练习\ClassLibrary1\bin\Debug\ClassLibrary1.dll");
这种方式也应该可以啊,我这里测试都通过了

你看看是哪一句出的错?
System.Reflection.Assembly a = System.Reflection.Assembly.LoadFrom(@"E:\代码练习\ClassLibrary1\bin\Debug\ClassLibrary1.dll");
Type t = a.GetType("ClassLibrary1.Class1");
if( t==null )
//判断一下T是不是为null,如果不是,证明Type已经获取到

object o = Activator.CreateInstance(t);

把o.GetType().Name打出来看看
另外:

if( o is yy )
//判断O是否实现了接口,如果没有,检查ClassLibrary1.Class1的源代码,重新编译试试
dragontt 2003-03-28
  • 打赏
  • 举报
回复
Assembly asm = Assembly.LoadFrom(@"c:\youpath\yourdllname.dll");
Type t = asm.GetType("YourNamespace.YourClassName");
IYourInterface obj = (IYourInterface)t.CreateInstance();

__>

Assembly asm = Assembly.LoadFrom(@"c:\youpath\yourdllname.dll");
Type t = asm.GetType("YourNamespace.YourClassName");
IYourInterface obj = (IYourInterface)Activator.CreateInstance(t);
maliguo_net 2003-03-28
  • 打赏
  • 举报
回复
Assembly asm = Assembly.LoadFrom(@"c:\youpath\yourdllname.dll");
Type t = asm.GetType("YourNamespace.YourClassName");
IYourInterface obj = (IYourInterface)t.CreateInstance();

编译不过来去,t 没有该方法
maliguo_net 2003-03-28
  • 打赏
  • 举报
回复
我已添加引用了,

第一种情况,是不行的,还是提示“指定转换无效”
System.Reflection.Assembly a = System.Reflection.Assembly.LoadFrom(@"E:\代码练习\ClassLibrary1\bin\Debug\ClassLibrary1.dll");

第二种情况是可以的
System.Reflection.Assembly a = System.Reflection.Assembly.Load("ClassLibrary1");

能不能不添加引用?直接调用


kamphkb 2003-03-28
  • 打赏
  • 举报
回复
简单的说:
Assembly asm = Assembly.LoadFrom(@"c:\youpath\yourdllname.dll");
Type t = asm.GetType("YourNamespace.YourClassName");
IYourInterface obj = (IYourInterface)t.CreateInstance();
timmy3310 2003-03-28
  • 打赏
  • 举报
回复
请把添加对ClassLibrary1.dll的引用,然后

System.Reflection.Assembly a = System.Reflection.Assembly.LoadFrom(@"E:\代码练习\ClassLibrary1\bin\Debug\ClassLibrary1.dll");

-》

System.Reflection.Assembly a = System.Reflection.Assembly.Load("ClassLibrary1");
maliguo_net 2003-03-28
  • 打赏
  • 举报
回复
不好意思,出错了,提示指定的转换无效
System.Reflection.Assembly a = System.Reflection.Assembly.LoadFrom(@"E:\代码练习\ClassLibrary1\bin\Debug\ClassLibrary1.dll");
Type t = a.GetType("ClassLibrary1.Class1");

yy obj=(yy)Activator.CreateInstance(t);

obj.test();
timmy3310 2003-03-28
  • 打赏
  • 举报
回复

需要调用LoadFrom来Load装配件,然后调用System.Reflection.Assembly的GetType方法获取Type实例,根据Type的实例用Activator.CreateInstance创建对象

另外,如果是全局缓存里面的装配件,就使用:LoadWithPartialName

例如:
System.Reflection.Assembly a = System.Reflection.Assembly.LoadWithPartialName("System.Web");

t = a.GetType("System.Web.UI.WebControls.Button");

Button btn = (Button)Activator.CreateInstance( t );
dragontt 2003-03-28
  • 打赏
  • 举报
回复
ObjectHandle -- System.Runtime.Remoting 命名空间

不过,刚才测试了
这样写,要创建的对象
必须在你当前执行的程序集中

还有把String.Empty -- 改为null
maliguo_net 2003-03-28
  • 打赏
  • 举报
回复
如果该类在当前装配件下,是可以调用的
但是,该类不是在当前装配件下,如何做?是不是需要用道loadform();
timmy3310 2003-03-28
  • 打赏
  • 举报
回复
你要确定你的类在哪个Dll里面,如果是在当前项目里面,那么上面的方法应该就可以,否则,你可以这样:

System.Reflection.Assembly a = System.Reflection.Assembly.LoadFrom("装配件名称");

t = a.GetType("ClassLibrary1.Class1");

yy obj=(yy)Activator.CreateInstance(t);

obj.test();
maliguo_net 2003-03-28
  • 打赏
  • 举报
回复
ObjectHandle 是引用那个名称空间?
maliguo_net 2003-03-28
  • 打赏
  • 举报
回复
没有在当前的装配件里面?
dragontt 2003-03-28
  • 打赏
  • 举报
回复
改成
ObjectHandle h = Activator.CreateInstance(String.Empty,"ClassLibrary1.Class1" );
Object o = h.Unwrap();
yy obj=(yy)o;
yarshray 2003-03-28
  • 打赏
  • 举报
回复
yy obj=Activator.CreateInstance(Type.GetType("ClassLibrary1.Class1"));
timmy3310 2003-03-28
  • 打赏
  • 举报
回复
你的Class1在不在当前的装配件里面?
maliguo_net 2003-03-28
  • 打赏
  • 举报
回复
楼上的兄弟,非常感谢
但是一运行,提示“ 值不能为空”
加载更多回复(14)

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧