当前位置:网站首页>C# 反射与Type
C# 反射与Type
2022-07-05 23:26:00 【廷益--飞鸟】

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 知识点回顾
// 源语言程序: 某种程序设计语言写成的,比如C、C++、Java等语言写的程序
// 目标语言程序:二进制数表示的伪机器代码写的程序
#endregion
#region 程序集
// 程序集 就是我们写的一个代码集合
// 比如一个代码库文件(dll)或者 一个可执行文件(exe)
#endregion
#region 元数据
// 元数据 就是描述数据的数据
// 程序中的类,类中的函数、变量等等信息就是程序的 元数据
// 有关程序以及类型的数据被称为 元数据,它们保存在程序集中。
#endregion
#region 反射的概念
// 一个运行的程序查看本身或者其它程序的 元数据的行为 就叫做反射。
// 在程序运行时,通过反射可以得到其它程序集或者自己程序集代码的各种信息。
// 类、函数、变量、对象等等,实例化它们、执行它们、操作它们。
#endregion
#region 反射的作用
// 因为反射可以在 程序编译后 获得信息、所以它提高了程序的拓展性和灵活性。
// 1.程序运行时,得到所有元数据、包括元数据的特性
// 2.程序运行时,实例化对象、操作对象
// 3.程序运行时,可以创建新对象,用这些对象执行任务。
#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("反射");
// 相关语法
#region Type
// type 类的信息类
// 是反射功能的基础.
// 它是访问元数据的主要方式。
// 使用Type 的成员获取有关类型声明的信息
// 有关类型的成员 (如构造函数、方法、字段、属性和类的事件)
#endregion
#region 获取Type
// 1.Object中 获取GetType() 可以获取对象的Type
int a = 38;
Type typeA = a.GetType();
Console.WriteLine("变量a的类型:" + typeA );
// 2.通过typeof关键字获取 传入类名
Type typeB = typeof(int);
Console.WriteLine("typeof 获取类型:" + typeB);
// 3.通过类的名字 获取Type
// 注意必须包含命名空间、不然找不到。
Type typeName = Type.GetType("System.Int32");
Console.WriteLine("类名 获取类型:" + typeName);
#endregion
#region 得到程序集信息
Console.WriteLine(typeA.Assembly);
Console.WriteLine(typeB.Assembly);
Console.WriteLine(typeName.Assembly);
#endregion
#region 获取类中的所有公共成员
// 首先得到Type
Type tTest = typeof(Test);
// 得到所有公共成员
// 需要引用命名空间 using System.Reflection;
MemberInfo[] infos = tTest.GetMembers();
foreach (MemberInfo item in infos)
{
Console.WriteLine(item);
}
#endregion
#region 获取类中的 公共构造函数并调用
// 1.获取所有构造函数
ConstructorInfo[] ctors = tTest.GetConstructors();
foreach (ConstructorInfo item in ctors)
{
Console.WriteLine(item);
}
// 2.获取其中一个构造函数 并执行
// 2-1. 无参构造
ConstructorInfo ctorInfo = tTest.GetConstructor(new Type[0]); // 传入 Type 数组
// 执行无参构造
Test obj = ctorInfo.Invoke(null) as Test; // 无参构造
Console.WriteLine(obj.j);
// 2-2. 得到有参构造
ConstructorInfo ctorInfo2 = tTest.GetConstructor(new Type[] {
typeof(int)});
// 执行无参构造
Test obj1 = ctorInfo2.Invoke(new object[] {
2 }) as Test; // 无参构造
obj1.Speek();
#endregion
#region 获取类中的 成员变量
// 1.获取所有成员变量
FieldInfo[] fieldInfos = tTest.GetFields();
foreach (FieldInfo item in fieldInfos)
{
Console.WriteLine(item);
}
// 2.得到指定名称的公共成员变量
FieldInfo fieldJ = tTest.GetField("j");
Console.WriteLine(fieldJ);
// 3.通过 成员变量获取对象的值
Test test = new Test();
test.j = 99;
test.str = "3333";
// 3-3 通过反射 获取对象的成员变量的值
Console.WriteLine(fieldJ.GetValue(test));
// 3-4 通过反射 设置对象的成员变量的值
fieldJ.SetValue(test, 101);
Console.WriteLine(fieldJ.GetValue(test));
#endregion
#region 获取类中的 方法
// 1.获取所有方法
Type strType = typeof(string);
MethodInfo[] methodInfos = strType.GetMethods();
foreach (MethodInfo item in methodInfos)
{
Console.WriteLine(item);
}
// 2.获取其中一个方法
MethodInfo strSub = strType.GetMethod("Substring",
new Type[] {
typeof(int), typeof(int)});
// 3.使用反射方式 调用方法
string strHl = "Hello world!";
// 如果是静态方法 第一个参数 设置为null
object result = strSub.Invoke(strHl, new object[] {
7, 5 });
Console.WriteLine(result);
#endregion
Console.ReadLine();
}
}
}

边栏推荐
- TS type declaration
- 6-axis and 9-axis IMU attitude estimation
- Spire.PDF for NET 8.7.2
- (4)UART應用設計及仿真驗證2 —— TX模塊設計(無狀態機)
- Debian 10 installation configuration
- orgchart. JS organization chart, presenting structural data in an elegant way
- UART Application Design and Simulation Verification 2 - TX Module Design (Stateless machine)
- Code farmers to improve productivity
- Judge whether the binary tree is a complete binary tree
- 2:第一章:认识JVM规范1:JVM简介;
猜你喜欢

Go language implementation principle -- map implementation principle

【经典控制理论】自控实验总结

GFS分布式文件系统

Spire Office 7.5.4 for NET

There are 14 God note taking methods. Just choose one move to improve your learning and work efficiency by 100 times!

98. Verify the binary search tree ●●

MySQL replace primary key delete primary key add primary key

STM32__06—单通道ADC

YML configuration, binding and injection, verification, unit of bean

数学公式截图识别神器Mathpix无限使用教程
随机推荐
In C#, why can't I modify the member of a value type instance in a foreach loop?
Objective C message dispatch mechanism
Switching power supply buck circuit CCM and DCM working mode
Multi sensor fusion of imu/ electronic compass / wheel encoder (Kalman filter)
Naoqi robot summary 26
regular expression
哪些偏门项目可以做到?自媒体做到月赚一万以上很难吗?
TVS管和ESD管的技术指标和选型指南-嘉立创推荐
Comparison of parameters between TVs tube and zener diode
Technical specifications and model selection guidelines for TVs tubes and ESD tubes - recommended by jialichuang
Pyqt control part (I)
There are 14 God note taking methods. Just choose one move to improve your learning and work efficiency by 100 times!
Fiddler Everywhere 3.2.1 Crack
The interface of grafana tool displays an error, incluxdb error
ts类型声明declare
424. 替换后的最长重复字符 ●●
Krypton Factor-紫书第七章暴力求解
Live tiktok shop 2022 latest gameplay card slot overseas live e-commerce new traffic
698. Divided into k equal subsets ●●
6-axis and 9-axis IMU attitude estimation