当前位置:网站首页>C reflection and type
C reflection and type
2022-07-05 23:42:00 【Tingyi -- flying bird】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace reflection
{
#region Knowledge review
// Source language program : Written in a programming language , such as C、C++、Java Wait for the program written in language
// Target language program : A program written in pseudo machine code represented by binary numbers
#endregion
#region Assembly
// Assembly It's a code set we wrote
// For example, a code base file (dll) perhaps An executable file (exe)
#endregion
#region Metadata
// Metadata It's the data that describes the data
// Classes in programs , Functions in class 、 Variables and other information are procedural Metadata
// Data about programs and types is called Metadata , They are saved in the program set .
#endregion
#region The concept of reflection
// A running program looks at itself or other programs Behavior of metadata It's called reflection .
// When the program is running , Through reflection, you can get all kinds of information about other assemblies or your own assembly code .
// class 、 function 、 Variable 、 Objects, etc. , Instantiate them 、 Execute them 、 Operate them .
#endregion
#region The effect of reflection
// Because reflection can be in After compiling the program Get information 、 So it improves the expansibility and flexibility of the program .
// 1. Program runtime , Get all metadata 、 Features including metadata
// 2. Program runtime , Instantiate objects 、 Action object
// 3. Program runtime , You can create new objects , Use these objects to perform tasks .
#endregion
class Test
{
private int i = 1;
public int j = 0;
public string str = "123";
public Test()
{
}
public Test(int i)
{
this.i = i;
}
public Test(int i, string str):this(i)
{
this.str = str;
}
public void Speek()
{
System.Console.WriteLine(i.ToString());
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" Reflection ");
// Related to the grammar
#region Type
// type Class information class
// It is the foundation of reflection function .
// It is the main way to access metadata .
// Use Type Get information about the type declaration
// About members of types ( Such as constructor 、 Method 、 Field 、 Events of properties and classes )
#endregion
#region obtain Type
// 1.Object in obtain GetType() You can get the name of the object Type
int a = 38;
Type typeA = a.GetType();
Console.WriteLine(" Variable a The type of :" + typeA );
// 2. adopt typeof Keyword acquisition Pass in the class name
Type typeB = typeof(int);
Console.WriteLine("typeof Access to type :" + typeB);
// 3. By the name of the class obtain Type
// Note that the namespace must be included 、 Or we can't find it .
Type typeName = Type.GetType("System.Int32");
Console.WriteLine(" Class name Access to type :" + typeName);
#endregion
#region Get assembly information
Console.WriteLine(typeA.Assembly);
Console.WriteLine(typeB.Assembly);
Console.WriteLine(typeName.Assembly);
#endregion
#region Get all public members in the class
// First get Type
Type tTest = typeof(Test);
// Get all the public members
// You need to reference a namespace using System.Reflection;
MemberInfo[] infos = tTest.GetMembers();
foreach (MemberInfo item in infos)
{
Console.WriteLine(item);
}
#endregion
#region Get... In class Public constructor and call
// 1. Get all constructors
ConstructorInfo[] ctors = tTest.GetConstructors();
foreach (ConstructorInfo item in ctors)
{
Console.WriteLine(item);
}
// 2. Get one of the constructors And implement
// 2-1. No arguments structure
ConstructorInfo ctorInfo = tTest.GetConstructor(new Type[0]); // Pass in Type Array
// Perform a parameterless construct
Test obj = ctorInfo.Invoke(null) as Test; // No arguments structure
Console.WriteLine(obj.j);
// 2-2. Get the parametric structure
ConstructorInfo ctorInfo2 = tTest.GetConstructor(new Type[] {
typeof(int)});
// Perform a parameterless construct
Test obj1 = ctorInfo2.Invoke(new object[] {
2 }) as Test; // No arguments structure
obj1.Speek();
#endregion
#region Get... In class Member variables
// 1. Get all member variables
FieldInfo[] fieldInfos = tTest.GetFields();
foreach (FieldInfo item in fieldInfos)
{
Console.WriteLine(item);
}
// 2. Get the public member variable with the specified name
FieldInfo fieldJ = tTest.GetField("j");
Console.WriteLine(fieldJ);
// 3. adopt Member variables get the value of the object
Test test = new Test();
test.j = 99;
test.str = "3333";
// 3-3 By reflection Get the value of the member variable of the object
Console.WriteLine(fieldJ.GetValue(test));
// 3-4 By reflection Set the value of the object's member variable
fieldJ.SetValue(test, 101);
Console.WriteLine(fieldJ.GetValue(test));
#endregion
#region Get... In class Method
// 1. Get all methods
Type strType = typeof(string);
MethodInfo[] methodInfos = strType.GetMethods();
foreach (MethodInfo item in methodInfos)
{
Console.WriteLine(item);
}
// 2. Get one of the methods
MethodInfo strSub = strType.GetMethod("Substring",
new Type[] {
typeof(int), typeof(int)});
// 3. Use reflection Calling method
string strHl = "Hello world!";
// If it's a static method The first parameter Set to null
object result = strSub.Invoke(strHl, new object[] {
7, 5 });
Console.WriteLine(result);
#endregion
Console.ReadLine();
}
}
}
边栏推荐
- Redis高可用——主从复制、哨兵模式、集群
- golang代码检查工具
- In C#, why can't I modify the member of a value type instance in a foreach loop?
- 98. Verify the binary search tree ●●
- Part III Verilog enterprise real topic of "Niuke brush Verilog"
- CIS基准测试工具kube-bench使用
- MySQL delete uniqueness constraint unique
- 带外和带内的区别
- 动态规划 之 打家劫舍
- Live tiktok shop 2022 latest gameplay card slot overseas live e-commerce new traffic
猜你喜欢
21.PWM应用编程
Spire Office 7.5.4 for NET
Part III Verilog enterprise real topic of "Niuke brush Verilog"
4 points tell you the advantages of the combination of real-time chat and chat robots
Breadth first search open turntable lock
CIS benchmark tool Kube bench
Zero rhino technology joined hands with the intelligence Club: the "causal faction" forum was successfully held, and the "causal revolution" brought the next generation of trusted AI
【LeetCode】5. Valid Palindrome·有效回文
Brushless drive design -- on MOS drive circuit
同事悄悄告诉我,飞书通知还能这样玩
随机推荐
[Yu Yue education] NC machining technology reference materials of Shaanxi University of science and technology
TVS管和ESD管的技術指標和選型指南-嘉立創推薦
Rethinking about MySQL query optimization
芯源&立创EDA训练营——无刷电机驱动
Scala concurrent programming (II) akka
11gR2 Database Services for " Policy" and " Administrator" Managed databases (file I
保研笔记四 软件工程与计算卷二(8-12章)
Pyqt control part (I)
The PNG image is normal when LabVIEW is opened, and the full black image is obtained when Photoshop is opened
Neural structured learning - Part 3: training with synthesized graphs
Opencvsharp (C openCV) shape detection and recognition (with source code)
Switching power supply buck circuit CCM and DCM working mode
Fiddler Everywhere 3.2.1 Crack
MySQL delete uniqueness constraint unique
CIS基准测试工具kube-bench使用
帶外和帶內的區別
无刷驱动设计——浅谈MOS驱动电路
【EF Core】EF Core与C# 数据类型映射关系
JVM details
Live tiktok shop 2022 latest gameplay card slot overseas live e-commerce new traffic