当前位置:网站首页>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
边栏推荐
- Chondroitin sulfate in vitreous
- Deeply understand what new and make in golang are and what are the differences?
- 机器学习理论之(7):核函数 Kernels —— 一种帮助 SVM 实现非线性化决策边界的方式
- Esp32 (UART receiving and sending) - receiving and sending communication of serial port (4)
- Query the logistics update quantity according to the express order number
- Resolution: cnpm: unable to load file \cnpm. PS1, because running scripts is prohibited on this system
- 7、 DOM (Part 2) - chapter after class exercises and answers
- CISSP certified daily knowledge points (April 15, 2022)
- ESP32 LVGL8. 1 - input devices (input devices 18)
- Daily CISSP certification common mistakes (April 13, 2022)
猜你喜欢
ESP32 LVGL8. 1 - slider slider (slider 22)
Practice of Druid SQL and security in meituan review
玻璃体中的硫酸软骨素
ESP32 LVGL8. 1 - checkbox (checkbox 23)
Machine learning theory (8): model integration ensemble learning
Query the logistics update quantity according to the express order number
Esp32 (UART 485 communication) - 485 communication of serial port (3)
Summary of actual business optimization scheme - main directory - continuous update
After opening the original normal project, the dependency package displays red and does not exist.
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
随机推荐
Summary of actual business optimization scheme - main directory - continuous update
Daily network security certification test questions (April 14, 2022)
配置iptables
Esp32 (UART ecoh) - serial port echo worm learning (2)
Use Chenxi bookkeeping book to analyze the balance of revenue and expenditure of each account in a certain period of time
Machine learning theory (8): model integration ensemble learning
The corresponding permissions required to automatically open the app in the setting interface through accessibility service
K210串口通信
Scrollto and scrollby
QT error: no matching member function for call to ‘connect‘
Feature selection feature_ selection--SelectKBest
Usage of functions decode() and replace() in SQL
特征选择feature_selection--SelectKBest
ctfshow-web361(SSTI)
Introduction to quantexa CDI syneo platform
Introduction to ROS learning notes (I)
Loop path
玻璃体中的硫酸软骨素
PyGame tank battle
机器学习理论之(8):模型集成 Ensemble Learning