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.