博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

c#反射实例讲解(2)

Posted on 2009-11-16 16:50  一刻  阅读(106)  评论(0)    收藏  举报

 

第一例:

1 创建用于反射使用的DLL

新建一个C#类库项目,拷贝源代码如下,编译生成DLL(假如DLL的文件名是TestReflect.dll)

namespace Webtest
{
      //// <summary>
      /// ReflectTest 类。
      /// </summary>
      public class ReflectTest
      {
          public ReflectTest()
          {}

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

          /// <summary>
          /// 静态方法
          /// </summary>
          /// <param name="s"></param>
          /// <returns></returns>
          public static string WriteName(string s)
          {
           return "欢迎您光临," + s;
          }

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

2 通过下列方法调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace WebApp
{
    public class AssClass
    {
        public string GetReturnString()
        {
            string restr = string.Empty;
            object obj;
            Assembly ass = Assembly.LoadFile("e:\\anb\\TestReflect.dll");
            obj = ass.CreateInstance("Webtest.ReflectTest");

            Type type = ass.GetType("Webtest.ReflectTest");

            MethodInfo method = type.GetMethod("WriteString");
            restr = (string)method.Invoke(obj, new string[] { "jianglijun" });


            method = type.GetMethod("WriteName");          //静态方法调用
            restr = (string)method.Invoke(null, new string[] { "your name" });


            method = type.GetMethod("WriteNoPara");
            restr = (string)method.Invoke(obj, null);      //无参数方法调用

            return restr;
        }
    }
}

第二例:
新建如下类:
namespace TestSpace
{
    public class TestClass
    {
        private string _value;
        public TestClass()
        {
        }
        public TestClass(string value)
        {
            _value = value;
        }

        public string GetValue(string prefix)
        {
            if (_value == null)
                return "NULL";
            else
                return prefix + ":" + _value;
        }

        public string Value
        {
            set
            {
                _value = value;
            }
            get
            {
                if (_value == null)
                    return "NULL";
                else
                    return _value;
            }
        }
    }
}

2 通过下列方法调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace WebApp
{
    public class AssClass
    {
        public string GetReturnString()
        {
            //获取类型信息
            Type t = Type.GetType("TestSpace.TestClass");
            //构造器的参数
            object[] constuctParms = new object[] { "vicson" };
            //根据类型创建对象
            object dObj = Activator.CreateInstance(t, constuctParms);
            //获取方法的信息
            MethodInfo method = t.GetMethod("GetValue");
            //调用方法的一些标志位,这里的含义是Public并且是实例方法,这也是默认的值
            BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
            //GetValue方法的参数
            object[] parameters = new object[] { "Hello" };
            //调用方法,用一个object接收返回值
            object returnValue = method.Invoke(dObj, flag, Type.DefaultBinder, parameters, null);

            return (string)returnValue;
        }
    }
}

原著:http://hi.baidu.com/dongdongjiao/blog/item/f4c72bc24deaa036e4dd3b47.html