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
[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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With