C# Variable Name To String
This question already has an answer here:
哈克方法,你可以通过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.
下一篇: C#将变量名称转换为字符串
