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;
            }

     

  • 相关阅读:
    HDU5597/BestCoder Round #66 (div.2) GTW likes function 打表欧拉函数
    HDU5596/BestCoder Round #66 (div.2) 二分BIT/贪心
    HDU 5596/BestCoder Round #66 (div.2) GTW likes math 签到
    BZOJ 1877: [SDOI2009]晨跑 费用流
    BZOJ 1452: [JSOI2009]Count 二维树状数组
    BZOJ 1143 1143: [CTSC2008]祭祀river 最长反链
    Codeforces Round #335 (Div. 2) D. Lazy Student 贪心
    Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 连续LIS
    Codeforces Round #335 (Div. 2) A. Magic Spheres 模拟
    UVALive 6187 Never Wait for Weights 带权并查集
  • 原文地址:https://www.cnblogs.com/scottckt/p/2046772.html
Copyright © 2011-2022 走看看