c# 反射LoadFile
时间: 2023-11-12 16:57:47 浏览: 43
反射中的 LoadFile 方法可以加载指定路径下的程序集,并返回一个 Assembly 对象。使用该对象可以获取程序集中的类型、方法、属性等信息,并进行操作。
示例代码:
```
string assemblyPath = "D:\\MyAssembly.dll";
Assembly assembly = Assembly.LoadFile(assemblyPath);
Type type = assembly.GetType("MyNamespace.MyClass");
object instance = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod("MyMethod");
method.Invoke(instance, null);
```
上述代码中,我们首先使用 LoadFile 方法加载了一个名为 MyAssembly.dll 的程序集,然后获取了其中的 MyClass 类型,并创建了一个该类型的实例。最后,我们获取了该类型中的 MyMethod 方法,并调用了它。
相关问题
c# 反射
C# 反射是指在运行时动态地获取类的信息,通过反射可以获取类的属性、方法、事件等信息,以及动态创建对象、调用方法、获取值等操作。这使得我们能够在运行时动态地编写代码,可以写出更加灵活和可扩展的程序。
C# 反射的核心是 `System.Reflection` 命名空间,该命名空间包含了许多与反射相关的类和接口,比如 `Type` 类、`MethodInfo` 类、`PropertyInfo` 类等。
下面是一些常用的反射操作:
1. 获取类型信息
可以使用 `typeof` 关键字或者 `Type.GetType()` 方法来获取类型的信息。`typeof` 关键字用于获取编译时已知的类型信息,而 `Type.GetType()` 方法则可以通过类型名称获取运行时的类型信息。
```csharp
// 获取 System.String 类型的信息
Type type1 = typeof(System.String);
// 获取指定类型名称的信息
Type type2 = Type.GetType("System.String");
```
2. 获取成员信息
可以使用 `Type.GetMembers()` 方法获取类型的所有成员信息,包括属性、方法、字段、事件等。也可以使用 `Type.GetMethod()`、`Type.GetProperty()`、`Type.GetField()`、`Type.GetEvent()` 等方法获取指定成员的信息。
```csharp
Type type = typeof(Person);
// 获取类型的所有成员信息
MemberInfo[] members = type.GetMembers();
// 获取指定属性的信息
PropertyInfo property = type.GetProperty("Name");
// 获取指定方法的信息
MethodInfo method = type.GetMethod("SayHello");
// 获取指定字段的信息
FieldInfo field = type.GetField("Age");
// 获取指定事件的信息
EventInfo eventInfo = type.GetEvent("PropertyChanged");
```
3. 动态创建对象
可以使用 `Activator.CreateInstance()` 方法动态创建对象,也可以使用 `Type.InvokeMember()` 方法调用构造函数来创建对象。
```csharp
Type type = typeof(Person);
// 使用 Activator.CreateInstance() 方法创建对象
Person person1 = (Person)Activator.CreateInstance(type);
// 使用 Type.InvokeMember() 方法调用构造函数创建对象
Person person2 = (Person)type.InvokeMember(null,
BindingFlags.CreateInstance,
null,
null,
new object[] { "Tom", 18 });
```
4. 调用成员
可以使用 `MethodInfo.Invoke()` 方法调用方法,也可以使用 `PropertyInfo.SetValue()` 方法设置属性的值,使用 `FieldInfo.SetValue()` 方法设置字段的值。
```csharp
Type type = typeof(Person);
Person person = new Person("Tom", 18);
// 调用方法
MethodInfo method = type.GetMethod("SayHello");
method.Invoke(person, null);
// 设置属性的值
PropertyInfo property = type.GetProperty("Name");
property.SetValue(person, "Jerry", null);
// 设置字段的值
FieldInfo field = type.GetField("Age");
field.SetValue(person, 20);
```
以上是 C# 反射的一些基本操作,反射的应用非常广泛,可以用来实现插件式开发、ORM 映射等功能。
C# 反射的实例代码
C#反射是一种强大的编程技术,它允许我们在运行时检查程序集、模块和类型,并动态地创建实例、调用方法、访问字段和属性等。下面是一个示例代码,展示了如何使用C#反射来加载和使用DLL。
```csharp
using System;
using System.Reflection;
namespace ReflectionExample
{
public interface IPlugin
{
void Run();
}
public class PluginA : IPlugin
{
public void Run()
{
Console.WriteLine("PluginA is running.");
}
}
public class PluginB : IPlugin
{
public void Run()
{
Console.WriteLine("PluginB is running.");
}
}
class Program
{
static void Main(string[] args)
{
// 动态加载DLL并获取类型
string dllPath = "path/to/your/plugin.dll";
Assembly assembly = Assembly.LoadFile(dllPath);
Type pluginType = assembly.GetType("Namespace.PluginA");
// 创建实例并调用方法
IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);
plugin.Run();
}
}
}
```
上述代码中,我们定义了一个接口`IPlugin`,并在DLL中实现了两个类`PluginA`和`PluginB`来实现该接口。在`Main`方法中,我们使用`Assembly.LoadFile`方法动态加载了DLL,并使用`assembly.GetType`方法获取了`PluginA`的类型。然后,我们使用`Activator.CreateInstance`方法创建了`PluginA`的实例,并通过调用其`Run`方法来运行插件的功能。
需要注意的是,上述示例仅仅展示了C#反射的基本用法,实际应用中可能会更加复杂。在实际使用中,你需要替换`"path/to/your/plugin.dll"`为你实际的DLL路径,以及`"Namespace.PluginA"`为你实际的类型名称。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)