[Reflection] How do I convert my MethodInfo definition to a MethodInfo where the declaring type is a generic type instance? #99591
Replies: 2 comments 7 replies
-
Not sure if this is 100% correct, but you can just make a generic type from method's info declaring type: class Program
{
static void Main()
{
var method = typeof(GenericT<>).GetMethod("PrintName", BindingFlags.Static | BindingFlags.NonPublic);
var genInstanceMet = method.DeclaringType.MakeGenericType(typeof(string)).GetMethod(method.Name, BindingFlags.Static | BindingFlags.NonPublic);
genInstanceMet.Invoke(null, null); // prints System.String
}
}
public class GenericT<T>
{
private static void PrintName()
{
Console.WriteLine(typeof(T).FullName);
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
var genInstanceMet = (MethodInfo)typeof(GenericT<string>).GetMemberWithSameMetadataDefinitionAs(method); |
Beta Was this translation helpful? Give feedback.
6 replies
Answer selected by
slxdy
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Right now, I have this code
How do I convert the
method
from a definition to a reference where the declaring type is a generic type instance?Edit: I mean without modifying the line where I define the
method
variableBeta Was this translation helpful? Give feedback.
All reactions