-4
// this is code
    public class Foo1Parent
    {
    }
    public delegate void CallBack<T>(T arg1);
    public delegate void CallBack(); 
    public class Foo
    {
    public void openWindow<T>(CallBack<T> onWindowAwake) where T:Foo1Parent
     {
      Debug.Log("test");
      onWindowAwake(T);
     }
//I use reflection to call methods "openWindow"
 public void openCWindow(CallBack onback, string dialogName)
    {
        Type t = Assembly.GetExecutingAssembly().GetType(dialogName);
        MethodInfo meth1 = this.GetType().GetMethod("openWindow");
       object[] obj = new object[] { null }
        meth.Invoke(this, obj );
    }

} //

Blockquote

The parameters of a generic is CallBack onWindowAwake I don't know how to do it

  • 3
    This is not C code. Please [edit] your question and use correct language tag. – user694733 Oct 24 '19 at 09:58
  • This is not a Java code as well. @flyhighandlong, You would be get faster response if the tags are correct. Please try to figure out the programming language and re-tag the question. – Prashant Oct 24 '19 at 10:11
  • @Prashant You are correct, looks more like c#, corrected – Ctx Oct 24 '19 at 10:14
  • It's completely unclear what you are trying to do, or why. You aren't doing anything with the `t` variable or the `onback` parameter. What is the expected value of `dialogName`? –  Oct 25 '19 at 03:20
  • I'm guessing this is a duplicate of [How do I use reflection to call a generic method](https://stackoverflow.com/questions/232535/how-do-i-use-reflection-to-call-a-generic-method) –  Oct 25 '19 at 03:27
  • The parameters of a generic is CallBack onWindowAwake I don't know how to do it – flyhighandlong Oct 25 '19 at 03:45

1 Answers1

0

It is unclear what the question here is but, from the looks of things you are asking:

How do I invoke the generic method openWindow using reflection?

If this is the question then what you need to do after you get the MethodInfo which has the generic definition of openWindow is to create a generic method using MakeGenericMethod

Example:

MethodInfo genericDefinition = this.GetType().GetMethod("openWindow");

MethodInfo genericMethod = genericDefinition.MakeGenericMethod(typeof(Foo1Parent));

object[] obj = new object[] { null };

genericMethod.Invoke(this, obj);

This is the only way you can invoke a generic method, it is not possible to invoke a definition because, it doesn't know what T is.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Vincent Bree
  • 425
  • 4
  • 10
  • Thanks, this is a generic reflection call,This generic T is created in openWindow, you mean you can't do this, I use another method to do it, no generics, thank you very much, you can write Chinese, it's amazing. – flyhighandlong Oct 28 '19 at 05:51