首页  >  文章  >  后端开发  >  C# 中的反射

C# 中的反射

PHPz
PHPz原创
2024-09-03 15:19:12781浏览

C#中的反射是收集其特性信息并对其自身进行操作的过程。收集的信息包括对象的属性、类型、事件、方法等;反射对于查找所有类型的程序集很有用。它动态地调用一个组装方法,我们可以动态地将类型绑定到现有对象或从现有对象获取类型;反射用于创建该类型的实例。我们可以访问其属性和字段,反射的主要目的是用于读取其元数据以在运行时搜索程序集。

为什么 C# 中需要反射?

运行时我们需要C#中的反射来获取类型信息;它是观察托管代码以读取其元数据以在运行时查找模块和程序集的过程。反射通过从程序集中提取元数据来反映程序,该元数据用于修改其行为。  系统。反射命名空间允许您访问方法、加载类型和字段的托管视图,以动态创建和调用类型。我们要求对以下申请流程进行反思,

  • 在运行时过程中,反射允许查看属性信息。
  • 反射允许后期绑定属性和方法
  • 它检查几种类型的程序集及其类型
  • 它允许在运行时创建新类型,并按照这些类型执行各种任务。

反射在 C# 中如何工作?

C# 反射使应用程序能够自行获取信息,并且对自身进行操作。它有效地搜索所有类型的程序集并动态调用程序集方法。

反射中使用的最重要的类是 System.类型类是一个抽象类,表示称为 CTS(通用类型系统)的类型。使用此类,我们可以找到在命名空间模块中使用的类型,并验证给定的类型是值类型还是引用类型。通过使用以下项目,我们可以解析元数据表,

  • 方法
  • 属性
  • 活动
  • 田野

利用反射,实现了Late Binding;在编译时,由于这些原因,我们可能不知道要加载哪个程序集;我们通过要求用户在运行时执行时输入程序集名称和类型来加载适当的程序集。通过直接加载程序集,我们使用 System.反射。通过获取三个静态类型进行组装是,

  • 加载自
  • 加载模块
  • LoadWithPartialName

考虑到程序集是 exe 或 dll 文件,使该文件成为通用类型系统的可移植可执行文件,其扩展名为 .dll 或 .exe。可移植的可执行文件是包含多个表的元数据,如下所示,

  • 方法定义表
  • 类型定义表
  • 字段定义表

C# 中的反射示例

下面给出了 C# 中反射的示例:

示例#1

using System;
using System.Reflection;
namespace Reflection_Sample {
class Program_1 {
// Main Method
static void Main(string[] args)
{
// to initialize _type as typeof string
Type _type = typeof(string);
// by using the Reflection to find and in any sort of data related to _type
Console.WriteLine("Name : {0}", _type.Name);
Console.WriteLine("Full Name : {0}", _type.FullName);
Console.WriteLine("Namespace : {0}", _type.Namespace);
Console.WriteLine("Base Type : {0}", _type.BaseType);
}
}
}

在上面的代码中,我们使用 typeof 方法将类型 _type 作为字符串加载。然后我们关联 _type 上的反射来获取字符串类的信息,包括命名空间、名称、全名和基本类型。

输出:

C# 中的反射

示例#2

在这个程序中,我们通过定义typeof方法来获取程序集,并通过这种方式获取_type。集会。我们来看看示例程序

using System;
using System.Reflection;
public class ReflectionExample
{
public static void Main()
{
Type _type = typeof(System.String);
Console.WriteLine(_type.Assembly);
}
}

输出:

C# 中的反射

示例#3

在这个程序中,我们使用反射来显示元数据;它包括方法、类和各种参数化构造函数。让我们看下面的例子,

using System;
using System.Reflection;
namespace Sample_ReflectionMetadata
{
// to define a class StudentDetails
class StudentDetails
{
// defining the Properties
public int _RollNo
{
get;
set;
}
public string _Name
{
get;
set;
}
// Constructor with no arguments
public StudentDetails()
{
_RollNo = 0;
_Name = string.Empty;
}
// this is a Parameterised Constructor with 2 parameters
public StudentDetails(int _Srollno, string _Sname)
{
_RollNo = _Srollno;
_Name = _Sname;
}
// to invoke method to Display Student Details
public void StudentDisplayData()
{
Console.WriteLine("Roll Number : {0}", _RollNo);
Console.WriteLine("Name : {0}", _Name);
}
}
class ReflectionClass
{
// Main Method
static void Main(string[] args)
{
// to declare Assembly and loading the current assembly
Assembly _executing = Assembly.GetExecutingAssembly();
Type[] _types = _executing.GetTypes();
foreach(var item in _types)
{
Console.WriteLine("Class : {0}", item.Name);
// storing the methods  in array
MethodInfo[] methods = item.GetMethods();
foreach(var method in methods)
{
// for displaying each method
Console.WriteLine("--> Method : {0}", method.Name);
// to store the parameters in array
ParameterInfo[] parameters = method.GetParameters();
foreach(var arg in parameters)
{
Console.WriteLine(" Parameter : {0} Type : {1}",
arg.Name, arg.ParameterType);
}
}
}
}
}
}

输出:

C# 中的反射

示例#4

反思是动态观察和改变其真实结构和行为的过程。在下面的示例程序中,反射的作用是我们可以在运行时分析和更改应用程序中的信息。  让我们看一下例子,

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ReflectionSample
{
class Program
{
private static int value_1= 15, value_2 = 25, value_3 = 30;
static void Main(string[] args)
{
Console.WriteLine("The Values are = " + (value_1 + value_2 + value_3));
Console.WriteLine("Input the Name of variable to be altered:");
string _varName = Console.ReadLine();
Type _type = typeof(Program);
FieldInfo _fieldInfo = _type.GetField(_varName, BindingFlags.NonPublic | BindingFlags.Static);
if(_fieldInfo != null)
{
Console.WriteLine("The Latest Value of " + _fieldInfo.Name + " is " + _fieldInfo.GetValue(null) + ". Input NeW Value:");
string newValue = Console.ReadLine();
int newInt;
if(int.TryParse(newValue, out newInt))
{
_fieldInfo.SetValue(null, newInt);
Console.WriteLine(" Final Values are = " + (value_1 + value_2 + value_3));
}
Console.ReadKey();
}
}
}
}

在这里,我们可以通过知道变量的名称来在运行时更改变量的值。通过使用反射,我们可以实现这些类型的方法。让我们看看下面的输出,

输出:

C# 中的反射

结论

希望您喜欢这篇文章; C# Reflection 涵盖了 .Net 中的重要功能;我们通过几个例子了解了反射在 C#.Net 中的工作原理。希望这篇文章能帮助您更好地理解。

以上是C# 中的反射的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:C# Params下一篇:C# Career