Click here to Skip to main content
15,912,294 members

Survey Results

Do you use Attributes in your .NET development?   [Edit]

Survey period: 17 Oct 2011 to 24 Oct 2011

Attributes provide a neat way to hide the plumbing. Do you use and/or create attributes yourself?

OptionVotes% 
Extensively626.31
Often17017.31
Occasionally27928.41
Rarely19419.76
Never787.94
I am unfamiliar with Attributes in .NET19920.26



 
GeneralIt's awesome when you figure its power Pin
Fabio Franco21-Oct-11 2:17
professionalFabio Franco21-Oct-11 2:17 
GeneralRe: It's awesome when you figure its power Pin
Rakesh Meel23-Oct-11 19:48
professionalRakesh Meel23-Oct-11 19:48 
GeneralYes, When in my first week of new contract Pin
raju melveetilpurayil20-Oct-11 11:54
professionalraju melveetilpurayil20-Oct-11 11:54 
GeneralAttributes are the undead citizens of the .net code world. Pin
dave.dolan20-Oct-11 8:50
dave.dolan20-Oct-11 8:50 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
Fabio Franco21-Oct-11 5:40
professionalFabio Franco21-Oct-11 5:40 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
dave.dolan21-Oct-11 5:46
dave.dolan21-Oct-11 5:46 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
Fabio Franco21-Oct-11 6:07
professionalFabio Franco21-Oct-11 6:07 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
dave.dolan21-Oct-11 6:11
dave.dolan21-Oct-11 6:11 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
Fabio Franco21-Oct-11 6:21
professionalFabio Franco21-Oct-11 6:21 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
dave.dolan21-Oct-11 6:27
dave.dolan21-Oct-11 6:27 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
Fabio Franco21-Oct-11 6:43
professionalFabio Franco21-Oct-11 6:43 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
dave.dolan21-Oct-11 6:45
dave.dolan21-Oct-11 6:45 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
Fabio Franco21-Oct-11 6:49
professionalFabio Franco21-Oct-11 6:49 
GeneralRe: Attributes are the undead citizens of the .net code world. Pin
PIEBALDconsult21-Oct-11 9:38
mvePIEBALDconsult21-Oct-11 9:38 
dave.dolan wrote:
like configuration is supposed to be


So write Attributes that read the configuration. Cool | :cool:


(Darn, now I have to contrive an example... Sigh | :sigh: )



[Later] And so I wrote...

using Prop=System.Collections.Generic.KeyValuePair<System.Reflection.PropertyInfo,object> ;
using Proptionary=System.Collections.Generic.Dictionary<System.Reflection.PropertyInfo,object> ;

public class ConfigValueAttribute : System.ComponentModel.DefaultValueAttribute
{
  private static readonly System.Collections.Generic.Dictionary<string,System.Xml.XmlDocument> doc ;
  private static readonly System.Collections.Generic.Dictionary<System.Type,Proptionary> typ ;

  static ConfigValueAttribute
  (
  )
  {
    doc = new System.Collections.Generic.Dictionary<string,System.Xml.XmlDocument>() ;
    typ = new System.Collections.Generic.Dictionary<System.Type,Proptionary>() ;
    return ;
  }

  public ConfigValueAttribute
  (
    string ConfigFilePath
  ,
    string XPath
  )
  : base
  (
    null
  )
  {
    string fil = System.Environment.ExpandEnvironmentVariables ( ConfigFilePath ) ;

    if ( !doc.ContainsKey ( fil ) )
    {
      System.Xml.XmlDocument temp = new System.Xml.XmlDocument() ;
      temp.Load ( fil ) ;
      doc [ fil ] = temp ;
    }

    System.Xml.XmlNode nod = doc [ fil ].SelectSingleNode ( XPath ) ;

    if ( nod is System.Xml.XmlAttribute )
    {
      this.SetValue ( nod.Value ) ;
    }
    else
    {
      this.SetValue ( nod.InnerText ) ;
    }
    return ;
  }

