C#基础:Reflection(反射)

作者:微信公众号:【架构师老卢】
8-3 18:18
34

概述:使用 Reflection,任何人都可以动态地确定程序的类型、方法、属性、构造函数和其他成员,而无需在编译时了解它们。反射提供了一种以灵活方式与类型和对象交互的强大方法。您可以使用反射来获取有关类型中定义的事件的信息,例如它们的名称、事件处理程序和其他元数据。C# 中的反射与示例以下是 C# 中反射的一些常见用法:检查类型:反射可用于在运行时获取有关类型的信息,例如它们的属性、方法、字段和特性。在运行时创建实例:您可以使用反射在运行时创建类型(属性、方法、字段和特性)的实例,即使您在运行时只知道类型(属性、方法、字段和属性)名称。在运行时调用的方法和属性:反射可用于在运行时调用方法和属性,这在

使用 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
相关留言评论
昵称:
邮箱:
阅读排行