当前位置:网站首页>C: generic reflection
C: generic reflection
2022-04-23 18:49:00 【jackletter】
1. Generic classes are templates for classes ( The runtime generally needs to be specific to use )
Generic classes are templates for classes , It cannot be used as a normal class , for instance : Cannot create instance .
as follows :
public class Person<T>
{
public string Name {
get; set; }
}
This Person<T>
It's a template for the class , It's not a normal class , therefore , Cannot create instance , Look below :
What's confusing is : When writing code, it doesn't distinguish whether it is a generic class or not , such as :
This NewInst<T>
There is no fault in the method .
Actually ,NewInst<T>
The reason why the method does not report an error , Because when the program is called , that T It's all set , In retrospect , Top note: is the code that was called before this? :
That is, no matter how many layers the method is nested , At the end of the day T
It must have been determined .
Look again : Person<Fu>
and Person<Zi>
Are there inheritance or implementation relationships between them ? Can be Person<Fu>
Assign a value to Person<Zi>
Do you ?
First ,Person<Fu>
and Person<Zi>
They have no inheritance or implementation relationships , Except that they are materialized from a generic template , No other contact .
in addition , Under normal circumstances, the Person<Fu>
Assign to or accept from Person<Zi>
The variable of , But we may be able to use inversion and covariance , reference :《c#: Covariant and inverse depth analysis 》.
because Person<Fu>
and Person<Zi>
Not the same class , They have different Type example , So the following usage will lead to unexpected results :
2. Generic methods are templates for methods ( The runtime generally needs to be specific to use )
Like generic classes , Generic methods cannot be used directly , Such as :
If you want to run , For example, will T Materialization is the only way , as follows :
Same as generic class , No matter how many layers our method is nested , When you run this method , It must have been embodied .
3. Special application scenarios of generic classes
A generic class is a template class , It cannot be instantiated and called as a normal class , But it is very useful when writing frameworks , such as :
stay asp.net core When you inject it :
4. How to reflect and materialize a generic class 、 Generic methods
public class Class2
{
public static void Main()
{
// Reflection generic class
var type = typeof(Person<>);
Console.WriteLine($"type.IsGenericType={
type.IsGenericType}");
Console.WriteLine($"type.Name={
type.Name}");
// Materialization of generic classes
var type2 = type.MakeGenericType(typeof(int));
var personInt = Activator.CreateInstance(type2);
// Reflection generic method
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}");
// Materialization of generic methods
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> {
}
Running effect :
版权声明
本文为[jackletter]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231847116154.html
边栏推荐
- 12个例子夯实promise基础
- 根据快递单号查询物流查询更新量
- ESP32 LVGL8. 1 - checkbox (checkbox 23)
- Daily network security certification test questions (April 18, 2022)
- Machine learning theory (7): kernel function kernels -- a way to help SVM realize nonlinear decision boundary
- kettle庖丁解牛第17篇之文本文件输出
- 迁移学习进阶
- Feature selection feature_ selection--SelectKBest
- 【数学建模】—— 层次分析法(AHP)
- Teach you to quickly rename folder names in a few simple steps
猜你喜欢
根据快递单号查询物流查询更新量
实战业务优化方案总结---主目录---持续更新
ESP32 LVGL8. 1 - input devices (input devices 18)
ctfshow-web361(SSTI)
ESP32 LVGL8. 1 - arc (arc 19)
Introduction to ROS learning notes (II)
After opening the original normal project, the dependency package displays red and does not exist.
ctfshow-web362(SSTI)
WebView opens H5 video and displays gray background or black triangle button. Problem solved
【科普】CRC校验(一)什么是CRC校验?
随机推荐
ESP32 LVGL8. 1 - checkbox (checkbox 23)
中金财富怎么样?在上边开户安全吗
机器学习理论之(8):模型集成 Ensemble Learning
玻璃体中的硫酸软骨素
【科普】CRC校验(一)什么是CRC校验?
CISSP certified daily knowledge points (April 14, 2022)
CISSP certified daily knowledge points (April 19, 2022)
22 year flying Book manpower Kit
Druid SQL和Security在美团点评的实践
Seata处理分布式事务
Introduction to ROS learning notes (II)
Introduction to ROS learning notes (I)
22年字节跳动飞书人力套件三面面经
ESP32 LVGL8. 1 - roller rolling (roller 24)
QT error: no matching member function for call to ‘connect‘
Nacos cluster construction and MySQL persistence configuration
ESP32 LVGL8. 1 - input devices (input devices 18)
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
Daily network security certification test questions (April 18, 2022)
Database computer experiment 4 (data integrity and stored procedure)