c#中的反射代码实例讲解(转载)

news/2024/7/4 6:32:05 标签: c#, string, assembly, null, dll, exception
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 应用于反射的例子
 public void test1()
   {
    System.Reflection. Assembly ass;
     Type type ;
    object obj;
     try
     {
      ass = System.Reflection. Assembly.Load("TestReflect");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;
    }
   } 

http://www.niftyadmin.cn/n/1414359.html

相关文章

swift 两个数组合并 去除重复

var arr1 [1,3,4,6,7]var arr2 [1,2,3,4,5,6,7,8,9]var result [Int]()var index 0for a1 in arr1{for a2 in arr2{if a1 ! a2{//如果不重复就两个都要result[index] a1indexresult[index] a2index}else{//重复只要一个result[index] a1index} } }for val in result{pr…

subString(index,end) 用法

sb sb.Substring(0, sb.Length - 1); 获取当前字符串的前一部分去掉最后一个字符转载于:https://www.cnblogs.com/Yusco/p/10216112.html

VS 2005中调试JavaScript的方法

第一步&#xff1a;在Visual Studio 2005中打开一个已经存在的asp.net应用程序&#xff0c;其中要有一些JavaScript。 第二步&#xff1a;当我们在微软的Visual Studio中创建了一个Web项目&#xff0c;那么一个名为Web.config的文件默认也被创建在了该Web项目中。 我们检查一…

matplotlib实现热成像图colorbar和极坐标图

热成像图 %matplotlib inline from matplotlib import pyplot as plt import numpy as npdef f(x, y):return (1 - x / 2 x ** 5 y ** 3) * np.exp(-x ** 2 - y ** 2)n 10 x np.linspace(-3, 3, 4 * n) y np.linspace(-3, 3, 3 * n) X, Y np.meshgrid(x, y) plt.imshow(…

SP短信平台-线程池实现

这里不支持Delphi源代码高亮,将就着看看了:) 线程池的实现设计的时候考虑得过于复杂了,有些功能实际没有实现或者是实现了,也没有用到或去测试,只是使用了很少一部分的功能. 预分配线程->请求线程->释放线程->销毁线程,这是实际使用的过程,一些细节处理并没有用上,或…

mybatis学习笔记之基础框架(2)

mybatis学习笔记之基础框架&#xff08;2&#xff09; mybatis是一个持久层的框架&#xff0c;是apache下的顶级项目。 mybatis让程序将主要精力放在sql上&#xff0c;通过mybatis提供的映射方式&#xff0c;自由灵活生成满足sql语句 mybatis可将向prparedStatement中的输入…

重新启程

2019-01-04 16:11:29 今天下午的通用技术课&#xff0c;JC忘记把网络禁掉了&#xff0c;被我们钻了空子。 然后我重新回到了博客上。 你好&#xff0c;我是一名拿到省一后上了大半学期文化课以为自己处于半退役状态现在又要冲刺省队的OIer 老师对我们很有信心&#xff0c;说我们…

matplotlib 配置色彩点线样式网格及图列

色彩配置 import matplotlib.pyplot as plt import numpy as npy np.arange(5)plt.plot(y, colorg) plt.plot(y1, color0.5) plt.plot(y2, color#ff00ff) plt.plot(y3, color(0.1,0.2,0.3))plt.show() 点线配置 import matplotlib.pyplot as plt import numpy as npy np.a…