反射泛型

1.使用反射替换类型中方法的泛型 

//获取Apple<T>类型,`1作为泛型参数数目

 var type = Type.GetType("UnitTest.Apple`1");
//将Apple<T>中的泛型替换成指定的类型C
 var appleType = type.MakeGenericType(new[] { Type.GetType("UnitTest.C") });
 //生成Apple<T>实例
dynamic apple = Activator.CreateInstance(appleType);
 //调用GetInfo方法

 var list = apple.GetInfo();


namespace UnitTest
{

public class Apple<T> where T : new()
    {
        public List<T> GetInfo() 
        {
            List<T> list = new List<T>();
            T t1 = new T();
            list.Add(t1);
            return list;
        }
    }

}


2.替换类型中的泛型

var listType = typeof(List<>);
var listofType = listType.MakeGenericType(new[] { Type.GetType("UnitTest.C") });
dynamic list = Activator.CreateInstance(listofType);
list.Add(new C() { i1=1 });
list.Add(new C() { i1 = 2 });
var list1 = list;

<think>我们正在处理用户关于C#反射的问题。用户请求一个示例,展示如何在C#中使用反射处理。 根据引用[1]:C# also has extensive support for querying generic information at runtime via System.Reflection, such as with System.Type.GetGenericArguments(). 引用[3]展示了一个协变接口的例子,但我们的重点是反射。 我们将通过一个示例来演示: 1. 获取的类定义(开放)和封闭。 2. 使用反射创建的实例。 3. 调用方法。 示例步骤: 步骤1:定义一个类和一个方法。 步骤2:使用反射获取并创建封闭。 步骤3:创建实例并调用方法。 注意:在C#中,反射操作时,通常需要先获取开放(未指定类参数的类),然后通过MakeGenericType方法传入类参数来构造封闭。 同样,对于方法,使用MakeGenericMethod来指定方法上的类参数。 下面是一个具体的示例代码:</think>### C# 反射处理示例 在 C# 中使用反射处理时,核心是通过 `System.Reflection` 获取信息,再通过 `MakeGenericType`/`MakeGenericMethod` 构造具体类或方法。以下是关键操作示例: #### 1. **创建实例** ```csharp // 获取开放 List<> Type openType = typeof(List<>); // 指定类参数并创建封闭类 Type closedType = openType.MakeGenericType(typeof(string)); // 创建实例 object listInstance = Activator.CreateInstance(closedType); // 添加元素(通过反射调用Add方法) MethodInfo addMethod = closedType.GetMethod("Add"); addMethod.Invoke(listInstance, new object[] { "Hello" }); Console.WriteLine(((IList)listInstance)[0]); // 输出: Hello ``` #### 2. **调用方法** ```csharp public class Processor { public T Process<T>(T input) => input; } // 反射调用方法 Processor processor = new Processor(); MethodInfo openMethod = typeof(Processor).GetMethod("Process"); // 指定方法类参数 MethodInfo closedMethod = openMethod.MakeGenericMethod(typeof(int)); // 执行方法 int result = (int)closedMethod.Invoke(processor, new object[] { 42 }); Console.WriteLine(result); // 输出: 42 ``` #### 3. **检查信息** ```csharp Type genericType = typeof(Dictionary<,>); // 获取参数 Type[] typeArgs = genericType.GetGenericArguments(); Console.WriteLine($"参数数量: {typeArgs.Length}"); // 输出: 2 Console.WriteLine($"参数1: {typeArgs[0].Name}"); // 输出: TKey ``` #### 4. **处理嵌套** ```csharp // 构造 Dictionary<string, List<int>> 类 Type dictOpenType = typeof(Dictionary<,>); Type listClosedType = typeof(List<int>); Type nestedType = dictOpenType.MakeGenericType(typeof(string), listClosedType); // 创建实例 object nestedInstance = Activator.CreateInstance(nestedType); ``` ### 关键反射类 - `Type.GetGenericArguments()`:获取参数[^1] - `Type.MakeGenericType()`:构造封闭 - `MethodInfo.MakeGenericMethod()`:构造封闭方法 - `Activator.CreateInstance()`:创建实例 > 注意:处理接口时需考虑协变/逆变,如 `IReadOnlyList<out T>` 的协变支持[^3]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值