Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am reading this article on provider patren. Kindly guide me what do this statement:

 [ConfigurationProperty("providers")]

Actually I want to learn what is [] ? I also saw such a line on web methods with []. What are [] ? what is there use ? I am even not aware to search what I should name it ? plz guide and help me.

Thanks

like image 761
user576510 Avatar asked May 04 '11 06:05

user576510


2 Answers

[Foo(bla)] is the syntax for an attribute - additional metadata about some type or member (or even the assembly itself; or indeed parameters). You can write your own attributes, for example that one is something like:

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

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

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

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

Most attributes don't do anything directly, but you can write code that inspects types for attributes (via reflection), which is a very convenient way of library code knowing how to work with a type.

For example:

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

this reconfigures XmlSerializer (which checks for the above attributes) to serialize the type as:

<abc name="blah"/>

where without the attributes it would be:

<MyType><UserName>blah</UserName></MyType>
like image 100
Marc Gravell Avatar answered Nov 20 '22 08:11

Marc Gravell


If you are writing something to read settings from the web or app .config, you can create a configuration section. This is where declaring the ConfigurationProperty comes in.

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

like image 41
iain Avatar answered Nov 20 '22 07:11

iain