lundi 9 mai 2016

C# Get an object which is a property and execute a method in the object from a string value

I have a class that have objects that are properties. Each of these objects has methods in them. From a string value/ variable I would like to get this property which is an object and execute its method.

var property = this.GetType().GetProperty( "SomePropertyObject" );
MethodInfo getMethod = property.GetMethod;
var obj = getMethod.Invoke......
var runMethod = obj.GetType().GetMethod( "SomeMethod" );
runMethod.Invoke(obj, new object[] { value1 } );

The problem is that when I get the property I only have the get and set methods to work with. If I can get the object via the get method, I can run the objects methods. Please can you tell me if this is possible and how do I go about doing it?

Your help is greatly appreciated.





Aucun commentaire:

Enregistrer un commentaire