Assembly.GetExecutingAssembly()

作者:追风剑情 发布于:2014-6-24 22:42 分类:C#

using System;
using System.Reflection;

namespace AssemblyTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //利用反射创建对象
            Assembly asm = Assembly.GetExecutingAssembly();
            Object obj = asm.CreateInstance("AssemblyTest.TestClass", true);
            //调用对象的成员方法
            Type t = typeof(TestClass);
            t.InvokeMember("Fun", BindingFlags.InvokeMethod, null, obj, null);
            //调用静态方法
            t.InvokeMember("StaticFun", BindingFlags.InvokeMethod, null, null, null);

            Console.ReadLine();
        }
    }

    class TestClass
    {
        public void Fun()
        {
            Console.WriteLine("Invoke Fun");
        }

        public static void StaticFun()
        {
            Console.WriteLine("Invoke Static Fun");
        }
    }
}
运行效果
invokefun.png 

标签: GetExecutingAssembly

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号