南飓风 发表于 2024-6-21 13:22:18

使用C#反射中的MAKEGENERICTYPE函数,来为泛型方法和泛型类指定(泛型的)

MakeGenericType 是一个在 C# 中用于创建开放范例的实例的方法。开放范例是一种未绑定范例参数的泛型范例。当你有一个泛型范例定义,并且想要用特定的范例实例化它时,你可以使用 MakeGenericType 方法。
public Type MakeGenericType (params Type[] typeArguments); 这个方法担当一个 Type[] 作为参数,此中包罗了用来更换泛型范例定义中的范例参数的范例。
比方,假设你有一个泛型类 Pair<T, U>,你想要创建一个 Pair<int, string> 的实例。你可以这样做:
// 泛型类型定义
public class Pair<T, U> {
    public T First { get; set; }
    public U Second { get; set; }
}

// 创建泛型类型的实例
Type pairType = typeof(Pair<,>); // 获取开放类型
Type[] typeArguments = { typeof(int), typeof(string) }; // 实例化类型参数
Type pairInstanceType = pairType.MakeGenericType(typeArguments); // 创建实例类型

// 创建实例
object pairInstance = Activator.CreateInstance(pairInstanceType); 在这个例子中,pairType 是一个开放范例,typeArguments 是用来更换 T 和 U 的详细范例。pairInstanceType 是一个已经绑定了详细范例参数的 Pair<int, string> 范例。最后,我们使用 Activator.CreateInstance 来创建这个范例的实例。

C#反射中的MakeGenericType函数可以用来指定泛型方法和泛型类的详细范例,方法如下面代码所示这里就不多讲了,详情看下面代码一切就清楚了:
using System;
using System.Reflection;

namespace RFTest
{
    //类ReflectionTest中定义了一个泛型函数DisplayType和泛型类MyGenericClass
    class ReflectionTest
    {
      //泛型类MyGenericClass有个静态函数DisplayNestedType
      public class MyGenericClass<T>
      {
            public static void DisplayNestedType()
            {
                Console.WriteLine(typeof(T).ToString());
            }
      }

      public void DisplayType<T>()
      {
            Console.WriteLine(typeof(T).ToString());
      }
    }

    class Program
    {
      static void Main(string[] args)
      {
            ReflectionTest rt = new ReflectionTest();

            MethodInfo mi = rt.GetType().GetMethod("DisplayType");//先获取到DisplayType<T>的MethodInfo反射对象
            mi.MakeGenericMethod(new Type[] { typeof(string) }).Invoke(rt, null);//然后使用MethodInfo反射对象调用ReflectionTest类的DisplayType<T>方法,这时要使用MethodInfo的MakeGenericMethod函数指定函数DisplayType<T>的泛型类型T

            Type myGenericClassType = rt.GetType().GetNestedType("MyGenericClass`1");//这里获取MyGenericClass<T>的Type对象,注意GetNestedType方法的参数要用MyGenericClass`1这种格式才能获得MyGenericClass<T>的Type对象
            myGenericClassType.MakeGenericType(new Type[] { typeof(float) }).GetMethod("DisplayNestedType", BindingFlags.Static | BindingFlags.Public).Invoke(null, null);
            //然后用Type对象的MakeGenericType函数为泛型类MyGenericClass<T>指定泛型T的类型,比如上面我们就用MakeGenericType函数将MyGenericClass<T>指定为了MyGenericClass<float>,然后继续用反射调用MyGenericClass<T>的DisplayNestedType静态方法

            Console.ReadLine();
      }
    }
}

C# 反射



反射是一种在运行时动态获取程序范例信息的技术,它可以用来查找和操纵程序中的范例、成员、属性和方法等。
(1)获取Type范例的几种方法:
(a)  实例调用GetType
(b)  typeof(范例)
(c)  Assembly.GetType(范例名称)
(d)  Type.GetType(范例全称)
(2)获取数组范例
typeof(范例).MakeArrayType()
如typeof(int).MakeArrayType()==typeof(int[]) //为true
(3)根据数组范例返回元素范例
typeof(int[]).GetElementType()==typeof(int)//为true
(4)范例具有Namespace,Name,FullName属性,FullName基本即是前两者组合在一起。
(5)数组,指针,ref,out 参数范例名称



MakeGenericType的使用:
MakeGenericType 方法用于创建一个泛型范例的实例,此中可以通过传递范例参数来指定详细的泛型参数范例。这在需要在运行时动态创建泛型范例的情况下非常有用。下面是一个示例代码演示怎样使用 MakeGenericType 方法:
假设有一个泛型类 MyGenericClass,你想要在运行时为其指定详细的范例参数并创建实例。首先,定义泛型类如下:

