在我的应用程序中,我有一个名为'property'的Reflection.PropertyInfo
变量。当我执行property.GetValue(myObject,null)
时,值为Master.Enterprise
。 Enterprise是我的应用程序中的一个类。因此,'property'包含对我的应用中的类的引用。
在运行时,我想以某种方式将'property'强制转换为它的类型(Master.Enterprise
),这样我就可以使用它,就好像它是类类型一样。
我知道这可以做到,因为当我查看调试器中的代码时,调试器正确地将'property'转换为它引用的类型,并且我可以在调试器中看到Enterprise类的所有属性。
我该怎么做呢?
答案 0 :(得分:5)
在我看来,你需要定义一个接口 - 然后你可以要求你的属性返回一个实现该接口的对象,然后你就可以转换到该接口,而不管哪个类实现了该接口: / p>
IEnterprise enterprise = (IEnterprise)property.GetValue(myObject, null);
您唯一的另一个选择是使用反射在返回的对象上调用方法和属性 - 这是visual studio调试器正在执行的操作。
答案 1 :(得分:3)
Master.Enterprise enterprise = (Master.Enterprise)property.GetValue(myObject,null);
或
Master.Enterprise enterprise = property.GetValue(myObject,null) as Master.Enterprise;
如果类型不兼容,第一个将抛出异常。如果不兼容,第二个将返回null。
答案 2 :(得分:2)
如果类型在编译时是未知的,并且您不能假设它实现了已知的接口,则无法将其转换为该类型,因为c#是静态类型语言。
如果您想等待它/使用测试版,您将能够使用c#4.0执行此操作,例如:
dynamic obj=property.GetValue(myObject,null);
obj.CallSomeMethod();
但是请注意,内部只会调用反射方法来确定如何执行此操作。你可以通过自己广泛的反思来做到这一点。如果您向我们提供有关您对返回对象的具体要求的更多信息,我将能够为您提供更详细的答案。
答案 3 :(得分:1)
如果您有类型名称的字符串,则可以执行以下操作:
Assembly execAsm = Assembly.GetExecutingAssembly();
object instance = AppDomain.CurrentDomain.CreateInstanceAndUnwrap(execAsm.FullName, "RipperWorkbench.Classes.LoadManager"); // the type name cam come from a variable or someplace else.
然后您可以将其转换为您需要的类型
Master.Enterprise ent = obj as Master.Enterprise;
if (obj != null)
{
...
}
或者,让对象实现一个接口: 您必须确保该类型已加载到当前AppDomain中,否则无法反映该类型。
Assembly asm = Assembly.GetExecutingAssembly();
// The executing assembly can change also; so you can load types from other assemblies
// use this if the type isn't loaded in this AppDomain yet.
//Assembly asm = Assembly.LoadFile("path to assembly here", new Evidence());
String typeToLoad = "Master.Enterprise"; // this can be from a config or database call
Type objType = asm.GetType(typeToLoad, true);
if (!objType.IsPublic)
return null;
// make sure the type isn't Abstract
if (((objType.Attributes & TypeAttributes.Abstract) == TypeAttributes.Abstract))
return null;
// IEnterprise is the interface that all of the plugin types must implement to be loaded
Type objInterface = objType.GetInterface("IEnterprise", true);
if (objInterface == null)
return null;
var iri = (IEnterprise)Activator.CreateInstance(objType);
return iri;
答案 4 :(得分:1)
将来您可以使用dynamic
。现在我觉得你应该use the solution with the interface。