1

Is it possible to create a function that returns the name of variable like .ToString() return the string value of a variable.

static class myExten
{
    public static string ToName(this object o)
    {
        ///some code that returns the name
    }
}

static void Main()
{
     string country = "India";
     string variableName = country.ToName();
}

Many thanks for your attention.

Edit As my question is being marked duplicate, I am trying to explain how my qestion is diffrent

I have a generic function that returns a bool value(given below)

    static public bool stateNameExists(string name)
    {
        return SQL_Functions.AlreadyExists("**state_name**", table, name);
    }


    public static bool AlreadyExists(string column, string table, string value)
    {
        string qry = "select count(" + column + ") from " + table + " where " + column + " = '"+value+"'";
        string result = execute_scaller(qry);

        try
        {
            if (Convert.ToInt16(result) > 0)
            {
                return true;
            }
        }
        catch
        {
            return false;
        }

        return false;
    }

Please pay attention at stateNameExists function. In this function I am hardcoding the column name "state_name"

I have a model class

class stateModel
{
   public int id [get; set;}
   public string state_name {get; set;}
}

I want to pass the name of column using model object like this SQL_Functions.AlreadyExists(obj.state_name.GetName(), table, name);

that's why I asked for an extension function.

P.S: I can not use c# 6.0 tricks here.

Note: If the question is still duplicate/already answered then please provide its link. Many and Many thanks.

Vikas Bansal
  • 10,662
  • 14
  • 58
  • 100

2 Answers2

1

in c# 6, you can use nameof operator, but not within an extension method

static class myExten
{
    public static string ToName(this object o)
    {
        return nameof(o);
    }
}

this will always returns "o", i.e. the name of the variable in this method

the best you can do is something such:

static void Main()
{
     string country = "India";
     string variableName = nameof(country);
}
Gian Paolo
  • 4,161
  • 4
  • 16
  • 34
1

If I get you right, you would need to do something like this:

PropertyHelper.GetName<stateModel>(x=>x.state_name)

and receive stringified name of property here: "state_name".

We use a property helper class for getting property names, you can try this implementation:

public class PropertyHelper
    {
        public static string GetName<T>(Expression<Func<T>> expression)
        {
            return GetName(expression.Body);
        }

        public static string GetName<T>(Expression<Func<T, object>> expression)
        {
            return GetName(expression.Body);
        }

        public static string GetName<T, TProperty>(Expression<Func<T, TProperty>> expression)
        {
            return GetName(expression.Body);
        }

        public static Type GetType<T>(Expression<Func<T, object>> expression)
        {
            return GetMemberExpression(expression.Body).Type;
        }

        public static Type GetType<T, TProperty>(Expression<Func<T, TProperty>> expression)
        {
            return GetMemberExpression(expression.Body).Type;
        }

        private static MemberExpression GetMemberExpression(Expression expression)
        {
            var getMemberExpression = expression as MemberExpression;

            if (getMemberExpression != null)
                return getMemberExpression;

            if (IsConversion(expression))
            {
                var unaryExpression = expression as UnaryExpression;

                if (unaryExpression != null)
                    return GetMemberExpression(unaryExpression.Operand);
            }

            return null;
        }

        private static string GetName(Expression expression)
        {
            return string.Join(".", GetNames(expression));
        }

        private static IEnumerable<string> GetNames(Expression expression)
        {
            var memberExpression = GetMemberExpression(expression);

            if (memberExpression == null)
                yield break;

            foreach (var memberName in GetNames(memberExpression.Expression))
                yield return memberName;

            yield return memberExpression.Member.Name;
        }

        private static bool IsConversion(Expression expression)
        {
            return (expression.NodeType == ExpressionType.Convert
                    || expression.NodeType == ExpressionType.ConvertChecked);
        }
    }
Red
  • 2,728
  • 1
  • 20
  • 22