当前位置:网站首页>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
边栏推荐
- CANopen STM32 transplantation
- One of the reasons why the WebView web page cannot be opened (and some WebView problem records encountered by myself)
- Dynamically add and delete layouts
- How to virtualize the video frame and background is realized in a few simple steps
- Practice of Druid SQL and security in meituan review
- ESP32 LVGL8. 1 - anim animation (anim 16)
- ESP32 LVGL8. 1 - calendar (calendar 25)
- ctfshow-web362(SSTI)
- MVVM model
- Tangle
猜你喜欢
Esp32 drive encoder -- siq-02fvs3 (vscade + IDF)
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
ESP32 LVGL8. 1 - checkbox (checkbox 23)
os_authent_prefix
ctfshow-web361(SSTI)
kettle庖丁解牛第17篇之文本文件输出
解决:cnpm : 無法加載文件 ...\cnpm.ps1,因為在此系統上禁止運行脚本
Eight bit binary multiplier VHDL
Esp32 (UART ecoh) - serial port echo worm learning (2)
ESP32 LVGL8. 1 - label (style 14)
随机推荐
22年字节跳动飞书人力套件三面面经
教你用简单几个步骤快速重命名文件夹名
How to virtualize the video frame and background is realized in a few simple steps
Deeply understand what new and make in golang are and what are the differences?
Esp32 (UART ecoh) - serial port echo worm learning (2)
数据库上机实验四(数据完整性与存储过程)
根据快递单号查询物流查询更新量
Simple use of viewbinding
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
纠结
ESP32 LVGL8. 1 - arc (arc 19)
【科普】CRC校验(一)什么是CRC校验?
Sentinel rule persistence into Nacos
ctfshow-web362(SSTI)
WebView opens H5 video and displays gray background or black triangle button. Problem solved
Get a list of recent apps
Screenshot using projectmediamanager
Ionic instruction set order from creation to packaging
Click the input box to pop up the keyboard layout and move up
After opening the original normal project, the dependency package displays red and does not exist.