当前位置:网站首页>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();
}
}
}
边栏推荐
- 做自媒体影视短视频剪辑号,在哪儿下载素材?
- 98. Verify the binary search tree ●●
- Bao Yan notebook IV software engineering and calculation volume II (Chapter 8-12)
- Do you regret becoming a programmer?
- Spire Office 7.5.4 for NET
- Rsync remote synchronization
- CIS benchmark tool Kube bench
- 成为程序员的你,后悔了吗?
- C# 反射与Type
- Cwaitabletimer timer, used to create timer object access
猜你喜欢
随机推荐
424. The longest repeated character after replacement ●●
Data analysis - Thinking foreshadowing
CAS and synchronized knowledge
Go language implementation principle -- map implementation principle
Comparison between webgl and webgpu [3] - vertex buffer
Neural structured learning 4 antagonistic learning for image classification
Tips for using pads router
MySQL (1) -- related concepts, SQL classification, and simple operations
UVA11294-Wedding(2-SAT)
TVS管和ESD管的技术指标和选型指南-嘉立创推荐
如何提升口才
Neural structured learning - Part 3: training with synthesized graphs
TS type declaration
Introduction to JVM
《牛客刷verilog》Part III Verilog企业真题
Pyqt control part (I)
做自媒体影视短视频剪辑号,在哪儿下载素材?
带外和带内的区别
98. 验证二叉搜索树 ●●
Xinyuan & Lichuang EDA training camp - brushless motor drive