LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#基础:Reflection(反射)

admin
2024年8月17日 20:11 本文热度 313

使用 Reflection,任何人都可以动态地确定程序的类型、方法、属性、构造函数和其他成员,而无需在编译时了解它们。反射提供了一种以灵活方式与类型和对象交互的强大方法。您可以使用反射来获取有关类型中定义的事件的信息,例如它们的名称、事件处理程序和其他元数据。

C# 中的反射与示例

以下是 C# 中反射的一些常见用法:

检查类型:

反射可用于在运行时获取有关类型的信息,例如它们的属性、方法、字段和特性。

在运行时创建实例:

您可以使用反射在运行时创建类型(属性、方法、字段和特性)的实例,即使您在运行时只知道类型(属性、方法、字段和属性)名称。

在运行时调用的方法和属性:

反射可用于在运行时调用方法和属性,这在编译时不知道成员名称时特别有用。

获取和更改字段:

您可以使用反射在运行时获取和更改对象的字段。

检查属性:

反射可用于检查应用于类型、字段、方法、属性、构造函数等的属性,并可以根据它们应用必要的操作。

C# 中的反射与示例

using System;  
using System.Reflection;
public class FruitClass
{
public string fruitName { get; set; }
public void getFruitName()
{
Console.WriteLine("Name of Fruit is = " + fruitName);
}
}


class Program
{
public static void Main(string[] args)
{
// Get assembly dynamically
Type t = typeof(System.String);
Console.WriteLine(t.Assembly);
Console.WriteLine(t.FullName);
Console.WriteLine(t.BaseType);
Console.WriteLine(t.IsClass);
Console.WriteLine(t.IsEnum);
Console.WriteLine(t.IsInterface);
Console.WriteLine(t.Name);
Console.WriteLine(t.Namespace);
Console.WriteLine(t.Module);
// Get the type dynamically
Type fruitType = typeof(FruitClass);
// Create an instance of the type dynamically
object fruitInstance = Activator.CreateInstance(fruitType);
// Access a property dynamically
PropertyInfo propertyInfo = fruitType.GetProperty("fruitName");
propertyInfo.SetValue(fruitInstance, "Mango, Reflection");
// Invoke a method dynamically
MethodInfo methodInfo = fruitType.GetMethod("getFruitName");
methodInfo.Invoke(fruitInstance, null);
// Access the property value
string propertyValue = (string)propertyInfo.GetValue(fruitInstance);
Console.WriteLine("Property value: " + propertyValue);
}
}

输出

System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e  
System.String
System.Object
True
False
False
String
System
System.Private.CoreLib.dll
Name of Fruit is = Mango, Reflection
Property value: Mango, Reflection

C# 中使用枚举示例的反射

using System;  
using System.Reflection;
public class EventExample
{
public event EventHandler FruitEvent;
public void OnMyEvent()
{
// Raise the event
FruitEvent .Invoke(this, EventArgs.Empty);
}
}


class Program
{
public static void Main(string[] args)
{
// Create an instance of the class
EventExample example = new EventExample();
// Get the type of the class
Type type = example.GetType();
// Get the events defined in the class
EventInfo[] events = type.GetEvents();
// Display information about each event
foreach (EventInfo evt in events)
{
Console.WriteLine($"Event Name: {evt.Name}");
// Display information about the event handler delegate type
Console.WriteLine($"Event Handler Type: {evt.EventHandlerType}");
}
}
}

输出

Event Name: FruitEvent  
Event Handler Type: System.EventHandlert

C# 中的反射与事件示例

using System;  
using System.Reflection;
public enum FruitEnumName
{
Apple,
Banana,
Mango,
Grape,
Orange,
Cherry,
Kiwi
}


class Program
{
public static void Main(string[] args)
{
// Get assembly dynamically
Type enumType = typeof(FruitEnumName);
// Display the name of the enum
Console.WriteLine($"Enum Name: {enumType.Name}");
Console.WriteLine(enumType.IsEnum);
// Get enum values
Array enumValues = Enum.GetValues(enumType);
foreach (object value in enumValues)
{
// Display enum value name and underlying string value
Console.WriteLine($"Name: {Enum.GetName(enumType, value)}, Value: {(object)value}");
// Get the type dynamically
Type fruitEnumType = typeof(FruitEnumName);
// Create an instance of the type dynamically
object fruitEnumInstance = Activator.CreateInstance(fruitEnumType);
// Access a enum dynamically
string enumInstance = enumType.GetEnumName(fruitEnumInstance);
Console.WriteLine("Enum value: " + enumInstance);
}
}
}

输出

Enum Name: FruitEnumName  
True
Name: Apple, Value: Apple
Enum value: Apple
Name: Banana, Value: Banana
Enum value: Apple
Name: Mango, Value: Mango
Enum value: Apple
Name: Grape, Value: Grape
Enum value: Apple
Name: Orange, Value: Orange
Enum value: Apple
Name: Cherry, Value: Cherry
Enum value: Apple
Name: Kiwi, Value: Kiwi
Enum value: Apple

该文章在 2024/8/19 11:05:43 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved