当前位置:网站首页>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();
}
}
}
边栏推荐
- Naoqi robot summary 26
- Latex multiple linebreaks
- 秒杀系统的设计与实现思路
- Initial experience | purchase and activate typora software
- The PostgreSQL column reference 'ID' is ambiguous - PostgreSQL column reference'id'is ambiguous
- 数学公式截图识别神器Mathpix无限使用教程
- Development specification: interface unified return value format [resend]
- 开源crm客户关系统管理系统源码,免费分享
- What is the process of building a website
- golang代码检查工具
猜你喜欢
Sum of two numbers, sum of three numbers (sort + double pointer)
14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
基于脉冲神经网络的物体检测
Development specification: interface unified return value format [resend]
Use of grpc interceptor
Rasa 3.x 学习系列-Rasa X 社区版(免费版) 更改
Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?
Fiddler Everywhere 3.2.1 Crack
Go语言实现原理——Map实现原理
Scala concurrent programming (II) akka
随机推荐
Latex multiple linebreaks
Qcombox (rewrite) + qcompleter (auto completion, auto loading the drop-down options of qcombox, setting the background color)
Use of grpc interceptor
俄外交部:日韩参加北约峰会影响亚洲安全稳定
TS type declaration
When to use useImperativeHandle, useLayoutEffect, and useDebugValue
Live tiktok shop 2022 latest gameplay card slot overseas live e-commerce new traffic
VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性
UVA11294-Wedding(2-SAT)
做自媒体影视短视频剪辑号,在哪儿下载素材?
数学公式截图识别神器Mathpix无限使用教程
Différence entre hors bande et en bande
代码农民提高生产力
YML configuration, binding and injection, verification, unit of bean
yate.conf
芯源&立创EDA训练营——无刷电机驱动
Use of shell:for loop
C# Linq Demo
Attacking technology Er - Automation
TVS管和ESD管的技術指標和選型指南-嘉立創推薦