C#反射实例讲解

第一例:

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;
        }
    }
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值