反射

msdn 对放射的说明:

  通过 System.Reflection 命名空间中的类以及 System.Type,您可以获取有关已加载的程序集和在其中定义的类型(如类、接口和值类型)的信息。             您也可以使用反射在运行时创建类型实例,以及调用和访问这些实例。 

  公共语言运行时加载器管理应用程序域,这些域在拥有相同应用程序范围的对象周围形成了确定边界。 这种管理包括将每个程序集加载到相应的应用程序域以及控制每个程序集中类型层次结构的内存布局。 

  程序集包含模块,而模块包含类型,类型又包含成员。            

  反射则提供了封装程序集、模块和类型的对象。  您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型。  然后,可以调用类型的方法或访问其字段和属性。

  1.程序集加载:

       JIT 在编译成IL时 会通过元数据定义表查找所有要加载的程序集。

          可以使用System.Reflection.Assembly的静态方法加载程序集。

 

Assembly {
public static Assembly Load(AssemblyName assemblyRef);
public static Assembly Load(String assemblyString);
public static Assembly LoadFrom(String path);
public static Assembly ReflectionOnlyLoadFrom(String assemblyFile);
public static Assembly ReflectionOnlyLoad(String assemblyString);
}

成员发现:

        Module :包含模块的程序集以及模块中的类等。           

       ConstructorInfo :构造函数的名称、参数、访问修饰符和实现 。 

     MethodInfo :方法的名称、返回类型、参数、访问修饰符和实现详细信息   

     FieldInfo :字段                    

    EventInfo :事件           

    PropertyInfo :属性                

     ParameterInfo:参数的名称、数据类型、参数是输入参数还是输出参数,以及参数在方法签名中的位置等。

   public class TypesAttribute : Attribute
    {
        public Type TypeC { set; get; }

        public TypesAttribute(Type t)
        {
            TypeC = t;
        }
    }

    public interface IAttText
    {
        string GetText(string ival);
    }
   
public class EdaControls : IAttText
    {
        public string GetText(string ival)
        {
                              return "";
        }
    }


  public class MidSmallVechile
    {
        [TypesAttribute(typeof(EdaControls))]
        public string Noop{ set; get; }

    }



  var s = from s1 in typeof (MidSmallVechile).GetProperties()
                where s1.GetCustomAttributes(typeof (TypesAttribute), false).Any()
                select s1;

 

posted @ 2014-09-19 15:13  坚硬的鸡蛋  阅读(149)  评论(0编辑  收藏  举报