• 通过反射将变量值转为变量名本身ZZ


     

    这是.NET反射的一个有趣小例子:  通过反射将变量值转为变量名本身.

    当然要先添加命名空间:using System.Reflection;

    示例代码如下:

    复制代码

        class Program
        {
            string name = "strA";
            string strA = "strB";
            string strB = "Hello World~";
    
            static void Main(string[] args)
            {
                Program p = new Program();
                p.GetTypeValue();
    
                p.GetStrValue(p.name);
    
                p.SetStrValue(p.strA);
    
                Console.ReadKey();
            }
            //本文原址:http://www.cnblogs.com/Interkey/p/3460566.html
    
            /// <summary>
            /// 获取所有FieldInfo的值
            /// </summary>
            void GetTypeValue()
            {
                Console.WriteLine("Method: GetTypeValue");
                FieldInfo[] fis = this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
                foreach (FieldInfo fi in fis)
                {
                    Console.WriteLine(fi.Name + " 的值为:" + fi.GetValue(this).ToString());
                }
                Console.WriteLine();
            }
    
            /// <summary>
            /// 获取字符串str对应的变量名的变量值对应的变量值
            /// </summary>
            /// <param name="str"></param>
            void GetStrValue(string str)
            {
                Console.WriteLine("Method: GetStrValue");
                Type type = this.GetType();
    
                //获取字符串str对应的变量名的变量值
                Console.WriteLine(type.GetField(str, BindingFlags.NonPublic | BindingFlags.Instance).GetValue(this).ToString());
    
                Console.WriteLine(
                    type.GetField(
                        type.GetField(str, BindingFlags.NonPublic | BindingFlags.Instance).GetValue(this).ToString(),
                            BindingFlags.NonPublic | BindingFlags.Instance).GetValue(this).ToString()
                );
                Console.WriteLine();
            }
    
            /// <summary>
            /// 设置字符串str对应的变量名的变量值
            /// </summary>
            /// <param name="str"></param>
            void SetStrValue(string str)
            {
                Console.WriteLine("Method: SetStrValue");
    
                //赋值前输出
                Console.WriteLine(this.GetType().GetField(str, BindingFlags.NonPublic | BindingFlags.Instance).GetValue(this));
    
                //进行赋值操作
                this.GetType().GetField(str, BindingFlags.NonPublic | BindingFlags.Instance).SetValue(this, "Hello Interkey~");
    
                //赋值后输出
                Console.WriteLine(this.GetType().GetField(str, BindingFlags.NonPublic | BindingFlags.Instance).GetValue(this));
                Console.WriteLine();
            }
            //本文原址:http://www.cnblogs.com/Interkey/p/3460566.html
        }

    复制代码

    代码已经相当清晰,所以就不多做解释了~

    本文原址:http://www.cnblogs.com/Interkey/p/3460566.html

    .NET的反射可参考:反射概述 或 了解.NET中反射机制的使用与分析

    .NET反射虽然执行效率相对较慢,但在软件破解过程中,作用却非常大。这里就留给有心人了~

    本文的代码已上传到附件~

     

     

     

     

    很想实现这个功能,就是把类某一个变量名转换为字符串,
    直接使用ToString()是把该变量存放的数据转换为字符串的,
    那该如何将变量名本身转换为字符串呢?

    C#语言 128 次浏览 2011-01-01 19:48

    7 个回答

    好像映射和反射是关于这个方面的,你去了解下吧.

    2011-01-01 20:21 推荐: 0 次

    嗯,反射就是做这个的。LZ可以学下反射!

    2011-01-01 20:23 推荐: 0 次

    PropertyInfo[] peroperties = typeof(A).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
      foreach (PropertyInfo property in peroperties)
      {property .Name;
      property .PropertyType;  
    }

    2011-01-01 20:45 推荐: 0 次

    有个偷懒的笨办法,对类进行xml序列化,然后可以从xml文件中得到各个属性名称、值 。。。

    2011-01-01 22:34 推荐: 0 次

    利用反射,代码如下:

    C# code

    //获取类型 Type userInfoType = typeof(UserInfo); //获取属性 PropertyInfo[] propertys = userInfoType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); //循环获取属性 foreach (PropertyInfo pi in propertys) { //获取属性名称 string propertyName = pi.Name; }

  • 相关阅读:
    This counter can increment, decrement or skip ahead by an arbitrary amount
    LUT4/MUXF5/MUXF6 logic : Multiplexer 8:1
    synthesisable VHDL for a fixed ratio frequency divider
    Bucket Brigade FIFO SRL16E ( VHDL )
    srl16e fifo verilog
    DualPort Block RAM with Two Write Ports and Bytewide Write Enable in ReadFirst Mode
    Parametrilayze based on SRL16 shift register FIFO
    stm32 spi sdcard fatfs
    SPI bus master for System09 (2)
    SQLSERVER中的自旋锁
  • 原文地址:https://www.cnblogs.com/xpvincent/p/4140071.html
Copyright © 2020-2023  润新知