Qui*_*ome 144
元数据.有关您的对象/方法/属性的数据.
例如,我可能会声明一个名为:DisplayOrder的属性,因此我可以轻松控制属性应在UI中显示的顺序.然后我可以将它附加到一个类并编写一些GUI组件来提取属性并适当地对UI元素进行排序.
public class DisplayWrapper
{
private UnderlyingClass underlyingObject;
public DisplayWrapper(UnderlyingClass u)
{
underlyingObject = u;
}
[DisplayOrder(1)]
public int SomeInt
{
get
{
return underlyingObject .SomeInt;
}
}
[DisplayOrder(2)]
public DateTime SomeDate
{
get
{
return underlyingObject .SomeDate;
}
}
}
Run Code Online (Sandbox Code Playgroud)
从而确保在使用我的自定义GUI组件时,SomeDate始终显示在SomeDate之前.
但是,您会看到它们在直接编码环境之外最常用.例如,Windows Designer广泛使用它们,因此它知道如何处理自定义对象.像这样使用BrowsableAttribute:
[Browsable(false)]
public SomeCustomType DontShowThisInTheDesigner
{
get{/*do something*/}
}
Run Code Online (Sandbox Code Playgroud)
告诉设计者不要在设计时在"属性"窗口的可用属性中列出这个.
您可能还使用它们生成代码,预编译操作(如后夏普)或运行时操作如Reflection.Emit的.例如,您可以编写一些用于分析的代码,这些代码透明地包装您的代码所做的每一次调用并对其进行计时.您可以通过放置在特定方法上的属性"选择退出"时间.
public void SomeProfilingMethod(MethodInfo targetMethod, object target, params object[] args)
{
bool time = true;
foreach (Attribute a in target.GetCustomAttributes())
{
if (a.GetType() is NoTimingAttribute)
{
time = false;
break;
}
}
if (time)
{
StopWatch stopWatch = new StopWatch();
stopWatch.Start();
targetMethod.Invoke(target, args);
stopWatch.Stop();
HandleTimingOutput(targetMethod, stopWatch.Duration);
}
else
{
targetMethod.Invoke(target, args);
}
}
Run Code Online (Sandbox Code Playgroud)
声明它们很简单,只需创建一个继承自Attribute的类.
public class DisplayOrderAttribute : Attribute
{
private int order;
public DisplayOrderAttribute(int order)
{
this.order = order;
}
public int Order
{
get { return order; }
}
}
Run Code Online (Sandbox Code Playgroud)
请记住,当您使用该属性时,您可以省略编译器将为您添加的后缀"属性".
注意:属性本身不做任何事情 - 需要一些其他代码使用它们.有时代码是为您编写的,但有时您必须自己编写代码.例如,C#编译器关心某些框架并使用某些框架(例如,NUnit在类上查找[TestFixture],在加载程序集时在测试方法上查找[Test]).
因此,在创建自己的自定义属性时,请注意它根本不会影响代码的行为.您需要编写检查属性的其他部分(通过反射)并对其进行操作.
Dre*_*kes 36
很多人都回答,但到目前为止,没有人提到这...
属性与反射大量使用.反思已经很慢了.
这是非常值得的标记您的自定义属性为sealed班提高自己的运行时性能.
考虑使用放置这样一个属性的适当位置,并将属性(!)属性指示通过也是一个好主意AttributeUsage.可用属性用法列表可能会让您感到惊讶:
AttributeUsage属性是AttributeUsage属性签名的一部分也很酷.哇因为循环依赖!
[AttributeUsageAttribute(AttributeTargets.Class, Inherited = true)]
public sealed class AttributeUsageAttribute : Attribute
Run Code Online (Sandbox Code Playgroud)
Pat*_*son 13
属性是一种用于标记类的元数据.这通常在WinForms中用于隐藏工具栏中的控件,但可以在您自己的应用程序中实现,以使不同类的实例能够以特定方式运行.
首先创建一个属性:
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false, Inherited=true)]
public class SortOrderAttribute : Attribute
{
public int SortOrder { get; set; }
public SortOrderAttribute(int sortOrder)
{
this.SortOrder = sortOrder;
}
}
Run Code Online (Sandbox Code Playgroud)
所有属性类必须具有后缀"Attribute"才有效.
完成此操作后,创建一个使用该属性的类.
[SortOrder(23)]
public class MyClass
{
public MyClass()
{
}
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果您想了解更多有关此内容的信息,请随时查看MSDN,其中包含非常好的描述.
我希望这能帮到你!
属性是一个包含一些功能的类,您可以将这些功能应用于代码中的对象。要创建一个,创建一个继承自System.Attribute的类。
至于它们的优点……它们几乎有无限的用途。
http://www.codeproject.com/KB/cs/dotnetattributes.aspx
属性就像应用于类,方法或程序集的元数据。
它们对许多事物都有好处(调试器可视化,将事物标记为过时,将事物标记为可序列化,列表无穷)。
创建自己的自定义项很容易。从这里开始:
http://msdn.microsoft.com/zh-CN/library/sw480ze8(VS.71).aspx
在我目前正在从事的项目中,有一组各种口味的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;
};
Run Code Online (Sandbox Code Playgroud)
我将其应用于这样的类:
[ControlDescription ("Pie Chart", "Displays a pie chart")]
public ref class PieControl sealed : UserControl
{
// stuff
};
Run Code Online (Sandbox Code Playgroud)
这是以前的海报所说的。
要使用该属性,编辑器具有一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
注意:以上是C ++ / CLI,但转换为C#并不难(是的,我知道,C ++ / CLI是可憎的,但这是我必须使用的:-()
您可以将属性放在大多数事物上,并且有很多预定义的属性。上面提到的编辑器还在属性上寻找自定义属性,这些属性描述了属性以及如何对其进行编辑。
一旦有了完整的想法,您会想知道没有它们的生活。
| 归档时间: |
|
| 查看次数: |
64800 次 |
| 最近记录: |