I use this code to dynamically invoke a getter from a transform component. Code (CSharp): 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) Spoiler: Exception I tried many things, but none of them works. Do you have some idea to solve it? I'm shocked that it works in a .NET project.
might be that this is something that works with more recent .NET framework version? i think unity is still on .NET 2.0...
@LeftyRighty This could be a possible reason. But I changed the Freamework version down to 2.0 and it stills works in VS. I also tried to change it to Unity 3.5 .NET and it works too.
When I use Code (CSharp): var invoked = method.Invoke(this.Value, new object[0]); System.Convert.ChangeType(invoked, method.ReturnType); Debug.Log("Invoke " + invoked); it also works. But I don't want to save the whole MethodInfo object, I only want the delegate.