C # - selecting a Reflection.PropertyInfo object for its type

In my application, I have a variable Reflection.PropertyInfocalled 'property'. When I do property.GetValue(myObject,null), value Master.Enterprise. An enterprise is a class in my application. So, the “property” contains a reference to the class in my application.

At runtime, I would like to somehow drop the “property” to its type ( Master.Enterprise) so that I can use it as if it were a class type.

I know that this can be done because when I look at the code in the debugger, the debugger correctly passes the "property" to its link type, and I can see all the properties of the Enterprise class in the debugger.

How can i do this?

+3
source share
5 answers

If you have a string like Name, you can do something like this:

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.

then you can apply it to the type you need.

Master.Enterprise ent = obj as Master.Enterprise;
if (obj != null) 
{
     ...
}

Or, conversely, if an object implements an interface: you must be sure that the type is loaded into the current AppDomain, otherwise the type cannot be reflected.

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;
+1
source

It seems to me that you need to define an interface - then you can require your property to return an object that implements this interface, and then you can use it in this interface regardless of which class implements this interface:

IEnterprise enterprise = (IEnterprise)property.GetValue(myObject, null);

The only thing you have is calling the methods and properties of the returned object using reflection is what the visual studio debugger does.

+5
source
Master.Enterprise enterprise = (Master.Enterprise)property.GetValue(myObject,null);

Master.Enterprise enterprise = property.GetValue(myObject,null) as Master.Enterprise;

, . null, .

+3

, , , , , # .

# 4.0, / -, :

dynamic obj=property.GetValue(myObject,null);
obj.CallSomeMethod();

, , , , . , . , , .

+2

dynamic. , .

+1

Source: https://habr.com/ru/post/1723664/


All Articles