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适用于需要动态获取类型和构造函数的情况,更加灵活。