反射示例1-----执行反射dll的方法和获取属性的值

项目结构:

成都创新互联主打移动网站、成都做网站、网站设计、网站改版、网络推广、网站维护、国际域名空间、等互联网信息服务,为各行业提供服务。在技术实力的保障下,我们为客户承诺稳定,放心的服务,根据网站的内容与功能再决定采用什么样的设计。最后,要实现符合网站需求的内容、功能与设计,我们还会规划稳定安全的技术方案做保障。

反射示例1-----执行反射dll的方法和获取属性的值

将Document类库生成的DLL文件放在ConsoleApplication2\ConsoleApplication2\bin\Debug文件夹下

-------------------------------------------------------------Document.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Document
{
    public class Document
    {
        public string Name = "李四";
        public string GetName(string name)
        {
            return name;
        }
    }
}

 -------------------------------------------------------------Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            //获取程序集
            Assembly asb = Assembly.LoadFrom(Directory.GetCurrentDirectory() + "/Document.dll");
            //获取程序集下面的Document类
            Type document = asb.GetType("Document.Document");
            //实例化Document类对象(有参数的话需要传递object参数)
            object docObj = Activator.CreateInstance(document);
            //获取Document类中的方法
            MethodInfo mi = document.GetMethod("GetName");
            //参数
            object[] parameter = new object[] { "张三" };
            Console.WriteLine(mi.Invoke(docObj, parameter).ToString());//输出方法的返回值:张三
            //获取Document类中的属性
            FieldInfo pi = document.GetField("Name");
            Console.WriteLine(pi.GetValue(docObj));//输出方法的属性:李四
            Console.ReadKey();
        }
    }
}

文章名称:反射示例1-----执行反射dll的方法和获取属性的值
当前链接:http://bzwzjz.com/article/gpjjee.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 网站建设 企业网站建设 成都网站建设推广 响应式网站设计 成都网站设计 成都网站设计 网站制作公司 成都h5网站建设 重庆网站制作 网站建设方案 自适应网站建设 手机网站建设套餐 营销型网站建设 成都品牌网站设计 成都网站制作 商城网站建设 成都网站建设 成都网站建设公司 成都网站设计 高端网站设计 成都企业网站建设公司 成都网站建设