namespace ConsoleApp3
{
public class AA
{
public string name = "";
}
class Program
{
public string aaaaa = "";
public void PrintTypeParameter<T>(string name1, int count, T aa)
{
Console.WriteLine(typeof(T) + " " + aa.GetType());
Console.WriteLine(name1 + " " + count);
Console.WriteLine((aa as AA)?.name);
Console.WriteLine(this.aaaaa);
Console.ReadKey();
}
static void Main()
{
Type type = typeof(Program);
MethodInfo definition = type.GetMethod("PrintTypeParameter");
MethodInfo constructed;
Type a = typeof(AA);
Program p = new Program();
p.aaaaa = "hello ppppp";
constructed = definition.MakeGenericMethod(a);
constructed.Invoke(p, new object[] { "name1", 200, new AA() { name = "my name" } });
}
}
}
网友评论