码迷,mamicode.com
首页 > 其他好文 > 详细

简单反射练习

时间:2016-11-06 22:35:02      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:open   each   通过   text   generic   eve   lag   console   from   

主要是:

  • 利用反射对实例进行构造;
  • 以及调用实例的方法;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace AssemblyBuilder
{
    class Program
    {
        public static Assembly ReadAssembly(string assemblyPath)
        {
            return Assembly.LoadFrom(assemblyPath);
        }

        public static Assembly ReadAssembly(Stream stream)
        {
            SecurityPermission securityPermission = new SecurityPermission(SecurityPermissionFlag.ControlEvidence);
            securityPermission.Assert();
            int num = (int)stream.Length;
            byte[] array = new byte[num];
            stream.Read(array, 0, num);
            try
            {
                return Assembly.Load(array);
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
        }

        public static Stream ReadAssemblyToStream(string assemblyPath)
        {
            FileStream fileStream = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read, FileShare.Read);
            try
            {
                int num = (int)fileStream.Length;
                byte[] array = new byte[num];
                fileStream.Read(array, 0, num);
                return new MemoryStream(array);
            }
            finally
            {
                fileStream.Close();
            }
        }

        static void Main(string[] args)
        {
            Stream assemblyStream = ReadAssemblyToStream("./Lambda_Test.exe");
            var assembly = ReadAssembly(assemblyStream);

            //使用 InvokeMember 调用构造函数初始化实例也可以
            var programInstance = assembly.CreateInstance("FactoryMode.Program");   
            if (programInstance == null)
            {
                Console.WriteLine("There is no this type");
            }
            else
            {
                var myType = programInstance.GetType();
                Console.WriteLine("Type name is {0}", myType.ToString());

                MethodInfo[] methods = myType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);
                Console.WriteLine("Method number:{0}", methods.Length);
                foreach(var method in methods)
                {
                    //怎样通过反射调用方法?
                    if(method.Name == "NotMainMethod")
                    {
                        Console.WriteLine("Find method, name:{0}", method.Name);

                        //没有实例直接用类型进行 InvokeMember 调用,需要调用类型的构造函数后再调用目标方法(前提示有定义构造函数),要带上 BindingFlags.CreateInstance                  
                        //myType.InvokeMember("NotMainMethod", BindingFlags.Public | BindingFlags.CreateInstance, null, null, null);

                        //使用已有实例时,就要指定 BindingFlags.Instance
                        myType.InvokeMember("NotMainMethod", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, programInstance, null);

                        //有实例则直接使用
                        method.Invoke(programInstance, null);
                    }
                }
            }

            Console.ReadKey();
        }
    }
}

 

简单反射练习

标签:open   each   通过   text   generic   eve   lag   console   from   

原文地址:http://www.cnblogs.com/Daniel-Liang/p/6036500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!