zoukankan      html  css  js  c++  java
  • 反射

    1. 强类型程序集

    为什么要强类型名称? 主要是防止不同公司起的名字不同,所以要强类型(秘钥)

    强类型名字=基本信息,版本号,秘钥等信息等

    1)检查程序集是否强命名:   sn –v  xxx.dll         

    2)生成秘钥:  sn –p keyPair.snk

    假设类库名字为  common  ,想强类型化,则在  项目的  Properties/AssemblyInfo.cs  最后加上  一句

    //手动添加秘钥   不加这句就会提醒  不是具有强名称的程序集
    [assembly:AssemblyKeyFile("keyPair.snk")]      //将上面生成的 秘钥 keyPair.snk 放到文件夹中

    不是强命名的话,被别的程序引用之后编译会出错,  微软的system 都强类型都强类型了,所以直接访问。

    2.通过反射获取程序集的信息

    1)获取除了托管程序集提供的 类似System.xx,之外的dll

    static void Main(string[] args)
    {
    Console.WriteLine("反射-获取System.IO.BinaryReader 中的公共成员,不包括父类的成员");

    Assembly a = Assembly.LoadFile(@"C:\Users\ipod\Documents\Visual Studio 2010\Projects\反射\反射\反射\bin\Debug\common.dll"); //system就不用这个了


    Type MyType = a.GetType("common.helper");



    MemberInfo[] Memberinfoarray = MyType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);

    Console.WriteLine("反射-获取System.IO.BinaryReader公共成员数量:{0}",Memberinfoarray.Length);
    Console.WriteLine("反射程序集name",MyType.FullName);


    for (int i = 0; i < Memberinfoarray.Length; i++)
    {

    Console.WriteLine("member[{0}]:{1}",i+1, Memberinfoarray[i].Name+",");



    }

    Console.ReadLine();





    }

    反射获取程序集信息,如果是IsContained方法,则调用此方法。MethodInfo  a,  a.invoke

    static void Main(string[] args)
    {
    Console.WriteLine("反射-获取System.IO.BinaryReader 中的公共成员,不包括父类的成员");

    Assembly a = Assembly.LoadFile(@"C:\Users\ipod\Documents\Visual Studio 2010\Projects\反射\反射\反射\bin\Debug\common.dll");


    Type MyType = a.GetType("common.helper");



    MemberInfo[] Memberinfoarray = MyType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);

    Console.WriteLine("反射-获取System.IO.BinaryReader公共成员数量:{0}",Memberinfoarray.Length);
    Console.WriteLine("反射程序集name",MyType.FullName);


    for (int i = 0; i < Memberinfoarray.Length; i++)
    {

    Console.WriteLine("member[{0}]:{1}",i+1, Memberinfoarray[i].Name+",");

    if (Memberinfoarray[i].Name == "IsContained")
    {
    Console.WriteLine("如果是 IsContained方法,则 调用方法");


    MethodInfo b =MyType.GetMethod(Memberinfoarray[i].Name);
    Console.WriteLine(b.Invoke(null, new object[] { "dd", "dd" })); //第一个参数为null,因为是静态方法
    return;
    }

    }


    Console.ReadLine();





    }

    2)获取 System.IO 中的成员信息

    static void Main(string[] args)
    {
    Console.WriteLine("反射-获取System.IO.BinaryReader 中的公共成员,不包括父类的成员");




    Type MyType = a.GetType("System.IO");



    MemberInfo[] Memberinfoarray = MyType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);

    Console.WriteLine("反射-获取System.IO.BinaryReader公共成员数量:{0}",Memberinfoarray.Length);
    Console.WriteLine("反射程序集name",MyType.FullName);


    for (int i = 0; i < Memberinfoarray.Length; i++)
    {

    Console.WriteLine("member[{0}]:{1}",i+1, Memberinfoarray[i].Name+",");



    }

    Console.ReadLine();





    }

  • 相关阅读:
    PostgreSQL 数据库备份与恢复 pd_dump pg_restore
    给数据库减负的7个技巧
    添加二级域名 配置多站点 阿里云
    PHP 循环输出多重数组元素
    CentOS 7 yum Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile
    CentOS 7 配置 nginx php-fpm 详细教程
    Centos7 卸载 Nginx 并重新安装 Nginx
    centos7 取消自动锁屏
    composer 安装以及使用教程
    MacBook Pro设置外接显示器竖屏显示 切换主显示器
  • 原文地址:https://www.cnblogs.com/StudyLife/p/2951918.html
Copyright © 2011-2022 走看看