当前位置:网站首页>c#:泛型反射
c#:泛型反射
2022-04-23 18:47:00 【jackletter】
1. 泛型类是类的模板(运行时一般需要具化才能使用)
泛型类是类的模板,它不能被当做正常类来使用,比如说:不能创建实例。
如下:
public class Person<T>
{
public string Name {
get; set; }
}
这个Person<T>
是类的模板,它都不是正常的类,所以,不能创建实例,看下面:
比较迷惑的是:在写代码的时候也不区分它是不是泛型类啊,比如:
这个NewInst<T>
方法也没有报过错啊。
其实,NewInst<T>
方法之所以不报错,是因为在程序运行调用的时候,那个T就已经确定了,回想下,之前调用的代码是不是这样的:
就是说无论方法嵌套了几层,最终执行的时候T
肯定是已经确定了。
再看问题: Person<Fu>
和Person<Zi>
它们之间有继承或实现关系吗?可以将 Person<Fu>
赋值给 Person<Zi>
吗?
首先,Person<Fu>
和Person<Zi>
它们没有继承或实现关系,它们除了是从一个泛型模板具化来的,没有其他联系。
另外,一般情况下不能将Person<Fu>
赋值给或接受自Person<Zi>
的变量,但我们运用逆变和协变的时候或许可以,参照:《c#: 协变和逆变深度解析》。
因为Person<Fu>
和Person<Zi>
不是同一个类,它们拥有不同的Type实例,所以下面的用法会导致意想不到的效果:
2. 泛型方法是方法的模板(运行时一般需要具化才能使用)
和泛型类一样,泛型方法也不能直接使用,如:
如果想运行,比如将T具化才行,如下:
同泛型类一样,无论我们的方法嵌套几层,运行到这个方法的时候,肯定是已经具化了的。
3. 泛型类的特别应用场景
泛型类是模板类,它不能当做正常类实例化调用,但是它在写框架的时候非常有用,比如:
在asp.net core中注入时:
4. 如何反射和具化一个泛型类、泛型方法
public class Class2
{
public static void Main()
{
//反射泛型类
var type = typeof(Person<>);
Console.WriteLine($"type.IsGenericType={
type.IsGenericType}");
Console.WriteLine($"type.Name={
type.Name}");
//泛型类的具化
var type2 = type.MakeGenericType(typeof(int));
var personInt = Activator.CreateInstance(type2);
//反射泛型方法
var method = typeof(Class2).GetMethod("Test", new[] {
typeof(IEnumerable<>).MakeGenericType(Type.MakeGenericMethodParameter(0)), Type.MakeGenericMethodParameter(0) });
Console.WriteLine($"method.IsGenericType={
method.IsGenericMethod}");
Console.WriteLine($"method.Name={
method.Name}");
//泛型方法的具化
var method2 = method.MakeGenericMethod(typeof(int));
method2.Invoke(null, new object[] {
new List<int>(), 1 });
Console.ReadLine();
}
public static void Test<T>(IEnumerable<T> list, T t) => Console.WriteLine($"t={
t},list.Count={
list.Count()}");
public static void Test<T>() => Console.WriteLine("ok");
}
public class Person<T> {
}
运行效果:
版权声明
本文为[jackletter]所创,转载请带上原文链接,感谢
https://blog.csdn.net/u010476739/article/details/124257320
边栏推荐
- Daily CISSP certification common mistakes (April 19, 2022)
- ESP32 LVGL8. 1 - bar progress bar (bar 21)
- How can programmers quickly develop high-quality code?
- Druid SQL和Security在美团点评的实践
- 7、 DOM (Part 2) - chapter after class exercises and answers
- 机器学习理论基础篇--关于机器学习的一些术语
- 22 year flying Book manpower Kit
- Esp32 (UART 485 communication) - 485 communication of serial port (3)
- Daily network security certification test questions (April 13, 2022)
- Introduction to quantexa CDI syneo platform
猜你喜欢
ESP32 LVGL8. 1 - arc (arc 19)
使用晨曦记账本,分析某个时间段每个账户收支结余
STM32: LCD显示
ctfshow-web361(SSTI)
Setting up keil environment of GD single chip microcomputer
Esp32 (UART event) - serial port event learning (1)
ESP32 LVGL8. 1 - textarea text area (textarea 26)
Tangle
How to virtualize the video frame and background is realized in a few simple steps
ESP32 LVGL8. 1 - slider slider (slider 22)
随机推荐
Promote QT default control to custom control
昇腾 AI 开发者创享日全国巡回首站在西安成功举行
Resolution: cnpm: unable to load file \cnpm. PS1, because running scripts is prohibited on this system
WebView opens H5 video and displays gray background or black triangle button. Problem solved
关于unity文件读取的操作(一)
The corresponding permissions required to automatically open the app in the setting interface through accessibility service
How can programmers quickly develop high-quality code?
Introduction to QT programming
从技术体系到商业洞察,中小研发团队架构实践之收尾篇
CISSP certified daily knowledge points (April 11, 2022)
机器学习理论基础篇--关于机器学习的一些术语
Sentinel服务熔断实战(sentinel整合ribbon+openFeign+fallback)
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
22 year flying Book manpower Kit
Nacos集群搭建和mysql持久化配置
Advanced transfer learning
深入理解 Golang 中的 new 和 make 是什么, 差异在哪?
Sentinel rule persistence into Nacos
Setting up keil environment of GD single chip microcomputer
iptables -L执行缓慢