samedi 24 juin 2017

C# Delegate vs Reflection

I'm trying to optimize a method and can't get to the solution..

I have the following class:

public class X : MyInterface<Model>
{
    public void Execute(Model m) { }
}

I am trying to invoke Execute(...) I know the Type of X and I know the Type of Model.

This works:

(here logic to find the correct method).Method.Invoke(InstanceX, new object[] { m });

This does not but should work faster:

(here logic to find the correct method).Method.CreateDelegate(Expression.GetDelegateType(typeof(m)), instanceX);

Error: Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.

Again; trying to optimize, so any other solution that works faster than the reflection Invoke would be cool.





Aucun commentaire:

Enregistrer un commentaire