一、介绍
最近没事干,就用闲暇时间写点东西,也记录一下温习历程。老人说的好,好记性,不如烂笔头。时间一长,当时记忆的再清楚,都会变得模糊,索性就写博客记录下来,如果下次需要,直接打开博客就找到了,不用去网上乱找了。
今天我要写一些有关反射创建类型实例和调用方法的文章,泛型的类型的创建和泛型方法的调用是普通类型(非泛型类型)的创建和调用还是有区别的,如果不知道,肯定会憋出一身臭汗,其实知道了,也没什么。怕时间长了,忘记。就把关键的代码写出来,为自己提个醒。
测试环境说明:
操作系统:Windows10 Professinal
开发工具:Visual Studio 2022
测试平台:Windows Console Application
框架平台:Net 7.0
二、我的代码
我的测试很简单,包含的很全,静态方法、实例方法、私有方法、重载构造函数和方法调用都有涉及,我更关注泛型,分为两种,一种是泛型类型的创建和实例化,第二种是泛型方法的调用。当然了,泛型方法的调用,也区分为:泛型类型的泛型方法的调用和非泛型类型的泛型方法的调用,废话不多说,直接上代码。
当然有泛型的创建使用,还有其他类型的访问,把所有情况都包好了。
辅助类型:
/// 非泛型类型的泛型方法反射调用。
/// </summary>
internal class InstanceGenericMethod
{
/// <summary>
/// 泛型方法不是生成占位符。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="S"></typeparam>
/// <param name="parameter"></param>
/// <param name="s"></param>
/// <returns></returns>
public T Generate<T, S>(T parameter, S s)
{
Console.WriteLine("非泛型类-泛型方法:Generate<T, S>(T parameter, S s) 执行");
return parameter;
}
}
/// <summary>
/// 泛型类型的泛型方法反射调用。
/// </summary>
/// <typeparam name="X"></typeparam>
internal class GenericClassMethod<X>
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="S"></typeparam>
/// <param name="parameter"></param>
/// <param name="s"></param>
/// <returns></returns>
public X Generate<T, S>(X x, T t, S s)
{
Console.WriteLine("泛型类-泛型方法:Generate<T, S>(T parameter, S s) 执行");
return x;
}
}
实例方法调用类型:
/// 针对实例类型方法反射的调用,包含重载方法的反射调用。
/// </summary>
internal class InstanceMethod
{
/// <summary>
/// 无参唯一实例方法。
/// </summary>
public void Process()
{
Console.WriteLine("Process()无参实例方法执行。");
}
/// <summary>
/// 多参实例方法。
/// </summary>
/// <param name="age"></param>
/// <param name="name"></param>
/// <param name="dateTime"></param>
public void DoSomething(int age,string name,DateTime dateTime)
{
Console.WriteLine("DoSomething(int age,string name,DateTime dateTime)多参实例方法执行。");
}
/// <summary>
/// 重载方法
/// </summary>
public void Generate()
{
Console.WriteLine($"Generate()");
}
/// <summary>
/// 重载方法
/// </summary>
/// <param name="age"></param>
/// <param name="name"></param>
public void Generate(int age,string name)
{
Console.WriteLine($"Generate(int age,string name)");
}
/// <summary>
/// 重载方法
/// </summary>
/// <param name="age"></param>
/// <param name="name"></param>
/// <param name="stream"></param>
public void Generate(int age, string name, Stream stream)
{
Console.WriteLine($"Generate(int age, string name, Stream stream)已执行");
}
/// <summary>
/// 有返回值的重载方法
/// </summary>
/// <param name="age"></param>
/// <param name="name"></param>
/// <param name="stream"></param>
public decimal Generate(int age, string name, Stream stream,decimal money)
{
Console.WriteLine($" Generate(int age, string name, Stream stream,decimal money) 已执行");
return money;
}
}
使用静态和实例方法反射调用的类型:
/// 通过反射调用私有方法。
/// </summary>
internal class PrivateMethodDemo
{
/// <summary>
/// 有参数的实例私有方法。
/// </summary>
/// <param name="age"></param>
/// <param name="name"></param>
private void PrivateInstanceMethodTest(int age, string name)
{
Console.WriteLine($"实例方法:年龄:{age};姓名:{name}");
}
/// <summary>
/// 有参数的静态私有方法。
/// </summary>
/// <param name="age"></param>
/// <param name="name"></param>
private static void PrivateStaticMethodTest(int age, string name)
{
Console.WriteLine($"静态方法:年龄:{age};姓名:{name}");
}
}
静态方法反射调用的类型:
/// 静态方法反射调用,包含重载静态方法
/// </summary>
internal class StaticMethod
{
/// <summary>
///
/// </summary>
public static void Process()
{
Console.WriteLine("Process()无参实例方法执行。");
}
/// <summary>
///
/// </summary>
/// <param name="age"></param>
/// <param name="name"></param>
/// <param name="dateTime"></param>
public static void DoSomething(int age, string name, DateTime dateTime)
{
Console.WriteLine("DoSomething(int age,string name,DateTime dateTime)多参实例方法执行。");
}
/// <summary>
/// 重载方法
/// </summary>
public static void Generate()
{
Console.WriteLine("重载方法:Generate() 方法执行。");
}
/// <summary>
/// 重载方法
/// </summary>
/// <param name="age"></param>
/// <param name="name"></param>
public static void Generate(int age, string name)
{
Console.WriteLine("重载方法:Generate(int age, string name) 方法执行。");
}
/// <summary>
/// 重载方法
/// </summary>
/// <param name="age"></param>
/// <param name="name"></param>
/// <param name="stream"></param>
public static void Generate(int age, string name, Stream stream)
{
Console.WriteLine("重载方法:Generate(int age, string name, Stream stream) 方法执行。");
}
/// <summary>
/// 有返回值的重载方法
/// </summary>
/// <param name="age"></param>
/// <param name="name"></param>
/// <param name="stream"></param>
public static decimal Generate(int age, string name, Stream stream, decimal money)
{
Console.WriteLine("重载方法:Generate(int age, string name, Stream stream, decimal money) 方法执行。");
return money;
}
}
反射调用重载构造函数的类型:
/// 反射调用重载构造函数
/// </summary>
internal class MultiCtorDemo
{
private readonly string? name;
private readonly DateTime dateTime;
private readonly int age;
/// <summary>
/// 无参构造函数
/// </summary>
public MultiCtorDemo()
{
}
/// <summary>
/// 重载构造函数
/// </summary>
/// <param name="name"></param>
public MultiCtorDemo(string name)
{
this.name = name;
}
/// <summary>
/// 重载构造函数
/// </summary>
/// <param name="age"></param>
public MultiCtorDemo(int age)
{
this.age = age;
}
/// <summary>
/// 重载构造函数
/// </summary>
/// <param name="age"></param>
/// <param name="name"></param>
/// <param name="dateTime"></param>
public MultiCtorDemo(int age,string name,DateTime dateTime)
{
this.age = age;
this.name = name;
this.dateTime = dateTime;
}
}
私有和受保护的构造函数反射调用的类型:
/// 私有和受保护的构造函数反射调用
/// </summary>
internal class PrivateCtorDemo
{
private readonly int age;
/// <summary>
/// 私有构造函数
/// </summary>
private PrivateCtorDemo() { }
/// <summary>
/// 受保护的构造函数
/// </summary>
/// <param name="age"></param>
protected PrivateCtorDemo(int age)
{
this.age = age;
}
}
测试代码:
{
Assembly assembly = Assembly.Load("PatrickLiu.DelegateDeep.AllStudy");
var myType = assembly.GetType("PatrickLiu.DelegateDeep.AllStudy.Reflections.MultiCtorDemo");
if (myType != null)
{
var instance = Activator.CreateInstance(myType);
var instance2 = Activator.CreateInstance(myType, new object[] { "魏延" });
var instance3 = Activator.CreateInstance(myType, new object[] { 123 });
var instance4 = Activator.CreateInstance(myType, new object[] { 123, "黄飞虎", DateTime.Now });
}
}
//202309181135、私有构造函数反射调用(只有无参构造函数)
{
Assembly assembly = Assembly.Load("PatrickLiu.DelegateDeep.AllStudy");
var myType = assembly.GetType("PatrickLiu.DelegateDeep.AllStudy.Reflections.PrivateCtorDemo");
if (myType != null)
{
var obj = Activator.CreateInstance(myType, true);
}
}
//202309181151、泛型类型的反射实例
{
Assembly assembly = Assembly.Load("PatrickLiu.DelegateDeep.AllStudy");
var myType = assembly.GetType("PatrickLiu.DelegateDeep.AllStudy.Reflections.ReflectionGenericDemo`4");
if (myType != null)
{
var genericType = myType.MakeGenericType(new Type[] { typeof(int), typeof(string), typeof(float), typeof(decimal), });
var obj = Activator.CreateInstance(genericType);
}
}
//202309181309、实例方法反射调用
{
Assembly assembly = Assembly.Load("PatrickLiu.DelegateDeep.AllStudy");
var myType = assembly.GetType("PatrickLiu.DelegateDeep.AllStudy.Reflections.Methods.InstanceMethod");
if (myType != null)
{
var myInstance = Activator.CreateInstance(myType);
if (myInstance != null)
{
var method = myType.GetMethod("Process");
method!.Invoke(myInstance, null);
var method2 = myType.GetMethod("DoSomething", new Type[] { typeof(int), typeof(string), typeof(DateTime) });
method2!.Invoke(myInstance, new object[] { 112233, "魏延", DateTime.Now });
}
}
}
//202309181330、实例重载方法反射调用
{
Assembly assembly = Assembly.Load("PatrickLiu.DelegateDeep.AllStudy");
var myType = assembly.GetType("PatrickLiu.DelegateDeep.AllStudy.Reflections.Methods.InstanceMethod");
if (myType != null)
{
var myInstance = Activator.CreateInstance(myType);
if (myInstance != null)
{
var method = myType.GetMethod("Generate", new Type[] { });
method!.Invoke(myInstance, null);
var method2 = myType.GetMethod("Generate", new Type[] { typeof(int), typeof(string) });
method2!.Invoke(myInstance, new object[] { 112233, "魏延" });
var method3 = myType.GetMethod("Generate", new Type[] { typeof(int), typeof(string), typeof(Stream) });
method3!.Invoke(myInstance, new object[] { 112233, "魏延", null });
var method4 = myType.GetMethod("Generate", new Type[] { typeof(int), typeof(string), typeof(Stream), typeof(decimal) });
var value = method4!.Invoke(myInstance, new object[] { 112233, "魏延", null, 4933m });
Console.WriteLine(value);
}
}
}
//202309181345、静态方法、重载方法反射调用(实例对象为空)
{
Assembly assembly = Assembly.Load("PatrickLiu.DelegateDeep.AllStudy");
var myType = assembly.GetType("PatrickLiu.DelegateDeep.AllStudy.Reflections.Methods.StaticMethod");
if (myType != null)
{
var staticProcess = myType.GetMethod("Process");
staticProcess!.Invoke(null, null);
var staticDoSomething = myType.GetMethod("DoSomething", new Type[] { typeof(int), typeof(string), typeof(DateTime) });
staticDoSomething!.Invoke(null, new object[] { 123, "dddd", DateTime.Now });
var staticGenerate = myType.GetMethod("Generate", new Type[] { });
staticGenerate!.Invoke(null, null);
var staticGenerate2 = myType.GetMethod("Generate", new Type[] { typeof(int), typeof(string) });
staticGenerate2!.Invoke(null, new object[] { 123, "123" });
var staticGenerate3 = myType.GetMethod("Generate", new Type[] { typeof(int), typeof(string), typeof(Stream) });
staticGenerate3!.Invoke(null, new object[] { 123, "123", null });
var staticGenerate4 = myType.GetMethod("Generate", new Type[] { typeof(int), typeof(string), typeof(Stream), typeof(decimal) });
var value = staticGenerate4!.Invoke(null, new object[] { 123, "123", null, 2345m });
Console.WriteLine(value);
}
}
//202309181345、静态方法、重载方法反射调用(实例对象不为空)
{
Assembly assembly = Assembly.Load("PatrickLiu.DelegateDeep.AllStudy");
var myType = assembly.GetType("PatrickLiu.DelegateDeep.AllStudy.Reflections.Methods.StaticMethod");
if (myType != null)
{
var instance = Activator.CreateInstance(myType);
var staticProcess = myType.GetMethod("Process");
staticProcess!.Invoke(instance, null);
var staticDoSomething = myType.GetMethod("DoSomething", new Type[] { typeof(int), typeof(string), typeof(DateTime) });
staticDoSomething!.Invoke(instance, new object[] { 123, "dddd", DateTime.Now });
var staticGenerate = myType.GetMethod("Generate", new Type[] { });
staticGenerate!.Invoke(instance, null);
var staticGenerate2 = myType.GetMethod("Generate", new Type[] { typeof(int), typeof(string) });
staticGenerate2!.Invoke(instance, new object[] { 123, "123" });
var staticGenerate3 = myType.GetMethod("Generate", new Type[] { typeof(int), typeof(string), typeof(Stream) });
staticGenerate3!.Invoke(instance, new object[] { 123, "123", null });
var staticGenerate4 = myType.GetMethod("Generate", new Type[] { typeof(int), typeof(string), typeof(Stream), typeof(decimal) });
var value = staticGenerate4!.Invoke(instance, new object[] { 123, "123", null, 2345m });
Console.WriteLine(value);
}
}
//202309181349、泛型方法反射调用
{
//非泛型类型的泛型方法反射调用。
{
Assembly assembly = Assembly.Load("PatrickLiu.DelegateDeep.AllStudy");
var type = assembly.GetType("PatrickLiu.DelegateDeep.AllStudy.Reflections.Methods.InstanceGenericMethod");
if (type != null)
{
var instance = Activator.CreateInstance(type);
if (instance != null)
{
var method = type.GetMethod("Generate");
var genericMethod = method!.MakeGenericMethod(typeof(int), typeof(string));
var value = genericMethod.Invoke(instance, new object[] { 123, "魏延" });
Console.WriteLine(value);
}
}
}
//泛型类型的泛型方法反射调用。
{
Assembly assembly = Assembly.Load("PatrickLiu.DelegateDeep.AllStudy");
var type = assembly.GetType("PatrickLiu.DelegateDeep.AllStudy.Reflections.Methods.GenericClassMethod`1");
if (type != null)
{
var genericType = type.MakeGenericType(new Type[] { typeof(int) });
var instance = Activator.CreateInstance(genericType);
if (instance != null)
{
var method = genericType.GetMethod("Generate");
var genericMethod = method!.MakeGenericMethod(typeof(int), typeof(string));
var value = genericMethod.Invoke(instance, new object[] { 123, 456, "魏延" });
Console.WriteLine(value);
}
}
}
}
//202309191105、反射调用静态和实例私有方法
{
Assembly assembly = Assembly.Load("PatrickLiu.DelegateDeep.AllStudy");
var type = assembly.GetType("PatrickLiu.DelegateDeep.AllStudy.Reflections.Methods.PrivateMethodDemo");
if (type != null)
{
var instance = Activator.CreateInstance(type);
if (instance != null)
{
var privateInstanceMethod = type.GetMethod("PrivateInstanceMethodTest", BindingFlags.NonPublic|BindingFlags.Instance);
privateInstanceMethod?.Invoke(instance,new object[] {123,"魏文长" });
var privateStaticMethod = type.GetMethod("PrivateStaticMethodTest", BindingFlags.NonPublic | BindingFlags.Static);
privateStaticMethod?.Invoke(instance, new object[] { 123, "魏文长" });
}
}
}
三、总结
好了,写了不少了,其实这里不只是包含泛型类型,也包含其他的,私有方法的调用,静态方法的调用,重载方法和构造函数的调用,基本上都涉及到了,行了,以后有新的东西再写吧。每天努力一点点,老天不负有心人。