请教类型转换的问题

maotin 2008-08-06 11:20:36
定义了一个基类,如下:

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace BusinessRules
{
public interface IBSBase
{
DataSet GetAll();
}
}


添加一个子类,如下:

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace BusinessRules
{
public class Class1:IBSBase
{
public DataSet GetAll()
{
return null;
}
}
}


然后在程序中,使用反射创建子类,代码如下:

Type obj = System.Reflection.Assembly.LoadFile(filePath).GetType("BusinessRules.Class1");
//该处出现bug,抛出:无法将类型为“BusinessRules.Class1”的对象强制转换为类型“BusinessRules.IBSBase”。
BusinessRules.IBSBase rules = (BusinessRules.IBSBase)Activator.CreateInstance(obj);


请教这里怎么得到BusinessRules.IBSBase ;我希望能通过反射得到的子类实例转换成基类!
请各位多指教,谢谢!
...全文
693 50 打赏 收藏 转发到动态 举报
写回复
用AI写文章
50 条回复
切换为时间正序
请发表友善的回复…
发表回复
Java_And_NET 2010-02-22
  • 打赏
  • 举报
回复
实 现 接 口 了 吗 ?
capeyf 2008-08-07
  • 打赏
  • 举报
回复
根据楼上的回答我的测试也已经调通了 确实是assmbly虽然是一个dll但是引用的时候却是不一样的
ws_hgo 2008-08-06
  • 打赏
  • 举报
回复
没有用反射
Learning........
vwxyzh 2008-08-06
  • 打赏
  • 举报
回复
出现这个问题,通常是因为Assembly的目录和应用程序的目录不同而导致的
BillMhw 2008-08-06
  • 打赏
  • 举报
回复
学习了
_空指针_ 2008-08-06
  • 打赏
  • 举报
回复
BusinessRules.IBSBase rules = null;
rules = Activator.CreateInstance(obj) as BusinessRules.IBSBase;
songyingjian2008 2008-08-06
  • 打赏
  • 举报
回复
学习
maotin 2008-08-06
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lovefootball 的回复:]
反射时的BusinessRules.IBSBase 和Class1继承的接口是否在同一个DLL???

Type t = typeof(BusinessRules.Class1);
BusinessRules.IBSBase rules = Activator.CreateInstance(t) as BusinessRules.IBSBase;
我这样是没问题的~~
[/Quote]
是在同一个dll,命名空间也一致

//如果这样处理,可以转换成功;
Type t = typeof(BusinessRules.Class1);
//如果使用下面这种方式,转换后为null,filePath为dll的路径,pBusinessName="BusinessRules.Class1"
Type t = System.Reflection.Assembly.LoadFile(filePath).GetType(pBusinessName);
BusinessRules.IBSBase rules = Activator.CreateInstance(t) as BusinessRules.IBSBase;
vwxyzh 2008-08-06
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 gomoku 的回复:]
1. The reason is that copying class/interface definition into your projects won't make them the identical type. So you need
to have a common dll that contains the interface (IBSBase.dll).

2. In your Class1 project add reference to IBSBase.dll

3. In your project that Activator.CreateInstance(), add reference to IBSBase.dll.

4. Then it is ok to cast: BusinessRules.IBSBase rules = (Busine…
[/Quote]
额外加一条,反射加载的dll要在相同目录,否则即使IBSBase.dll的版本相同也会出现类型不同的情况
codehunter008 2008-08-06
  • 打赏
  • 举报
回复
11楼正解!
stning 2008-08-06
  • 打赏
  • 举报
回复
肯定可以,就as。楼上有人给出解决方案了
Tll_W 2008-08-06
  • 打赏
  • 举报
回复
关注一下
smartkide 2008-08-06
  • 打赏
  • 举报
回复
学习中,帮顶.
wangxb8888 2008-08-06
  • 打赏
  • 举报
回复
路过
ilovey4 2008-08-06
  • 打赏
  • 举报
回复
不懂 帮顶
gomoku 2008-08-06
  • 打赏
  • 举报
回复
1. The reason is that copying class/interface definition into your projects won't make them the identical type. So you need
to have a common dll that contains the interface (IBSBase.dll).

2. In your Class1 project add reference to IBSBase.dll

3. In your project that Activator.CreateInstance(), add reference to IBSBase.dll.

4. Then it is ok to cast: BusinessRules.IBSBase rules = (BusinessRules.IBSBase)Activator.CreateInstance(obj);
Dobzhansky 2008-08-06
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lovefootball 的回复:]
反射时的BusinessRules.IBSBase 和Class1继承的接口是否在同一个DLL???

Type t = typeof(BusinessRules.Class1);
BusinessRules.IBSBase rules = Activator.CreateInstance(t) as BusinessRules.IBSBase;
我这样是没问题的~~
[/Quote]

很可能是不同程序集的问题把
Dobzhansky 2008-08-06
  • 打赏
  • 举报
回复
我这样可以转换

assembly = Assembly.Load(_assemblyName);// Assembly.LoadFrom(assemblyName);
_providerInstance = (EcaProvider)assembly.CreateInstance(_providerName);

hiddkiller 2008-08-06
  • 打赏
  • 举报
回复
BusinessRules.IBSBase rules =(BusinessRules.IBSBase)Activator.CreateInstance(obj);

这样不行吗?
lovefootball 2008-08-06
  • 打赏
  • 举报
回复
反射时的BusinessRules.IBSBase 和Class1继承的接口是否在同一个DLL???

Type t = typeof(BusinessRules.Class1);
BusinessRules.IBSBase rules = Activator.CreateInstance(t) as BusinessRules.IBSBase;
我这样是没问题的~~
加载更多回复(30)

110,539

社区成员

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

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

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