Finding the variable name passed to a function

前端 未结 17 1977
广开言路
广开言路 2020-11-22 04:11

Let me use the following example to explain my question:

public string ExampleFunction(string Variable) {
    return something;
}

string WhatIsMyName = "         


        
17条回答
  •  故里飘歌
    2020-11-22 05:06

    Well Try this Utility class,

    public static class Utility
    {
        public static Tuple GetNameAndValue(Expression> sourceExpression)
        {
            Tuple result = null;
            Type type = typeof (TSource);
            Func> process = delegate(MemberExpression memberExpression)
                                                                        {
                                                                            ConstantExpression constantExpression = (ConstantExpression)memberExpression.Expression;
                                                                            var name = memberExpression.Member.Name;
                                                                            var value = ((FieldInfo)memberExpression.Member).GetValue(constantExpression.Value);
                                                                            return new Tuple(name, (TSource) value);
                                                                        };
    
            Expression exception = sourceExpression.Body;
            if (exception is MemberExpression)
            {
                result = process((MemberExpression)sourceExpression.Body);
            }
            else if (exception is UnaryExpression)
            {
                UnaryExpression unaryExpression = (UnaryExpression)sourceExpression.Body;
                result = process((MemberExpression)unaryExpression.Operand);
            }
            else
            {
                throw new Exception("Expression type unknown.");
            }
    
            return result;
        }
    
    
    }
    

    And User It Like

        /*ToDo : Test Result*/
        static void Main(string[] args)
        {
            /*Test : primivit types*/
            long maxNumber = 123123;
            Tuple longVariable = Utility.GetNameAndValue(() => maxNumber);
            string longVariableName = longVariable.Item1;
            long longVariableValue = longVariable.Item2;
    
            /*Test : user define types*/
            Person aPerson = new Person() { Id = "123", Name = "Roy" };
            Tuple personVariable = Utility.GetNameAndValue(() => aPerson);
            string personVariableName = personVariable.Item1;
            Person personVariableValue = personVariable.Item2;
    
            /*Test : anonymous types*/
            var ann = new { Id = "123", Name = "Roy" };
            var annVariable = Utility.GetNameAndValue(() => ann);
            string annVariableName = annVariable.Item1;
            var annVariableValue = annVariable.Item2;
    
            /*Test : Enum tyoes*/
            Active isActive = Active.Yes;
            Tuple isActiveVariable = Utility.GetNameAndValue(() => isActive);
            string isActiveVariableName = isActiveVariable.Item1;
            Active isActiveVariableValue = isActiveVariable.Item2;
        }
    

提交回复
热议问题