can not do this in the method GetName

解决方案 »

  1.   

    int a;  //因为不知道你的a的类型,我假设是int
    public int GetName(string x)
    {
     select case x
     {
       case: 'a'
        return a;break;
       case: 'b'
        return b;break;
       default:
        return null;break;
      }
    }
    //在转c#阶段,语法有问题的话大家包含一下。理解意思就行了。
      

  2.   

    没看清题目。知道变量,得到变量名啊。枚举行么?
    public string GetName(ref object x)
    {
     object t = a; 
     if (t == x ) return "a";
     object t = b; 
     if (t == x ) return "b";
    }
      

  3.   

    改一下,第二此的不用定义,
    object t = b; 
    改为:
    t = b;
      

  4.   

    试了一下。可以得到变量名:
    测试例子:
    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(); }
    结果正确。
      

  5.   

    又试试,发现上面方法有缺陷。如果变量没有赋值,都是null,就区分不出来了。没法通过比较引用。给你顶顶。