using System;

public class MyGenericClass<T>
{
    public void PrintType()
    {
      Console.WriteLine(typeof(T).Name);
    }
}

接下来,可以使用 MakeGenericType 方法来动态创建泛型范例的实例:

using System;

class Program
{
    static void Main(string[] args)
    {
      // 获取泛型类型的定义
      Type genericTypeDefinition = typeof(MyGenericClass<>);

      // 指定泛型类型参数
      Type[] typeArguments = { typeof(int) };

      // 使用MakeGenericType创建泛型类型实例
      Type specificType = genericTypeDefinition.MakeGenericType(typeArguments);
      object instance = Activator.CreateInstance(specificType);

      // 调用泛型类型的方法
      var printMethod = specificType.GetMethod("PrintType");
      printMethod.Invoke(instance, null);
    }
}



在这个示例中,首先获取了泛型范例的定义 MyGenericClass<>,然后指定了详细的泛型范例参数,比方 int。接着使用 MakeGenericType 创建了指定参数的泛型范例实例,并通过 Activator.CreateInstance 创建了实例对象。最后,使用反射调用了泛型范例的方法。

using System;
using System.Reflection;

class Program
{
    static void Main(string[] args)
    {
      // 通过反射查找类型
      Type type = Type.GetType("Demo.Person");

      // 通过反射创建对象
      object person = Activator.CreateInstance(type);

      // 通过反射调用方法
      MethodInfo methodInfo = type.GetMethod("SayHello");
      methodInfo.Invoke(person, null);

      // 通过反射获取属性
      PropertyInfo propertyInfo = type.GetProperty("Name");
      Console.WriteLine("Name: {0}", propertyInfo.GetValue(person));

      // 通过反射修改属性
      propertyInfo.SetValue(person, "Tom", null);
      Console.WriteLine("Name: {0}", propertyInfo.GetValue(person));

      // 通过反射获取字段
      FieldInfo fieldInfo = type.GetField("Age");
      Console.WriteLine("Age: {0}", fieldInfo.GetValue(person));

      // 通过反射修改字段
      fieldInfo.SetValue(person, 20);
      Console.WriteLine("Age: {0}", fieldInfo.GetValue(person));
    }
}

class Person
{
    public string Name { get; set; }
    public int Age;

    public void SayHello()
    {
      Console.WriteLine("Hello, my name is {0}.", Name);
    }
}


在这个示例中,我们通过反射查找了一个名为Demo.Person的范例,并创建了一个该范例的对象。然后,我们使用反射获取了该对象的SayHello方法,并通过Invoke方法调用了该方法。接着,我们使用反射获取了该对象的Name属性,并获取了该属性的值。然后,我们通过反射修改了该对象的Name属性的值,并再次获取了该属性的值。最后,我们使用反射获取了该对象的Age字段,并获取了该字段的值。然后,我们通过反射修改了该对象的Age字段的值,并再次获取了该字段的值。
·使用反射调用构造器,可以通过以下步调实现:
通过Type.GetType方法大概typeof关键字获取目标范例的Type对象。比方,获取Demo.Person范例的Type对象可以使用以下代码:Type type = Type.GetType("Demo.Person"); 大概 Type type = typeof(Demo.Person);




Activator.CreateInstance和constructor.Invoke都可以用于创建对象,但它们的实现方式有所差别。
Activator.CreateInstance是一个静态方法,它使用指定的范例名、程序集名、参数等信息来创建一个实例。它可以自动选择适当的构造函数进行创建,并且支持泛型范例的创建。使用Activator.CreateInstance可以避免手动获取构造函数的过程,让创建对象的过程更加简便。但是,由于其通过字符串来指定范例名和程序集名,因此需要在编译时指定完整的范例名和程序集名,不太方便动态获取范例。
constructor.Invoke则是使用反射获取到一个构造函数后,通过Invoke方法来调用构造函数,创建一个对象。与Activator.CreateInstance相比,使用constructor.Invoke需要手动获取构造函数,需要明确指定构造函数的参数,因此相对来说更加复杂。但是,它可以在运行时动态获取范例和构造函数,更加灵活。
总的来说,Activator.CreateInstance实用于已知范例名和程序集名的情况,可以让创建对象更加简便;而constructor.Invoke实用于需要动态获取范例和构造函数的情况,更加灵活。









免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 使用C#反射中的MAKEGENERICTYPE函数,来为泛型方法和泛型类指定(泛型的)