当前位置:网站首页>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
边栏推荐
- ESP32 LVGL8. 1 - roller rolling (roller 24)
- 使用晨曦记账本,分析某个时间段每个账户收支结余
- [mathematical modeling] - analytic hierarchy process (AHP)
- CISSP certified daily knowledge points (April 14, 2022)
- STM32: LCD显示
- ESP32 LVGL8. 1 - img picture (IMG 20)
- Halo open source project learning (VII): caching mechanism
- Daily network security certification test questions (April 14, 2022)
- Esp32 drive encoder -- siq-02fvs3 (vscade + IDF)
- Recyclerview control list item layout match_ Fundamental principle of parent attribute invalidation
猜你喜欢
随机推荐
Esp32 drive encoder -- siq-02fvs3 (vscade + IDF)
[popular science] CRC verification (I) what is CRC verification?
The type initializer for ‘Gdip‘ threw an exception
Go 语言 GUI 框架 fyne 中文乱码或者不显示的问题
iptables初探
ESP32 LVGL8. 1 - calendar (calendar 25)
WebView saves the last browsing location
Download xshell 6 and xftp6 official websites
玻璃体中的硫酸软骨素
简化路径(力扣71)
Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
Nacos作为服务配置中心实战
Database computer experiment 4 (data integrity and stored procedure)
Methods of nested recycleview to solve sliding conflict and incomplete item display
Domestic GD chip can filter
Daily network security certification test questions (April 15, 2022)
Daily CISSP certification common mistakes (April 19, 2022)
QT curve / oscilloscope customplot control
#yyds干货盘点#stringprep --- 因特网字符串预备
MVVM模型