.NET中的属性是什么,它们有什么好处,我如何创建自己的属性?
当前回答
属性也通常用于面向方面编程。要获得这样的示例,请查看PostSharp项目。
其他回答
属性是一个类,它包含一些可以应用于代码中的对象的功能。要创建一个类,请创建一个继承自System.Attribute的类。
至于它们有什么好处……它们的用途几乎是无限的。
http://www.codeproject.com/KB/cs/dotnetattributes.aspx
属性就像应用于类、方法或程序集的元数据。
它们适用于任何数量的事情(调试器可视化,标记为过时的东西,标记为可序列化的东西,列表是无限的)。
创建您自己的自定义是很容易的。从这里开始:
http://msdn.microsoft.com/en-us/library/sw480ze8 (VS.71) . aspx
如前所述,属性相对容易创建。工作的另一部分是创建使用它的代码。在大多数情况下,您将在运行时使用反射来根据属性或其属性的存在来改变行为。在某些情况下,您将检查已编译代码上的属性以进行某种静态分析。例如,参数可能被标记为非空,分析工具可以将此作为提示。
使用这些属性并了解使用它们的适当场景是大部分工作。
属性是一种标记类的元数据。这通常用于WinForms中,例如在工具栏中隐藏控件,但可以在您自己的应用程序中实现,以使不同类的实例以特定的方式运行。
首先创建一个属性:
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false, Inherited=true)]
public class SortOrderAttribute : Attribute
{
public int SortOrder { get; set; }
public SortOrderAttribute(int sortOrder)
{
this.SortOrder = sortOrder;
}
}
所有属性类必须有后缀“attribute”才有效。 完成此操作后,创建一个使用该属性的类。
[SortOrder(23)]
public class MyClass
{
public MyClass()
{
}
}
现在你可以检查一个特定类的SortOrderAttribute(如果它有的话),方法如下:
public class MyInvestigatorClass
{
public void InvestigateTheAttribute()
{
// Get the type object for the class that is using
// the attribute.
Type type = typeof(MyClass);
// Get all custom attributes for the type.
object[] attributes = type.GetCustomAttributes(
typeof(SortOrderAttribute), true);
// Now let's make sure that we got at least one attribute.
if (attributes != null && attributes.Length > 0)
{
// Get the first attribute in the list of custom attributes
// that is of the type "SortOrderAttribute". This should only
// be one since we said "AllowMultiple=false".
SortOrderAttribute attribute =
attributes[0] as SortOrderAttribute;
// Now we can get the sort order for the class "MyClass".
int sortOrder = attribute.SortOrder;
}
}
}
如果你想了解更多这方面的信息,你可以查看MSDN,那里有很好的描述。 我希望这对你有所帮助!
在我目前从事的项目中,有一组不同风格的UI对象和一个编辑器来组装这些对象以创建用于主应用程序的页面,有点像DevStudio中的表单设计器。这些对象存在于它们自己的程序集中,每个对象都是从UserControl派生的类,并具有自定义属性。该属性的定义如下:
[AttributeUsage (AttributeTargets::Class)]
public ref class ControlDescriptionAttribute : Attribute
{
public:
ControlDescriptionAttribute (String ^name, String ^description) :
_name (name),
_description (description)
{
}
property String ^Name
{
String ^get () { return _name; }
}
property String ^Description
{
String ^get () { return _description; }
}
private:
String
^ _name,
^ _description;
};
我把它应用到这样一个课上:
[ControlDescription ("Pie Chart", "Displays a pie chart")]
public ref class PieControl sealed : UserControl
{
// stuff
};
之前的海报也是这么说的。
要使用该属性,编辑器有一个Generic::List <Type>,其中包含控件类型。有一个列表框,用户可以从该列表框中拖放到该页上,以创建控件的实例。为了填充列表框,我获得控件的ControlDescriptionAttribute,并在列表中填写一个条目:
// done for each control type
array <Object ^>
// get all the custom attributes
^attributes = controltype->GetCustomAttributes (true);
Type
// this is the one we're interested in
^attributetype = ECMMainPageDisplay::ControlDescriptionAttribute::typeid;
// iterate over the custom attributes
for each (Object ^attribute in attributes)
{
if (attributetype->IsInstanceOfType (attribute))
{
ECMMainPageDisplay::ControlDescriptionAttribute
^description = safe_cast <ECMMainPageDisplay::ControlDescriptionAttribute ^> (attribute);
// get the name and description and create an entry in the list
ListViewItem
^item = gcnew ListViewItem (description->Name);
item->Tag = controltype->Name;
item->SubItems->Add (description->Description);
mcontrols->Items->Add (item);
break;
}
}
注:上面是c++ /CLI,但是转换成c#并不难 (是的,我知道,c++ /CLI是一个讨厌的东西,但它是我必须使用的:-()
你可以把属性放在大多数东西上,有一整套预定义的属性。上面提到的编辑器还会在描述属性以及如何编辑属性的属性上查找自定义属性。
一旦你明白了它们的全部含义,你就会想,没有它们你是怎么生活的。
推荐文章
- 不能在lambda表达式中使用ref或out参数
- c# int到字节[]
- 如何跟踪log4net问题
- 将WPF组合框绑定到自定义列表
- foreach vs somlist . foreach (){}
- 为什么try{…}最后{…}好;尝试{…} catch{}坏?
- c# 8用多种情况切换表达式,结果相同
- 在没有事件源注册的情况下写入Windows应用程序事件日志
- 并发HashSet<T>在。net框架?
- 从控制器内获得控制器和动作名称?
- _ViewStart的位置和方式。CSHTML布局文件链接?
- 新建T()
- 如何将枚举绑定到WPF中的组合框控件?
- 拒绝访问该路径
- Visual Studio - Resx文件默认“内部”为“公共”