zoukankan      html  css  js  c++  java
  • 反射 运用

    在使用之前了解一下相关知识。

    介绍:

       反射使用System.Reflection命名空间。此命名空间中几个类的关系需要先明白。

        1) AppDomain:应用程序域,可以将其理解为一组程序集的逻辑容器.
        2) Assembly:程序集类
        3) Module:模块类
        4) Type:使用反射得到类型信息的最核心的类
        他们之间是一种从属关系,也就是说,一个AppDomain可以包含N个Assembly,一个Assembly可以包含N个Module,而一个Module可以包含N个Type.

    使用 :

        要动态加载一个程序集主要用Assembly的Load和LoadFrom的静态方法.

        Assembly.Load("程序集名")
        Assembly.LoadFrom("程序集实际路径")

    例子:

    下边代码摘自:http://www.cnblogs.com/zhangpengshou/archive/2010/03/29/1699881.html

    //程序集测试类test.cs
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace testclass
    {
        
    public class Class1
        {
            
    public string GetName1()
            {
                
    return "Get the name!";
            }

            
    public string GetName2(string name)
            {
                
    return name;
            }
        }
    }


        
    //调用代码
        class Program
        {
            
    static void Main(string[] args)
            {
                System.Reflection.Assembly ass 
    = System.Reflection.Assembly.LoadFile(@"C:\testclass.dll");
                Type type;
                
    object obj;
                type 
    = ass.GetType("testclass.Class1");      //必须使用名称空间+类名称
                obj = ass.CreateInstance("testclass.Class1");//必须使用名称空间+类名称

               
                System.Reflection.MethodInfo method 
    = type.GetMethod("GetName1");//方法的名称
                string s = (string)method.Invoke(obj, null); //实例方法的调
                Console.WriteLine(s);

                method 
    = type.GetMethod("GetName2");
                s 
    = (string)method.Invoke(obj, new string[] { "你好!" });
                Console.WriteLine(s);
            }
        }


    在实际运用中,我们可以用下边方法直接创建类。

            public static AbstractFactory GetInstance()
            {
                
    //从配置文件中取类名名称
                string factoryName =  ConfigurationSettings.AppSettings["factoryName"];
                AbstractFactory instance;
                
    if (factoryName != "")
                {
                    
    //加载程序集,并创建类CreateInstance的格式为CreateInstace(命名空间.类名)
                    instance = (AbstractFactory)Assembly.Load("AbstractFactoryPattern")
                        .CreateInstance(
    "AbstractFactoryPattern."+factoryName );
                }
                
    else
                {
                    
    return null;
                }
                
    //返加通过反射得到的类
                return instance;
            }

     

  • 相关阅读:
    lhgdialogv3.13 使用点滴
    CheckBoxList 取值 及选中相关用法
    repeater 及 gridview 中绑定短日期
    数据库中日期大小的判断
    父子不同窗口间刷新传值
    子级Repeater获取父级Repeater绑定项的值
    vs.net 2010 web 项目中使用 webservice
    web打印实现方案 Lodop6.034 使用方法总结
    用 showModalDialog 方法回传数据到父页中去
    vs.net2010中使用 Ajax Control Toolkit
  • 原文地址:https://www.cnblogs.com/scottckt/p/2046772.html
Copyright © 2011-2022 走看看