Click here to Skip to main content
15,891,423 members
Home / Discussions / C#
   

C#

 
GeneralGet name of variable Pin
Stefan Troschuetz8-May-04 4:06
Stefan Troschuetz8-May-04 4:06 
GeneralRe: Get name of variable Pin
Hesham Amin8-May-04 4:13
Hesham Amin8-May-04 4:13 
GeneralRe: Get name of variable Pin
Stefan Troschuetz8-May-04 4:22
Stefan Troschuetz8-May-04 4:22 
GeneralRe: Get name of variable Pin
kayhustle8-May-04 16:03
kayhustle8-May-04 16:03 
GeneralRe: Get name of variable Pin
Stefan Troschuetz9-May-04 0:51
Stefan Troschuetz9-May-04 0:51 
GeneralRe: Get name of variable Pin
Heath Stewart8-May-04 19:29
protectorHeath Stewart8-May-04 19:29 
GeneralRe: Get name of variable Pin
Stefan Troschuetz9-May-04 0:42
Stefan Troschuetz9-May-04 0:42 
GeneralRe: Get name of variable Pin
Heath Stewart9-May-04 2:51
protectorHeath Stewart9-May-04 2:51 
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;
        name = attrAttrs[0].AttributeName;
      }
    }
 
    // Get optional XML comment string.
    XmlCommentAttribute[] comments = (XmlCommentAttributes[])
      field.GetCustomAttributes(typeof(XmlAttributeAttributes));
    if (comments != null && comments.Length > 0)
      comment = comments[0].Comment;
 
    // Now write all this to your XmlTextWriter or whatever you use.
    // ...
  }
}
Basically, try to handle the basic XML elements so that you can easily override how fields are named. If you ever change private member names (not typically a good idea - always keep serialization in the back of your head when developing) you can use XML attributes to keep their serialized form the same - thus not breaking your new code against old serialized formats.

Finally, use FieldInfo to get the value. This is very easy to use. Do not just use ToString on the returned object (if it's not null). First, try to get the TypeConverter for the Type like so:
TypeConverter converter = TypeDescriptor.GetConverter(field.FieldType);
Use that to convert to and from strings. If you just use ToString, you'll have to handle the parsing yourself. Some types provide their own parsing, but typically only the primatives and the methods often use different signatures. You can also use the ConvertTo class, but those only cover the primatives. Using a TypeConverter makes your code more robust.

 

Microsoft MVP, Visual C#
My Articles
Generaladd a property Pin
cristina_tudor8-May-04 2:48
cristina_tudor8-May-04 2:48 
GeneralRe: add a property Pin
Dave Kreskowiak8-May-04 3:17
mveDave Kreskowiak8-May-04 3:17 
GeneralC# Questions relating to TextBox Pin
Member 6910898-May-04 0:33
Member 6910898-May-04 0:33 
GeneralRe: C# Questions relating to TextBox Pin
Hesham Amin8-May-04 1:19
Hesham Amin8-May-04 1:19 
GeneralRe: C# Questions relating to TextBox Pin
surgeproof8-May-04 4:21
surgeproof8-May-04 4:21 
GeneralRe: C# Questions relating to TextBox Pin
kayhustle8-May-04 5:19
kayhustle8-May-04 5:19 
GeneralRe: C# Questions relating to TextBox Pin
Heath Stewart8-May-04 19:56
protectorHeath Stewart8-May-04 19:56 
Questionhow can i got the version infomation about the sqlsever? Pin
lowiq7-May-04 20:04
lowiq7-May-04 20:04 
AnswerRe: how can i got the version infomation about the sqlsever? Pin
Mazdak7-May-04 20:20
Mazdak7-May-04 20:20 
GeneralRe: how can i got the version infomation about the sqlsever? Pin
lowiq7-May-04 20:30
lowiq7-May-04 20:30 
GeneralRe: how can i got the version infomation about the sqlsever? Pin
Hesham Amin7-May-04 21:02
Hesham Amin7-May-04 21:02 
GeneralRe: how can i got the version infomation about the sqlsever? Pin
lowiq7-May-04 21:09
lowiq7-May-04 21:09 
GeneralRe: how can i got the version infomation about the sqlsever? Pin
Mazdak7-May-04 21:05
Mazdak7-May-04 21:05 
GeneralRe: how can i got the version infomation about the sqlsever? Pin
lowiq7-May-04 21:17
lowiq7-May-04 21:17 
GeneralDynamic Event Handling Pin
DustinMiles7-May-04 18:56
DustinMiles7-May-04 18:56 
GeneralRe: Dynamic Event Handling Pin
CWIZO7-May-04 20:57
CWIZO7-May-04 20:57 
GeneralRe: Dynamic Event Handling Pin
kayhustle8-May-04 5:14
kayhustle8-May-04 5:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.