C#反射简单例子

参考了 http://www.cnblogs.com/sosoft/p/3506015.html

    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            System.Console.WriteLine("列出程序集中的所有类型");
            Assembly a = Assembly.LoadFrom("ReflectionExample.exe");


            Type[] mytypes = a.GetTypes();


            foreach (Type t in mytypes)
            {
                System.Console.WriteLine(t.Name);
            }
            System.Console.ReadLine();


            System.Console.WriteLine("列出HelloWorld中的所有方法");


            Type ht = typeof(HelloWorld);


            MethodInfo[] mif = ht.GetMethods();


            foreach (MethodInfo mf in mif)
            {
                System.Console.WriteLine(mf.Name);
            }


            System.Console.ReadLine();


            System.Console.WriteLine("实例化HelloWorld,并调用SayHello方法");


            Object obj = Activator.CreateInstance(ht);


            string[] s = { "ZhenLei" };


            Object objName = Activator.CreateInstance(ht, s);


            //BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public | 
            // BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);


            MethodInfo msayhello = ht.GetMethod("SayHello");


            msayhello.Invoke(obj, null);


            msayhello.Invoke(objName, null);


            System.Console.ReadLine();


        }
    }


    class HelloWorld
    {
        string myName = null;


        public HelloWorld(string name)
        {
            myName = name;
        }
        public HelloWorld()
            : this(null)
        {


        }


        public string Name
        {
            get
            { return myName; }
        }


        public void SayHello()
        {
            if (myName == null)
                System.Console.WriteLine("Hello World");
            else
                System.Console.WriteLine("Hello," + myName);
        }
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#反射是一种强大的编程技术,它允许我们在运行时检查程序集、模块和类型,并动态地创建实例、调用方法、访问字段和属性等。下面是一个示例代码,展示了如何使用C#反射来加载和使用DLL。 ```csharp using System; using System.Reflection; namespace ReflectionExample { public interface IPlugin { void Run(); } public class PluginA : IPlugin { public void Run() { Console.WriteLine("PluginA is running."); } } public class PluginB : IPlugin { public void Run() { Console.WriteLine("PluginB is running."); } } class Program { static void Main(string[] args) { // 动态加载DLL并获取类型 string dllPath = "path/to/your/plugin.dll"; Assembly assembly = Assembly.LoadFile(dllPath); Type pluginType = assembly.GetType("Namespace.PluginA"); // 创建实例并调用方法 IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType); plugin.Run(); } } } ``` 上述代码中,我们定义了一个接口`IPlugin`,并在DLL中实现了两个类`PluginA`和`PluginB`来实现该接口。在`Main`方法中,我们使用`Assembly.LoadFile`方法动态加载了DLL,并使用`assembly.GetType`方法获取了`PluginA`的类型。然后,我们使用`Activator.CreateInstance`方法创建了`PluginA`的实例,并通过调用其`Run`方法来运行插件的功能。 需要注意的是,上述示例仅仅展示了C#反射的基本用法,实际应用中可能会更加复杂。在实际使用中,你需要替换`"path/to/your/plugin.dll"`为你实际的DLL路径,以及`"Namespace.PluginA"`为你实际的类型名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值