我需要使用如下方法:
DoSomething<(T)>();
Run Code Online (Sandbox Code Playgroud)
但我不知道我有哪种类型,只有类型的对象.如果我只有以下情况,我该如何调用此方法:
Type typeOfGeneric;
Run Code Online (Sandbox Code Playgroud)
如果您只有一个指定为 Type 的 Type,则必须构建泛型方法,并通过反射调用它。
Type thisType = this.GetType(); // Get your current class type
MethodInfo doSomethingInfo = thisType.GetMethod("DoSomething");
MethodInfo concreteDoSomething = doSomethingInfo.MakeGenericMethod(typeOfGeneric);
concreteDoSomething.Invoke(this, null);
Run Code Online (Sandbox Code Playgroud)