I use this code to dynamically invoke a getter from a transform component.
public delegate T1 GenericGet<T1>(Transform t);
private static System.Delegate createdDelegate;
public void Init()
{
PropertyInfo[] infos = typeof(Transform).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo info in infos)
{
// Save getter
MethodInfo method = info.GetGetMethod();
System.Type returnType = method.ReturnType;
//System.Type paramType = method.GetParameters()[0].GetType();
System.Type newType = typeof(GenericGet<>).MakeGenericType(returnType);
createdDelegate = System.Delegate.CreateDelegate(newType, method);
//var instance = System.Activator.CreateInstance(newType);
var obj = createdDelegate.DynamicInvoke(this.Value);
}
}
This code works perfectly in a .NET project. (VS) But in Unity I got a TargetCount Exception. (line 17)
I tried many things, but none of them worked. Do you have any idea to solve it?
I'm shocked that it works in a .NET project.
var invoked = method.Invoke(this.Value, new object[0]);
System.Convert.ChangeType(invoked, method.ReturnType);
Debug.Log("Invoke " + invoked);
.. also works. But I don't want to save the whole MethodInfo object, I only want the delegate.
Aucun commentaire:
Enregistrer un commentaire