How to convert variable name to string?
-
How to convert variable name to string?
class SomeClass
{
public int SomeVariable;
};
string className = typeof(SomeClass).Name; // returns "SomeClass"
// need to obtain in the same way "SomeVariable"Чесноков
-
How to convert variable name to string?
class SomeClass
{
public int SomeVariable;
};
string className = typeof(SomeClass).Name; // returns "SomeClass"
// need to obtain in the same way "SomeVariable"Чесноков
if i understand you correct; What you need to look at is Boxing and Unboxing in C#. MSDN Boxing and Unboxing[^] boxing-and-unboxing-c#/[^]
-
How to convert variable name to string?
class SomeClass
{
public int SomeVariable;
};
string className = typeof(SomeClass).Name; // returns "SomeClass"
// need to obtain in the same way "SomeVariable"Чесноков
If you are using .NET 3 or above, you can use an expression tree to accomplish this. Here's an example of retrieving the field -
private static FieldInfo GetFieldInfo<T>(Expression<Func<T>> field)
{
FieldInfo fi = (field.Body as MemberExpression).Member as FieldInfo;
if (fi == null)
throw new ArgumentException("The field parameter should point to a valid field");
return fi;
}I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
How to convert variable name to string?
class SomeClass
{
public int SomeVariable;
};
string className = typeof(SomeClass).Name; // returns "SomeClass"
// need to obtain in the same way "SomeVariable"Чесноков
Can't be done in general as: - Values don't have names; - Most objects don't have names (major exception is WinForm Controls, they have a Name property which is heavily used by Visual Designer); - Variables have a name, obviously, inside the source file; the information is not preserved when compilation finishes. That is why Reflector uses mock names for everything that is a local variable. Hoowever data members such as your
SomeVariable
do have meta-data inside the class, so you can use reflection to enumerate them, or obtain the value of a variable whose name you know at run-time.Chesnokov Yuriy wrote:
string className = typeof(SomeClass).Name;
is just silly. Less typing is required to do
string className = "SomeVariable";
I suggest you describe the real problem you are trying to solve, not a detail along the wrong path you seem to have chosen; and/or read up on reflection. :)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
How to convert variable name to string?
class SomeClass
{
public int SomeVariable;
};
string className = typeof(SomeClass).Name; // returns "SomeClass"
// need to obtain in the same way "SomeVariable"Чесноков
Use reflection. I.e. PropertyInfo.Name for the variable names.
-
How to convert variable name to string?
class SomeClass
{
public int SomeVariable;
};
string className = typeof(SomeClass).Name; // returns "SomeClass"
// need to obtain in the same way "SomeVariable"Чесноков
-
If you are using .NET 3 or above, you can use an expression tree to accomplish this. Here's an example of retrieving the field -
private static FieldInfo GetFieldInfo<T>(Expression<Func<T>> field)
{
FieldInfo fi = (field.Body as MemberExpression).Member as FieldInfo;
if (fi == null)
throw new ArgumentException("The field parameter should point to a valid field");
return fi;
}I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
That little snippet deserves T&T status.
Never underestimate the power of human stupidity RAH
-
Can't be done in general as: - Values don't have names; - Most objects don't have names (major exception is WinForm Controls, they have a Name property which is heavily used by Visual Designer); - Variables have a name, obviously, inside the source file; the information is not preserved when compilation finishes. That is why Reflector uses mock names for everything that is a local variable. Hoowever data members such as your
SomeVariable
do have meta-data inside the class, so you can use reflection to enumerate them, or obtain the value of a variable whose name you know at run-time.Chesnokov Yuriy wrote:
string className = typeof(SomeClass).Name;
is just silly. Less typing is required to do
string className = "SomeVariable";
I suggest you describe the real problem you are trying to solve, not a detail along the wrong path you seem to have chosen; and/or read up on reflection. :)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Luc Pattyn wrote:
I suggest you describe the real problem you are trying to solve, not a detail along the wrong path you seem to have chosen; and/or read up on reflection.
It is in the question, given class variable as input convert it to string:
string name = FieldToString(SomeClass.SomeVariable); // name = "SomeVariable"
Чесноков