有没有办法在不同类型的不同组件之间进行强制转换?我需要执行一个函数,它的程序集已经加载了Assembly.Load(ReadAllBytes(...)),但是它在参数投射中失败了。那么,有没有什么办法可以在c#中“reinterpret_cast”对象?强制铸造一种组装类型到另一种组装类型在c#
编辑我铸钢问题的
最基本的例子是:
Assembly ass = Assembly.Load(File.ReadAllBytes("external.dll"))
object other_type_instance = ass.GetType("OtherType").InvokeMember(null, BindingFlags.CreateInstance, null, null, new Object[]{});
OtherType casted_isntance = (OtherType)other_type_instance; // fails with runtime error, because there are two OtherType:s classes loaded.
请提供一些代码 – 2010-05-11 21:12:35
我认为你会更乐意编写一个适配器类来包装一种类型并提供与另一种类型兼容的接口。当然,如果其他类型是密封的或其他不可变的,那么你很大程度上没有运气。 – 2010-12-10 19:49:19