zoukankan      html  css  js  c++  java
  • C#开发之反射的简单使用

    奋斗的蘑菇 原文C#开发之反射的简单使用

    以前在Windows Mobile中写过一个写好的Dll中的图片的例子,现在在项目中有接触到在一个大的窗体中,动态的加载一些窗体这样的需求。将功能按照模块的划分进行单独开发成单独的Dll,主框架根据需要动态加载不同的Dll来加载不同的窗体来满足不同的需求。


    1.以下是一个简单的例子,有一个按钮点击事件,点击事件实现加载已知的Dll中的窗体。

    private void button1_Click(object sender, EventArgs e) 
    {  
          
        //点击加载DLL中的窗体Form4  
        string dllName = "ClassLibrary1.dll";  
        string formName = "ClassLibrary1.Form4";  
        Form ff = null;  
        //加载Dll  
        Assembly MyAssembly = Assembly.LoadFrom(dllName);  
        //获得Dll中的所有类、成员  
        Type[] types = MyAssembly.GetTypes();  
        Type type = null;  
        //遍历出需要的成员(窗体)  
        foreach(Type t in types) {  
            if(t.FullName == formName) {  
                type = t;                  
                break;  
            }  
        }  
        //对窗体进行实例化  
        ff = (Form)Activator.CreateInstance(type);  
        ff.Show();  
    }  

    2.反射机制调用Dll中的方法

    private void button1_Click(object sender, EventArgs e) 
    {  
          
        //点击加载DLL中的窗体Form4  
        string dllName = "ClassLibrary1.dll";  
        string formName = "ClassLibrary1.Form4";  
        Form ff = null;  
        //加载Dll  
        Assembly MyAssembly = Assembly.LoadFrom(dllName);  
        //获得Dll中的所有类、成员  
        Type[] types = MyAssembly.GetTypes();  
        Type type = null;  
        //遍历出需要的成员(窗体)  
        foreach(Type t in types) {  
            if(t.FullName == formName) {  
                type = t;                  
                break;  
            }  
        }  
        //对窗体进行实例化  
        ff = (Form)Activator.CreateInstance(type);  
        ff.Show();  
    }      
    
    public string Add(int x, int y)
     {  return x + y + "";  }  
    
    private void button2_Click(object sender, EventArgs e) 
    {  
      
        //加载Dll信息  
        string dllName = "ClassLibrary1.dll";  
        string dllNamespace="ClassLibrary1";  
        string className = "Class1";  
        string methodName = "Add";  
        //调用的方法参数  
        object[] parameters = new object[2] { 1, 2 };  
        string message = "";  
        //加载Dll信息  
        Assembly MyAssembly = Assembly.LoadFrom(dllName);  
        Type[] types = MyAssembly.GetTypes();  
        //遍历方法所在的类  
        foreach(Type t in types) {  
            if(t.Namespace == dllNamespace && t.Name == className) {  
                MethodInfo m = t.GetMethod(methodName);  
                if(m != null) {  
                    //调用Dll中的方法  
                    object o = Activator.CreateInstance(t);  
                    message= m.Invoke(o, parameters).ToString();  
                    MessageBox.Show(message);  
                } else  
                    MessageBox.Show(" 装载出错 !");  
            }  
        }  
      
    }  
  • 相关阅读:
    ubuntu如何设置Python的版本
    PHP队列之理论篇
    ubuntu系統如何啟動root用戶登陸?
    如何绑定腾讯企业邮箱?
    VMware虚拟机安装Ubuntu并设置root登陆
    毕业生,如何选择高薪资与学习机会?
    如何改变memcached默认的缓存时间?
    PHP常用函数之数组篇
    如何安装并使用bower包依赖工具
    z-score
  • 原文地址:https://www.cnblogs.com/arxive/p/6126212.html
Copyright © 2011-2022 走看看