当前位置:网站首页>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
边栏推荐
- CISSP certified daily knowledge points (April 15, 2022)
- Machine learning theory (8): model integration ensemble learning
- Use of content provider
- 昇腾 AI 开发者创享日全国巡回首站在西安成功举行
- ESP32 LVGL8. 1 - roller rolling (roller 24)
- CISSP certified daily knowledge points (April 19, 2022)
- Esp32 (UART ecoh) - serial port echo worm learning (2)
- CISSP certified daily knowledge points (April 13, 2022)
- Go 语言 GUI 框架 fyne 中文乱码或者不显示的问题
- Résolution: cnpm: impossible de charger le fichier... Cnpm. PS1 parce que l'exécution de scripts est désactivée sur ce système
猜你喜欢

Machine learning theory (8): model integration ensemble learning

视频边框背景如何虚化,简单操作几步实现

实战业务优化方案总结---主目录---持续更新

解决:cnpm : 无法加载文件 ...\cnpm.ps1,因为在此系统上禁止运行脚本

ESP32 LVGL8. 1 - checkbox (checkbox 23)

kettle庖丁解牛第17篇之文本文件输出

Chondroitin sulfate in vitreous

Query the logistics update quantity according to the express order number

Druid SQL和Security在美团点评的实践

ESP32 LVGL8. 1 - textarea text area (textarea 26)
随机推荐
With the use of qchart, the final UI interface can be realized. The control of qweight can be added and promoted to a user-defined class. Only the class needs to be promoted to realize the coordinate
Configure iptables
WebView opens H5 video and displays gray background or black triangle button. Problem solved
根据快递单号查询物流查询更新量
Setting up keil environment of GD single chip microcomputer
ESP32 LVGL8. 1 - slider slider (slider 22)
ESP32 LVGL8. 1 - anim animation (anim 16)
Druid SQL和Security在美团点评的实践
深入理解 Golang 中的 new 和 make 是什么, 差异在哪?
Fundamentals of machine learning theory -- some terms about machine learning
WebView saves the last browsing location
Tangle
Ctfshow - web362 (ssti)
7、 DOM (Part 2) - chapter after class exercises and answers
CISSP certified daily knowledge points (April 15, 2022)
ESP32 LVGL8. 1 - bar progress bar (bar 21)
Excel intercept text
七、DOM(下) - 章节课后练习题及答案
ESP32 LVGL8. 1 - BTN button (BTN 15)
昇腾 AI 开发者创享日全国巡回首站在西安成功举行