C# Variable Name To String

This question already has an answer here:

  • Finding the variable name passed to a function 16 answers

  • 哈克方法,你可以通过Expression获得:

    string myVariable = string.Empty;
    Expression<Func<object>> expression = () => myVariable;
    
    string name = (expression.Body as MemberExpression).Member.Name;
    

    If you can access a variable by name, you already have its name, so type "MyVariable" .

    If you've reassigned it, this is not possible. Values don't keep a history of variable names somewhere.


    So in order to handle field to column mappings, I would suggest a Dictionary . Reflection is a possibility, but is needlessly complicated for a simple task like that.

    链接地址: http://www.djcxy.com/p/11836.html

    上一篇: 并行同步两个ListViews Android

    下一篇: C#将变量名称转换为字符串