string myWeb="abc";
//我怎么得到这个字符串 "myWeb"
//而当这种情况时
string myWeb2="abc";
//我得到的字符串是 "myWeb2"//就是得到它本身的那个字符串  怎么得到?  //反射 GetType().Name  得到的字符串是  "string"

解决方案 »

  1.   

    Reflection只能获得类型的字段、属性、方法或事件,以及方法的参数、元数据等等,但局部变量的变量名是无法获取的
    即使获取出来,又有什么用呢?lz要它做什么?
      

  2.   

    没明白。
    你想得到变量名?貌似没意义,发布的软件都要混淆的,发布后变量名都变了,这样反射没意义。而且,貌似没办法。编译后的IL中是否还有这个myWeb?貌似没有了吧。IL中还有变量的名字么?
      

  3.   


    public   object   a,b; 
    private   void   button1_Click(object   sender,   System.EventArgs   e) 

    a   =   1; 
    b   =   1; 
    MessageBox.Show(GetName(ref   a)); 

    private   string   GetName(ref   object   x) 

    object   t   =   a;   
    if   (t   ==   x   )   return   "a "; 
    t   =   b;   
    if   (t   ==   x   )   return   "b "; 
    return   x.ToString(); } 
      

  4.   

    使用Lambda表达式遍历类字段,寻找和指定值相等的属性。我也就说说行,lambda我不太懂。
      

  5.   

    好吧。。既然你不到黄河心不死。告诉你个方法得到。。你通过Struct来实现。。大概如下。。struct MyStruct
            {
                public int MyProperty { get; set; }        }
           //在这里面给你的sting变量赋值。。
    在类中就可以得到属性名称了。。
    MyStruct aaa = new MyStruct();
    aaa.MyProperty;
      

  6.   

    http://www.programfan.com/club/showpost.asp?id=26241
      

  7.   

    现有的代码结构不允许改成struct了  只有一类 变量  string myWeb;  string myWeb2;  string myWeb3;....
      

  8.   


    hi,peter  你给的这个链接  我看不懂最后那段C++  ⊙﹏⊙b汗
      

  9.   


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;
    namespace GetPrivateMemberTestA
    {
        class myc
        {
            string name = "my name";
            string address = "my address";
            int age = 23;
            DateTime birthday = new DateTime(1986, 5, 18);        public override string ToString()
            {
                return "name = " + name + "\r\n"
                    + "address = " + address + "\r\n"
                    + "age = " + age + "\r\n"
                    + "birthday = " + birthday + "\r\n";
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                myc c = new myc();
                Console.WriteLine("ToString : \r\n-------------------------------------------------------------");
                Console.WriteLine(c.ToString());
                FieldInfo[] fields = c.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
                Console.WriteLine("FieldInfo[] : \r\n-------------------------------------------------------------");
                foreach (FieldInfo field in fields)
                {
                    Console.WriteLine(String.Format("{0} : {1}", field.Name, field.GetValue(c)));
                }
                Console.ReadKey();
            }
        }
    }输出:ToString :
    -------------------------------------------------------------
    name = my name
    address = my address
    age = 23
    birthday = 1986-5-18 0:00:00FieldInfo[] :
    -------------------------------------------------------------
    name : my name
    address : my address
    age : 23
    birthday : 1986-5-18 0:00:00
      

  10.   

    实在不行你建一个数据库。。把里面的String的name都存里面。。到时候你一个个去找吧。。不过我觉得反射应该可以。。你试试。。通过名称就能反射出来。。
      

  11.   

    参考:    public partial class Form1 : Form
        {
            public string str = "spp";
            public string spp = "very good";        public string myWeb = "abc";
            public string myWeb2 = "abc";        public Form1()
            {
                InitializeComponent();
                string str1 = GetName(2);  //str1="myWeb"
                string str2 = GetName(3);  //str2="myWeb2"
                string str3 = this.GetType().GetField(str).GetValue(this).ToString(); //str3="very good"
            }
            public string GetName(int i)  
            {
                System.Reflection.FieldInfo[] fields = this.GetType().GetFields();
                return fields[i].Name;
            }
        }