.NET中的属性是什么,它们有什么好处,我如何创建自己的属性?
当前回答
元数据。关于对象/方法/属性的数据。
例如,我可以声明一个名为:DisplayOrder的属性,这样我就可以很容易地控制属性应该在UI中出现的顺序。然后,我可以将它附加到一个类中,并编写一些提取属性并对UI元素进行适当排序的GUI组件。
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;
}
}
}
因此,在使用自定义GUI组件时,确保SomeInt始终显示在SomeDate之前。
但是,您将看到它们最常用于直接编码环境之外。例如,Windows设计器广泛使用它们,因此它知道如何处理自定义对象。像这样使用BrowsableAttribute:
[Browsable(false)]
public SomeCustomType DontShowThisInTheDesigner
{
get{/*do something*/}
}
例如,告诉设计器在设计时不要在“属性”窗口的可用属性中列出此属性。
您还可以将它们用于代码生成、预编译操作(如Post-Sharp)或运行时操作(如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);
}
}
声明它们很简单,只需创建一个从Attribute继承的类。
public class DisplayOrderAttribute : Attribute
{
private int order;
public DisplayOrderAttribute(int order)
{
this.order = order;
}
public int Order
{
get { return order; }
}
}
记住,当你使用属性时,你可以省略后缀“attribute”,编译器会为你添加。
NOTE: Attributes don't do anything by themselves - there needs to be some other code that uses them. Sometimes that code has been written for you but sometimes you have to write it yourself. For example, the C# compiler cares about some and certain frameworks frameworks use some (e.g. NUnit looks for [TestFixture] on a class and [Test] on a test method when loading an assembly). So when creating your own custom attribute be aware that it will not impact the behaviour of your code at all. You'll need to write the other part that checks attributes (via reflection) and act on them.
其他回答
元数据。关于对象/方法/属性的数据。
例如,我可以声明一个名为:DisplayOrder的属性,这样我就可以很容易地控制属性应该在UI中出现的顺序。然后,我可以将它附加到一个类中,并编写一些提取属性并对UI元素进行适当排序的GUI组件。
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;
}
}
}
因此,在使用自定义GUI组件时,确保SomeInt始终显示在SomeDate之前。
但是,您将看到它们最常用于直接编码环境之外。例如,Windows设计器广泛使用它们,因此它知道如何处理自定义对象。像这样使用BrowsableAttribute:
[Browsable(false)]
public SomeCustomType DontShowThisInTheDesigner
{
get{/*do something*/}
}
例如,告诉设计器在设计时不要在“属性”窗口的可用属性中列出此属性。
您还可以将它们用于代码生成、预编译操作(如Post-Sharp)或运行时操作(如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);
}
}
声明它们很简单,只需创建一个从Attribute继承的类。
public class DisplayOrderAttribute : Attribute
{
private int order;
public DisplayOrderAttribute(int order)
{
this.order = order;
}
public int Order
{
get { return order; }
}
}
记住,当你使用属性时,你可以省略后缀“attribute”,编译器会为你添加。
NOTE: Attributes don't do anything by themselves - there needs to be some other code that uses them. Sometimes that code has been written for you but sometimes you have to write it yourself. For example, the C# compiler cares about some and certain frameworks frameworks use some (e.g. NUnit looks for [TestFixture] on a class and [Test] on a test method when loading an assembly). So when creating your own custom attribute be aware that it will not impact the behaviour of your code at all. You'll need to write the other part that checks attributes (via reflection) and act on them.
如前所述,属性相对容易创建。工作的另一部分是创建使用它的代码。在大多数情况下,您将在运行时使用反射来根据属性或其属性的存在来改变行为。在某些情况下,您将检查已编译代码上的属性以进行某种静态分析。例如,参数可能被标记为非空,分析工具可以将此作为提示。
使用这些属性并了解使用它们的适当场景是大部分工作。
属性也通常用于面向方面编程。要获得这样的示例,请查看PostSharp项目。
您可以使用自定义属性作为在子类中定义标记值的简单方法,而不必为每个子类反复编写相同的代码。我看到John Waters写的一个很好的简洁示例,介绍了如何在自己的代码中定义和使用自定义属性。
在http://msdn.microsoft.com/en-us/library/aa288454(VS.71).aspx上有一个教程
属性就像应用于类、方法或程序集的元数据。
它们适用于任何数量的事情(调试器可视化,标记为过时的东西,标记为可序列化的东西,列表是无限的)。
创建您自己的自定义是很容易的。从这里开始:
http://msdn.microsoft.com/en-us/library/sw480ze8 (VS.71) . aspx
推荐文章
- 转换为值类型'Int32'失败,因为物化值为空
- c#中有任何连接字符串解析器吗?
- 在Linq中转换int到字符串到实体的问题
- 是否可以动态编译和执行c#代码片段?
- 创建自定义MSBuild任务时,如何从c#代码获取当前项目目录?
- MSBuild路径
- c#和Java的主要区别是什么?
- 在c#中创建一个特定时区的DateTime
- .NET中的属性是什么?
- csproj文件中的“Service Include”是干什么用的?
- 如何使用try catch进行异常处理是最佳实践
- 替换字符串中第一次出现的模式
- .NET中字节的字面后缀?
- 如何处理AccessViolationException
- c#忽略证书错误?