Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Get name of variable

Get name of variable

Scheduled Pinned Locked Moved C#
csstutorialquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stefan Troschuetz
    wrote on last edited by
    #1

    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 class FieldInfo but unfortunately the only way to get an instance are the methods GetField or the GetFields. Both methods would more or less require that i already know the name of the variable. THX in advance!

    H 1 Reply Last reply
    0
    • S Stefan Troschuetz

      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 class FieldInfo but unfortunately the only way to get an instance are the methods GetField or the GetFields. Both methods would more or less require that i already know the name of the variable. THX in advance!

      H Offline
      H Offline
      Hesham Amin
      wrote on last edited by
      #2

      I believe there is no way... why do you need this ?

      S 1 Reply Last reply
      0
      • H Hesham Amin

        I believe there is no way... why do you need this ?

        S Offline
        S Offline
        Stefan Troschuetz
        wrote on last edited by
        #3

        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 the localName param by determining the name of the variable instead of hardcoding it.

        K H 2 Replies Last reply
        0
        • S Stefan Troschuetz

          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 the localName param by determining the name of the variable instead of hardcoding it.

          K Offline
          K Offline
          kayhustle
          wrote on last edited by
          #4

          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.

          S 1 Reply Last reply
          0
          • S Stefan Troschuetz

            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 the localName param by determining the name of the variable instead of hardcoding it.

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            Why don't you look into XML serialization? Read the documentatio for the classes in the System.Xml.Serialization namespace. The XmlSerializer 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

            S 1 Reply Last reply
            0
            • H Heath Stewart

              Why don't you look into XML serialization? Read the documentatio for the classes in the System.Xml.Serialization namespace. The XmlSerializer 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

              S Offline
              S Offline
              Stefan Troschuetz
              wrote on last edited by
              #6

              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.

              H 1 Reply Last reply
              0
              • K kayhustle

                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.

                S Offline
                S Offline
                Stefan Troschuetz
                wrote on last edited by
                #7

                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); }

                1 Reply Last reply
                0
                • S Stefan Troschuetz

                  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.

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #8

                  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 or CreateInstance 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 - say XmlCommentAttribute - 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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups