Sunday 15 August 2010

attributes - C#: What's the Difference Between TypeDescriptor.GetAttributes() and GetType() .GetCustomAttributes? -


itemprop = "text">

Take these two code things:

  example.GetType () .GetCustomAttributes (true) . (Where object => is the object validation attribute);  

and

  TypeDescriptor.GetAttributes (example) .OfType & LT; ValidationAttribute & gt; ();  

looks like a class:

  [RequiredIfOtherPropertyIsNotEmpty ("state", "city", error message = ErrorDescription.CreateAccount_CityRequiredWithState)] [RequiredIfOtherPropertyIsNotEmpty (" State "," address 1 ", error message = ErrorDescription.CreateAccount_Address1RequiredWithState)] Public class ManagePostModel {...}  

Where RequiredIfOtherPropertyIsNotEmpty is a ValidationAttribute < / Code> and allow multilevel = true .

First gives one of the two attributes, returns the second.

What is the difference due to this? To return several instances of a

to:

Attribute AttributeCollection from

AttributeUsageAttribute.AllowMultiple , Attribute.TypeId property to answer your attribute.

Override should be asked "What" is this difference? ": The values ​​returned by TypeDescriptor can be extended to runtime, while this can not be in the type MSDN page I add more to the related. P>

TypeDescriptor handles many features if you do not need this type of order detail, and the way is a problem, you are probably Type.GetCustomAttributes .


No comments:

Post a Comment