Most Useful Attributes [closed]

[DebuggerDisplay] can be really helpful to quickly see customized output of a Type when you mouse over the instance of the Type during debugging. example: [DebuggerDisplay(“FirstName={FirstName}, LastName={LastName}”)] class Customer { public string FirstName; public string LastName; } This is how it should look in the debugger: Also, it is worth mentioning that [WebMethod] attribute with … Read more

displayname attribute vs display attribute

DisplayName sets the DisplayName in the model metadata. For example: [DisplayName(“foo”)] public string MyProperty { get; set; } and if you use in your view the following: @Html.LabelFor(x => x.MyProperty) it would generate: <label for=”MyProperty”>foo</label> Display does the same, but also allows you to set other metadata properties such as Name, Description, … Brad Wilson … Read more

What does [STAThread] do?

The STAThreadAttribute is essentially a requirement for the Windows message pump to communicate with COM components. Although core Windows Forms does not use COM, many components of the OS such as system dialogs do use this technology. MSDN explains the reason in slightly more detail: STAThreadAttribute indicates that the COM threading model for the application … Read more

What are attributes in .NET?

Metadata. Data about your objects/methods/properties. For example I might declare an Attribute called: DisplayOrder so I can easily control in what order properties should appear in the UI. I could then append it to a class and write some GUI components that extract the attributes and order the UI elements appropriately. public class DisplayWrapper { … Read more

Getting attributes of Enum’s value

This should do what you need. try { var enumType = typeof(FunkyAttributesEnum); var memberInfos = enumType.GetMember(FunkyAttributesEnum.NameWithoutSpaces1.ToString()); var enumValueMemberInfo = memberInfos.FirstOrDefault(m => m.DeclaringType == enumType); var valueAttributes = enumValueMemberInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); var description = ((DescriptionAttribute)valueAttributes[0]).Description; } catch { return FunkyAttributesEnum.NameWithoutSpaces1.ToString() }