How can I invoke a Method Property from a MethodInfo (Reflection) while keeping it's properties and types generic?

public class MyComponent{
void MyBoolClass(bool value){};
void MyFloatClass(float value){};
float MyReturnFloat(){return 1f;};
}

private Action myAction = ()=>{};
public MyComponent myComponent;
void Start()
{
    MethodInfo methods = myComponent.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

    //I will want to create a list later, just using the first index as a code example
    myAction = (Action)Delegate.CreateDelegate(typeof(Action), this, methods[0]);
    //Currently getting an Exeption
 }

I am trying to invoke a method from an array of methods but I running into a problem, how would I go about using dynamic generic properties? In the example how am I able to create an action to handle the MyBoolClass and MyFloatClass?

Thanks guys!

Your question doesn’t make much sense. You have a delegate type that takes no parameters and has no return type. All your methods either have a parameter or return a value. Those methods have completely incompatible signatures. There is no “generic” delegate type that can be directly used. A generic type like Action<T> requires a type argument Action and Action<T> are also completely incompatible to each other.

What exactly are you trying to achieve? If you want “somehow” assign “MyBoolClass” or “MyFloatClass” to an Action delegate you would need a closure that includes the parameter.

For example you would need a class like this:

public class BoolClosure
{
    public bool parameter;
    public System.Action<bool> method;
    public BoolClosure(MethodInfo aMethod, object aObj, bool aParameter)
    {
        var t = typeof(System.Action<bool>);
        method = (System.Action<bool>)System.Delegate.CreateDelegate(t, aObj, aMethod);
    }
    public void Execute()
    {
        method(parameter);
    }
}

Usually closure classes are generated by the compiler automatically. However since you want to create them dynamically you have to create them yourself.

You can use it like this:

MethodInfo method = myComponent.GetType().GetMethod("MyBoolClass", BindingFlags.Instance | BindingFlags.NonPublic);

myAction = new BoolClosure(method, myComponent, true).Execute;

Now when you invoke myAction like this:

myAction();

You will actually invoke the method “MyBoolClass” with the parameter “true”. What we just manually created actually happens when you do something like:

myAction = ()=>myComponent.MyBoolClass( true );

Though we still don’t know what you actually want to do. You should add more information on your background. For what purpose would you need this?

Hi there

Instead of trying to store an action created by CreateDelegate (which is strongly typed), store the MethodInfo. Then use MethodInfo.Invoke to call the method, which just takes a set of objects as arguments and should do the conversions/ type checking internally.

Generally the CreateDelegate is there as a tool for when you specifically need to get a way of calling the code without reflection such as attaching it to an event, or passing into other code that doesn’t handle reflection. As you can imagine, this naturally imposes the strongly typed properties of c#, so at that point you lose the ability to ignore types.

Chris