咨询热线:
024-88614238
最新动态
相关新闻
C#中的反射实例
实例一

using System;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace Wrox.ProCSharp.TypeView
{
   class MainClass
   {
      static void Main()
      {
         Type t = typeof(double);
         AnalyzeType(t);
         MessageBox.Show(OutputText.ToString(), "Analysis of type " + t.Name);
         Console.ReadLine();
      }

      static void AnalyzeType(Type t)
      {
         AddToOutput("Type Name:  " + t.Name);
         AddToOutput("Full Name:  " + t.FullName);
         AddToOutput("Namespace:  " + t.Namespace);
         Type tBase = t.BaseType;
         if (tBase != null)
            AddToOutput("Base Type:" + tBase.Name);
         Type tUnderlyingSystem = t.UnderlyingSystemType;
         if (tUnderlyingSystem != null)
            AddToOutput("UnderlyingSystem Type:" + tUnderlyingSystem.Name);

         AddToOutput(" PUBLIC MEMBERS:");
         MemberInfo [] Members = t.GetMembers();
         foreach (MemberInfo NextMember in Members)
         {
            AddToOutput(NextMember.DeclaringType + "  " + NextMember.MemberType +
               " + NextMember.Name);
         }
      }

      static void AddToOutput(string Text)
      {
         OutputText.Append(" " + Text);
      }

      static StringBuilder OutputText = new StringBuilder(500);
   }
}

运行结果:

 

实例二

 步骤1:新建一个类库test1,在类库下有一个类ReflectTest.cs,此类中的代码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace test1
{
    public class ReflectTest
 {
  public ReflectTest()
  {}

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

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

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

 

步骤2:

 建立一个类test_1.cs,代码如下:

using System;
using System.Text;
using System.Reflection;
public class test1
{
 public static void Main()
 {
   System.Reflection.Assembly ass;
   Type type ;
   object obj;
   try
    {
        //ass = Assembly.LoadFile(@"D:C#学习Chapter11WhatsNewAttributes est1objDebug est1.dll");
        //ass = Assembly.LoadFrom(@"D:C#学习Chapter11WhatsNewAttributes est1objDebug est1.dll");
        ass = Assembly.Load("test1");
        Type[] test_type1 = ass.GetTypes();
        foreach (Type t1 in test_type1)
        {
            Console.WriteLine(t1.Name);           
        }
        type = ass.GetType("test1.ReflectTest");//必须使用名称空间+类名称

        foreach (MemberInfo MI in type.GetMembers())
        {
            Console.WriteLine(MI.MemberType + "  " + MI.Name);           
        }

        Console.WriteLine("*******");

        MethodInfo method_1 = type.GetMethod("WriteString");//方法的名称
        obj = ass.CreateInstance("test1.ReflectTest");//必须使用名称空间+类名称

        string s = (string)method_1.Invoke(obj, new string[] {"jianglijun" }); //实例方法的调用
      
        Console.WriteLine(s);
        MethodInfo method_2 = type.GetMethod("WriteName");//方法的名称
        s = (string)method_2.Invoke(null,new string[]{"jianglijun"}); //静态方法的调用
        Console.Write(s);

        MethodInfo method_3 = type.GetMethod("WriteNoPara");//无参数的实例方法
        s = (string)method_3.Invoke(obj,null);
        Console.WriteLine(s);
        //method = null;
   }
   catch(Exception ex)
   {
        Console.WriteLine("出现错误!");
        Console.WriteLine(ex);
   }
   finally
   {
        ass = null;
        type = null;
        obj = null;
   }
 }
}

运行结果如下:

 

沈阳网络公司招聘

联系方式
地址:沈阳市东陵区
万柳塘路109甲1宏发 大厦525室
电话:024-24804848
8748636
15317796
102656105

友情链接(联系QQ:8748636) 沈阳网站制作| 沈阳网站制作| 沈阳SEO公司| 沈阳主机托管| 沈阳网络公司| 沈阳网站制作| 沈阳网站建设|
地址:沈阳市沈河区万柳塘路36-1 弘锦大厦412室 经理QQ:8748636 技术QQ:30999233 联系电话:024-88614238
Copyright? 2005-2013,沈阳海风网络科技有限公司 All Right Reserved. 版权所有 辽ICP备05004358号
【海风数据港】是专业沈阳服务器托管、沈阳网站制作、沈阳网站优化、沈阳网站建设的沈阳网络公司