c# 反射使用实例

 
1 创建用于反射使用的DLL
新建一个C#类库项目,拷贝源代码如下,编译生成DLL(假如DLL的文件名是TestReflect.dll)
  using  System;
 
namespace  Webtest
 
{
  
/**//// <summary>
  
/// ReflectTest 的摘要说明。
  
/// </summary>

  public class ReflectTest
  
{
  
public ReflectTest()
  
{}
  
public string WriteString(string s)
  
{
   
return "欢迎您," + s;
  }

  
/**//// <summary>
  
/// dsajkjflasjdfalksdjfaskfd
  
/// </summary>
  
/// <param name="s"></param>
  
/// <returns></returns>

  public static string WriteName(string s)
  
{
   
return "欢迎您光临," + s;
  }

  
public string WriteNoPara()
  
{
   
return "您使用的是无参数方法";
  }

 }

}

2 应用于反射的例子

ASPNET页面中加入以下函数:

  public   void  test1()
 
{
    System.Reflection.Assembly ass;
    Type type ;
    
object obj;
    
try
    
{
     ass 
= System.Reflection.Assembly.LoadFile(@"d:TestReflect.dll");
     type 
= ass.GetType("Webtest.ReflectTest");//必须使用名称空间+类名称
     System.Reflection.MethodInfo method = type.GetMethod("WriteString");//方法的名称
     obj = ass.CreateInstance("Webtest.ReflectTest");//必须使用名称空间+类名称
    string s = (string)method.Invoke(obj,new string[]{"jianglijun"}); //实例方法的调用
    Response.Write(s+"<br>");
    method 
= type.GetMethod("WriteName");//方法的名称
    s = (string)method.Invoke(null,new string[]{"jianglijun"}); //静态方法的调用
    Response.Write(s+"<br>");
    method 
= type.GetMethod("WriteNoPara");//无参数的实例方法
    s = (string)method.Invoke(obj,null);
    Response.Write(s
+"<br>");
    method 
= null;
   }

   
catch(Exception ex)
   
{
    Response.Write(ex
+"<br>");
   }

   
finally
   
{
    ass 
= null;
    type 
= null;
    obj 
= null;
   }

  }


 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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"`为你实际的类型名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值