当前位置:网站首页>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();
}
}
}
边栏推荐
- UVA11294-Wedding(2-SAT)
- 如何提升口才
- VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性
- golang代码检查工具
- Russian Foreign Ministry: Japan and South Korea's participation in the NATO summit affects security and stability in Asia
- [EF core] mapping relationship between EF core and C data type
- Brushless drive design -- on MOS drive circuit
- 【LeetCode】5. Valid palindrome
- Rasa 3.x 学习系列-Rasa 3.2.1 新版本发布
- 保研笔记一 软件工程与计算卷二(1-7章)
猜你喜欢
GFS distributed file system
21. PWM application programming
Technical specifications and model selection guidelines for TVs tubes and ESD tubes - recommended by jialichuang
开关电源Buck电路CCM及DCM工作模式
[classical control theory] summary of automatic control experiment
Huawei simulator ENSP - hcip - MPLS experiment
保研笔记一 软件工程与计算卷二(1-7章)
Spire Office 7.5.4 for NET
The PNG image is normal when LabVIEW is opened, and the full black image is obtained when Photoshop is opened
98. 验证二叉搜索树 ●●
随机推荐
MySQL (1) -- related concepts, SQL classification, and simple operations
poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
The PNG image is normal when LabVIEW is opened, and the full black image is obtained when Photoshop is opened
GFS分布式文件系统
C# 反射与Type
Russian Foreign Ministry: Japan and South Korea's participation in the NATO summit affects security and stability in Asia
How to enable relationship view in phpMyAdmin - how to enable relationship view in phpMyAdmin
哪些偏门项目可以做到?自媒体做到月赚一万以上很难吗?
Spécifications techniques et lignes directrices pour la sélection des tubes TVS et ESD - Recommandation de jialichuang
保研笔记四 软件工程与计算卷二(8-12章)
Cwaitabletimer timer, used to create timer object access
无刷驱动设计——浅谈MOS驱动电路
MySQL replace primary key delete primary key add primary key
SpreadJS 15.1 CN 与 SpreadJS 15.1 EN
Technical specifications and model selection guidelines for TVs tubes and ESD tubes - recommended by jialichuang
Fiddler Everywhere 3.2.1 Crack
20220703 周赛:知道秘密的人数-动规(题解)
Opencvsharp (C openCV) shape detection and recognition (with source code)
Design and implementation of secsha system
Bao Yan notes II software engineering and calculation volume II (Chapter 13-16)