C# 最有用的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/144833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Most Useful Attributes
提问by wusher
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?
我知道属性非常有用。有一些预定义的,例如[Browsable(false)]
允许您在属性选项卡中隐藏属性。这是一个解释属性的好问题:什么是 .NET 中的属性?
What are the predefined attributes (and their namespace) you actually use in your projects?
您在项目中实际使用的预定义属性(及其命名空间)是什么?
回答by Gilligan
[Serializable]
is used all the time for serializing and deserializing objects to and from external data sources such as xml or from a remote server. More about it here.
[Serializable]
一直用于将对象序列化和反序列化到外部数据源(例如 xml)或来自远程服务器。更多关于它在这里。
回答by Shog9
[Flags]
is pretty handy. Syntactic sugar to be sure, but still rather nice.
[Flags]
很方便。语法糖可以肯定,但仍然相当不错。
[Flags]
enum SandwichStuff
{
Cheese = 1,
Pickles = 2,
Chips = 4,
Ham = 8,
Eggs = 16,
PeanutButter = 32,
Jam = 64
};
public Sandwich MakeSandwich(SandwichStuff stuff)
{
Console.WriteLine(stuff.ToString());
// ...
}
// ...
MakeSandwich(SandwichStuff.Cheese
| SandwichStuff.Ham
| SandwichStuff.PeanutButter);
// produces console output: "Cheese, Ham, PeanutButter"
Leppiepoints out something I hadn't realized, and which rather dampens my enthusiasm for this attribute: it does notinstruct the compiler to allow bit combinations as valid values for enumeration variables, the compiler allows this for enumerations regardless. My C++ background showing through... sigh
Leppie指出了一些我没有意识到的事情,这反而降低了我对这个属性的热情:它没有指示编译器允许位组合作为枚举变量的有效值,编译器无论如何都允许枚举。我的 C++ 背景显示通过...叹息
回答by FlySwat
If I were to do a code coverage crawl, I think these two would be top:
如果我要进行代码覆盖率爬网,我认为这两个是最重要的:
[Serializable]
[WebMethod]
回答by Chris Wenham
In Hofstadtian spirit, the [Attribute]
attribute is very useful, since it's how you create your own attributes. I've used attributes instead of interfaces to implement plugin systems, add descriptions to Enums, simulate multiple dispatch and other tricks.
在 Hofstadtian 精神中,[Attribute]
属性非常有用,因为它是您创建自己属性的方式。我使用属性而不是接口来实现插件系统、向枚举添加描述、模拟多分派和其他技巧。
回答by Dan Herbert
System.Obsolete
is one of the most useful attributes in the framework, in my opinion. The ability to raise a warning about code that should no longer be used is very useful. I love having a way to tell developers that something should no longer be used, as well as having a way to explain why and point to the better/new way of doing something.
System.Obsolete
在我看来,是框架中最有用的属性之一。对不应再使用的代码发出警告的功能非常有用。我喜欢有办法告诉开发人员不应再使用某些东西,也喜欢有办法解释原因并指出更好/新的做某事的方式。
The Conditional attribute
is pretty handy too for debug usage. It allows you to add methods in your code for debug purposes that won't get compiled when you build your solution for release.
这Conditional attribute
对于调试使用也非常方便。它允许您在代码中添加用于调试目的的方法,这些方法在您构建发布解决方案时不会被编译。
Then there are a lot of attributes specific to Web Controls that I find useful, but those are more specific and don't have any uses outside of the development of server controls from what I've found.
然后有很多特定于 Web 控件的属性我觉得很有用,但这些属性更具体,除了我发现的服务器控件开发之外没有任何用途。
回答by wprl
For what it's worth, here's a list of all .NET attributes. There are several hundred.
对于它的价值,这里是所有 .NET 属性的列表。有几百个。
I don't know about anyone else but I have some serious RTFM to do!
我不知道其他人,但我有一些严肃的 RTFM 要做!
回答by Lawrence Johnston
I've found [DefaultValue]
to be quite useful.
我发现[DefaultValue]
很有用。
回答by 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]
在调试期间将鼠标悬停在 Type 的实例上时,对于快速查看 Type 的自定义输出非常有帮助。例子:
[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 CacheDuration
property set can avoid unnecessary execution of the web service method.
另外值得一提的是[WebMethod]
,CacheDuration
设置属性的属性可以避免不必要的web服务方法的执行。
回答by Adrian Wible
回答by Blair Conrad
I like [DebuggerStepThrough]
from System.Diagnostics.
我喜欢[DebuggerStepThrough]
System.Diagnostics。
It's very handy for avoiding stepping into those one-line do-nothing methods or properties (if you're forced to work in an early .Net without automatic properties). Put the attribute on a short method or the getter or setter of a property, and you'll fly right by even when hitting "step into" in the debugger.
这对于避免进入那些单行无用的方法或属性非常方便(如果您被迫在没有自动属性的早期 .Net 中工作)。将属性放在一个简短的方法或属性的 getter 或 setter 上,即使在调试器中点击“step into”,你也会直接飞过去。