反射示例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();
}
}
}另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网页题目:反射示例1-----执行反射dll的方法和获取属性的值-创新互联
文章源于:http://www.jxjierui.cn/article/csgpci.html


咨询
建站咨询
