Get name of variable
-
Hi! Is it possible to get the name of a variable as a string? For the following example I want to get the string "variable".
class Test { private int variable; }
I looked into Reflection namespace, but found nothing that fits well. The namespace provides classFieldInfo
but unfortunately the only way to get an instance are the methodsGetField
or theGetFields
. Both methods would more or less require that i already know the name of the variable. THX in advance! -
Hi! Is it possible to get the name of a variable as a string? For the following example I want to get the string "variable".
class Test { private int variable; }
I looked into Reflection namespace, but found nothing that fits well. The namespace provides classFieldInfo
but unfortunately the only way to get an instance are the methodsGetField
or theGetFields
. Both methods would more or less require that i already know the name of the variable. THX in advance!I believe there is no way... why do you need this ?
-
I believe there is no way... why do you need this ?
I'm storing some data of my class in a XML file that I read out later.
XmlTextWriter writer = new XmlTextWriter(this.configFilePath, this.configFileEncoding); writer.WriteStartDocument(); writer.WriteStartElement("Configuration"); writer.WriteElementString("Duration", this.duration.ToString()); writer.WriteEndElement();//Configuration writer.WriteEndDocument();
Thought it would be nice to get the string for thelocalName
param by determining the name of the variable instead of hardcoding it. -
I'm storing some data of my class in a XML file that I read out later.
XmlTextWriter writer = new XmlTextWriter(this.configFilePath, this.configFileEncoding); writer.WriteStartDocument(); writer.WriteStartElement("Configuration"); writer.WriteElementString("Duration", this.duration.ToString()); writer.WriteEndElement();//Configuration writer.WriteEndDocument();
Thought it would be nice to get the string for thelocalName
param by determining the name of the variable instead of hardcoding it.You can get the name. Let's say you have a class Test as follows,
class Test { private int variable; private int Variable { get{return variable;} } private int VariableFunc() { return variable; } }
Let says we use the FindNames class to get the names of the members of this class. Since we already know the class name we can do,using System.Reflection; class FindNames { public string[] GetNamesType() { Type mytype = typeof(Test); ArrayList names = new ArrayList(); PropertyInfo [] properties = mytype.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance); foreach(PropertyInfo pinfo in properties) { names.Add(pinfo.Name);//this will get the string "Variable" } FieldInfo [] fields= mytype.GetFields(BindingFlags.NonPublic|BindingFlags.Instance); foreach(FieldInfo finfo in fields) { names.Add(finfo.Name);//this will get the string "variable" } MethodInfo [] methods = mytype.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly); foreach(MethodInfo methinfo in properties) { names.Add(methinfo.Name);//this will get the string "VariableFunc" } MemberInfo [] members = mytype.GetMembers (BindingFlags.NonPublic|BindingFlags.Instance); foreach(MemberInfo minfo in members) { names.Add(pinfo.Name);//this will get the strings "Variable", "variable", //"VariableFunc", } return names.ToArray(typeof(string)) as string[]; }
Make sure you put in the BindingFlags.Instance or BindingFlags.Static or it will throw an exception. Play around with the BindingFlags depending on what type of members you want to get. You can find out many useful things about the member of the class including its type, name, etc. You can also dynamically get/set/invoke it if it is static, or get/set/invoke it on an instance that you pass in. P.S. if you don't want to hardcode the class name too then you can just take an instance of that class and do something like Type mytype=instance.GetType(); . . . Hope that helped. -
I'm storing some data of my class in a XML file that I read out later.
XmlTextWriter writer = new XmlTextWriter(this.configFilePath, this.configFileEncoding); writer.WriteStartDocument(); writer.WriteStartElement("Configuration"); writer.WriteElementString("Duration", this.duration.ToString()); writer.WriteEndElement();//Configuration writer.WriteEndDocument();
Thought it would be nice to get the string for thelocalName
param by determining the name of the variable instead of hardcoding it.Why don't you look into XML serialization? Read the documentatio for the classes in the
System.Xml.Serialization
namespace. TheXmlSerializer
already supports this and you can always override defauls using the attributes also in that namespace. Why do what's been already done for you in the .NET FCL?Microsoft MVP, Visual C# My Articles
-
Why don't you look into XML serialization? Read the documentatio for the classes in the
System.Xml.Serialization
namespace. TheXmlSerializer
already supports this and you can always override defauls using the attributes also in that namespace. Why do what's been already done for you in the .NET FCL?Microsoft MVP, Visual C# My Articles
THX for pointing me to this. I'm consistently astonished what else the .NET FCL has to offer. :) But I think it doesn't fit right for what I'm trying to do as I also want to write comments and private fields to the XML file. Anyway, I will keep the
XmlSerializer
in mind. Surely, I will need it sometimes. -
You can get the name. Let's say you have a class Test as follows,
class Test { private int variable; private int Variable { get{return variable;} } private int VariableFunc() { return variable; } }
Let says we use the FindNames class to get the names of the members of this class. Since we already know the class name we can do,using System.Reflection; class FindNames { public string[] GetNamesType() { Type mytype = typeof(Test); ArrayList names = new ArrayList(); PropertyInfo [] properties = mytype.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance); foreach(PropertyInfo pinfo in properties) { names.Add(pinfo.Name);//this will get the string "Variable" } FieldInfo [] fields= mytype.GetFields(BindingFlags.NonPublic|BindingFlags.Instance); foreach(FieldInfo finfo in fields) { names.Add(finfo.Name);//this will get the string "variable" } MethodInfo [] methods = mytype.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly); foreach(MethodInfo methinfo in properties) { names.Add(methinfo.Name);//this will get the string "VariableFunc" } MemberInfo [] members = mytype.GetMembers (BindingFlags.NonPublic|BindingFlags.Instance); foreach(MemberInfo minfo in members) { names.Add(pinfo.Name);//this will get the strings "Variable", "variable", //"VariableFunc", } return names.ToArray(typeof(string)) as string[]; }
Make sure you put in the BindingFlags.Instance or BindingFlags.Static or it will throw an exception. Play around with the BindingFlags depending on what type of members you want to get. You can find out many useful things about the member of the class including its type, name, etc. You can also dynamically get/set/invoke it if it is static, or get/set/invoke it on an instance that you pass in. P.S. if you don't want to hardcode the class name too then you can just take an instance of that class and do something like Type mytype=instance.GetType(); . . . Hope that helped.THX for your posting. I knew I could make it this way and searched for an easier way. Furthermore, what do you do if the class has more than one field? Which element of the names
ArrayList
do you use?FieldInfo [] fields= mytype.GetFields(BindingFlags.NonPublic|BindingFlags.Instance); foreach(FieldInfo finfo in fields) { names.Add(finfo.Name); }
-
THX for pointing me to this. I'm consistently astonished what else the .NET FCL has to offer. :) But I think it doesn't fit right for what I'm trying to do as I also want to write comments and private fields to the XML file. Anyway, I will keep the
XmlSerializer
in mind. Surely, I will need it sometimes.To learn what the FCL has - browse through the class library documentation. You don't have to memorize everything, but at least understand what's there and were. As you develop .NET applications and libraries, you should also see a pattern that Microsoft uses to try to maintain consistency. Like for provider classes: you'll typically see a static
Create
orCreateInstance
method. If you want to serialize your class in such a way, then you'll have to do it yourself. But take a lesson from both XML and runtime serialization. A good solution would be to create a new attribute - sayXmlCommentAttribute
- that stores the comment for a field:[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class XmlCommentAttribute : System.Attribute
{
private string comment;
public XmlCommentAttribute(string comment)
{
this.comment = comment;
}
public string Comment
{
get { return this.comment; }
}
}This would allow you to declaritivly define the comments for your fields:
[XmlRoot("myClass", Namespace="urn:myClass"]
public class MyClass
{
[XmlComment("This is parsed as a string.")]private string field1;
[XmlElement("myField2"), XmlComment("This is an int.")] private int field2;
// ...
}Noticed how I used the XML serialization attributes in places? This allows you to easily rename members or declare how things are stored, whether they should be elements or attributes, etc. If they aren't, you can have your serialization routine default to one or the other. Then just do something like this:
FieldInfo[] fields = myClass1.GetType().GetFields(
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (FieldInfo field in fields)
{
if (!field.IsNotSerialized) // At least honor the NotSerializedAttribute
{
string name = field.Name;
bool useElement = true;
string comment = null;
// Get optional element name from custom attributes.
XmlElementAttribute[] elemAttrs = (XmlElementAttribute[])
field.GetCustomAttributes(typeof(XmlElementAttribute))
if (elemAttrs != null && elemAttrs.Length > 0)
name = elemAttrs[0].ElementName;
else
{
// Get optional attribute name from custom attributes.
XmlAttributeAttribute[] attrAttrs = (XmlAttributeAttribute[])
field.GetCustomAttributes(typeof(XmlAttributeAttribute));
if (attrAttrs != null && attrAttrs.Length > 0)
{
useElement = false