What does [ConfigurationProperty ("providers")] do?

I am reading this article about patren provider article. Please call me what this expression is:

[ConfigurationProperty("providers")] 

Actually I want to know what [] is? I also saw this line in web methods using []. What []? what is used there? I don’t even know to look for what I should call? Plz and help me.

thanks

+4
source share
2 answers

[Foo(bla)] is the syntax for the attribute β€” additional metadata about a certain type or member (or even the assembly itself, or even parameters). You can write your own attributes, for example, something like:

 public class ConfigurationPropertyAttribute : Attribute { public ConfigurationPropertyAttribute(string something) {...} } 

the name Attribute is displayed, so only [ConfigurationProperty] is required. The string "providers" used as an argument to the constructor, and you can also use property assignments, for example:

 [Foo(123, "abc", Bar = 123)] 

looks for the type FooAttribute or Foo , with a constructor that accepts int and string , and has the Bar property, which can be assigned an int .

Most attributes do nothing directly , but you can write code that checks attribute types (through reflection), which is a very convenient way of library code, knowing how to work with the type.

For instance:

 [XmlType("abc"), XmlRoot("abc")] public class MyType { [XmlAttribute("name")] public string UserName {get;set;} } 

this will reconfigure the XmlSerializer (which checks the above attributes) to serialize the type:

 <abc name="blah"/> 

where without attributes it would be:

 <MyType><UserName>blah</UserName></MyType> 
+4
source

If you are writing something to read the settings from the Internet or the .config application, you can create a configuration section. This is where ConfigurationProperty declared.

Check out http://msdn.microsoft.com/en-us/library/system.configuration.configurationpropertyattribute(v=VS.100).aspx

+1
source

All Articles