.net-attributes

Most Useful Attributes [closed]

筅森魡賤 提交于 2019-11-27 05:42:50
I know that attributes are extremely useful. There are some predefined ones such as [Browsable(false)] which allows you to hide properties in the properties tab. Here is a good question explaining attributes: What are attributes in .NET? What are the predefined attributes (and their namespace) you actually use in your projects? Vivek [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;

Can I fail to deserialize with XmlSerializer in C# if an element is not found?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 05:16:23
I am using XmlSerializer to write and read an object to xml in C#. I currently use the attributes XmlElement and XmlIgnore to manipulate the serialization of the object. If my xml file is missing an xml element that I require, my object still deserializes (xml -> object) just fine. How do I indicate (preferably via Attributes) that a certain field is "required"? Here is a sample method of what I am using currently: [XmlElement(ElementName="numberOfWidgets")] public int NumberThatIsRequired { set ...; get ...; } My ideal solution would be to add something like an XmlRequired attribute. Also, is

displayname attribute vs display attribute

旧城冷巷雨未停 提交于 2019-11-26 19:40:20
What is difference between DisplayName attribute and Display attribute in ASP.NET MVC? Spock They both give you the same results but the key difference I see is that you cannot specify a ResourceType in DisplayName attribute. For an example in MVC 2, you had to subclass the DisplayName attribute to provide resource via localization. Display attribute (new in MVC3 and .NET4) supports ResourceType overload as an "out of the box" property. Darin Dimitrov DisplayName sets the DisplayName in the model metadata. For example: [DisplayName("foo")] public string MyProperty { get; set; } and if you use

What does [STAThread] do?

帅比萌擦擦* 提交于 2019-11-26 14:02:48
I am learning C# 3.5 and I want to know what [STAThread] does in our programs? Noldorin 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 is single-threaded apartment. This attribute must be present on the entry point of any application that uses Windows Forms; if it is omitted, the

Most Useful Attributes [closed]

半城伤御伤魂 提交于 2019-11-26 12:48:56
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago . I know that attributes are extremely useful. There are some predefined ones such as [Browsable(false)] which allows you to hide properties in the properties tab. Here is a good question explaining attributes: What are attributes in .NET? What are the predefined attributes

Can I fail to deserialize with XmlSerializer in C# if an element is not found?

感情迁移 提交于 2019-11-26 11:29:37
问题 I am using XmlSerializer to write and read an object to xml in C#. I currently use the attributes XmlElement and XmlIgnore to manipulate the serialization of the object. If my xml file is missing an xml element that I require, my object still deserializes (xml -> object) just fine. How do I indicate (preferably via Attributes) that a certain field is \"required\"? Here is a sample method of what I am using currently: [XmlElement(ElementName=\"numberOfWidgets\")] public int

Why does C# forbid generic attribute types?

≯℡__Kan透↙ 提交于 2019-11-26 01:29:04
问题 This causes a compile-time exception: public sealed class ValidatesAttribute<T> : Attribute { } [Validates<string>] public static class StringValidation { } I realize C# does not support generic attributes. However, after much Googling, I can\'t seem to find the reason. Does anyone know why generic types cannot derive from Attribute ? Any theories? 回答1: Well, I can't answer why it's not available, but I can confirm that it's not a CLI issue. The CLI spec doesn't mention it (as far as I can

What are attributes in .NET?

烈酒焚心 提交于 2019-11-26 00:39:35
问题 What are attributes in .NET, what are they good for, and how do I create my own attributes? 回答1: 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 { private UnderlyingClass underlyingObject;

Find a private field with Reflection?

心不动则不痛 提交于 2019-11-25 23:49:06
问题 Given this class class Foo { // Want to find _bar with reflection [SomeAttribute] private string _bar; public string BigBar { get { return this._bar; } } } I want to find the private item _bar that I will mark with a attribute. Is that possible? I have done this with properties where I have looked for an attribute, but never a private member field. What are the binding flags that I need to set to get the private fields? 回答1: Use BindingFlags.NonPublic and BindingFlags.Instance flags FieldInfo

Getting attributes of Enum&#39;s value

為{幸葍}努か 提交于 2019-11-25 22:28:19
问题 I would like to know if it is possible to get attributes of the enum values and not of the enum itself? For example, suppose I have the following enum: using System.ComponentModel; // for DescriptionAttribute enum FunkyAttributesEnum { [Description(\"Name With Spaces1\")] NameWithoutSpaces1, [Description(\"Name With Spaces2\")] NameWithoutSpaces2 } What I want is given the enum type, produce 2-tuples of enum string value and its description. Value was easy: Array values = System.Enum