  public static T
  Apply<T>
  (
    T Source
  )
  {
    System.Type t = typeof(T) ;

    if ( !typ.ContainsKey ( t ) )
    {
      Proptionary props = new Proptionary() ;

      foreach ( System.Reflection.PropertyInfo pi in t.GetProperties() )
      {
        foreach ( ConfigValueAttribute att in pi.GetCustomAttributes ( typeof(ConfigValueAttribute) , false ) )
        {
          props [ pi ] = att.Value ;
        }
      }
      typ [ t ] = props ;
    }

    foreach ( Prop prop in typ [ t ] )
    {
      try
      {
        prop.Key.SetValue
        (
          Source
        ,
          System.Convert.ChangeType
          (
            prop.Value
          ,
            prop.Key.PropertyType
          )
        ,
          null
        ) ;
      }
      catch
      {
        /* Leave it at the default */
      }
    }
    return ( Source ) ;
  }
}

public class SomeClass
{
  [ConfigValueAttribute("ConfigValueAttributeTest.xml","/ConfigValueAttributeTest/StringValue")]
  public string StringValue { get ; private set ; }

  [ConfigValueAttribute("ConfigValueAttributeTest.xml","/ConfigValueAttributeTest/StringValue/@AttValue")]
  public string AttValue    { get ; private set ; }

  [ConfigValueAttribute("ConfigValueAttributeTest.xml","/ConfigValueAttributeTest/NumValue")]
  public int    IntValue    { get ; private set ; }

  [ConfigValueAttribute("ConfigValueAttributeTest.xml","/ConfigValueAttributeTest/NumValue")]
  public double DoubleValue { get ; private set ; }

  public SomeClass
  (
  )
  {
    ConfigValueAttribute.Apply ( this ) ;

    return ;
  }
}




SomeClass x = new SomeClass() ;

System.Console.WriteLine ( "StringValue = {0}" , x.StringValue ) ;
System.Console.WriteLine ( "AttValue    = {0}" , x.AttValue    ) ;
System.Console.WriteLine ( "IntValue    = {0}" , x.IntValue    ) ;
System.Console.WriteLine ( "DoubleValue = {0}" , x.DoubleValue ) ;




<ConfigValueAttributeTest>
  <StringValue AttValue='ATAT' >Test</StringValue>
  <NumValue>3.14</NumValue>
</ConfigValueAttributeTest>


modified 22-Oct-11 10:27am.

GeneralReflection + Attributes = FUN Pin
milkplus18-Oct-11 5:36
milkplus18-Oct-11 5:36 
GeneralRe: Reflection + Attributes = FUN Pin
Fabio Franco21-Oct-11 5:35
professionalFabio Franco21-Oct-11 5:35 
GeneralMostly with enumerations Pin
PIEBALDconsult17-Oct-11 18:13
mvePIEBALDconsult17-Oct-11 18:13 
GeneralRe: Mostly with enumerations Pin
ThatsAlok17-Oct-11 20:37
ThatsAlok17-Oct-11 20:37 
Generalwhat?? Pin
Dennis E White17-Oct-11 10:26
professionalDennis E White17-Oct-11 10:26 
GeneralRe: what?? Pin
CDP180217-Oct-11 11:27
CDP180217-Oct-11 11:27 
GeneralRe: what?? Pin
Dennis E White17-Oct-11 11:35
professionalDennis E White17-Oct-11 11:35 
GeneralRe: what?? Pin
Stonkie17-Oct-11 11:54
Stonkie17-Oct-11 11:54 
GeneralRe: what?? Pin
Edward Steward3-Dec-11 23:15
Edward Steward3-Dec-11 23:15 
GeneralRe: what?? Pin
PIEBALDconsult17-Oct-11 18:09
mvePIEBALDconsult17-Oct-11 18:09 
GeneralRe: what?? Pin
Keith Barrow18-Oct-11 0:35
professionalKeith Barrow18-Oct-11 0:35 

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.