.NET:什么是属性? [英] .NET: What are attributes?

查看:153
本文介绍了.NET:什么是属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它们是什么,它​​们是什么好,如何创建我自己的属性?

What are they, what are they good for, and how do I create my own attributes?

推荐答案

元数据。关于你的对象/方法/属性数据。

Metadata. Data about your objects/methods/properties.

例如我可以声明叫做属性:DisplayOrder这样我就可以在应该出现在UI什么样的顺序性容易控制。那么我可以追加到一个类,并写一些GUI组件中提取的属性和适当排序的UI元素。

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;

    public DisplayWrapper(UnderlyingClass u)
    {
        underlyingObject = u;
    }

    [DisplayOrder(1)]
    public int SomeInt
    {
        get
        {
            return underlyingObject .SomeInt;
        }
    }

    [DisplayOrder(2)]
    public DateTime SomeDate
    {
        get
        {
            return underlyingObject .SomeDate;
        }
    }
}

从而保证SomeDate之前,我自定义GUI组件工作时SomeInt始终显示。

Thereby ensuring that SomeInt is always displayed before SomeDate when working with my custom GUI components.

不过,你会看到他们最常用的直接编码环境之外使用。例如Windows的设计使用它们广泛,所以它知道如何处理定制的对象。使用BrowsableAttribute像这样:

However, you'll see them most commonly used outside of the direct coding environment. For example the Windows Designer uses them extensively so it knows how to deal with custom made objects. Using the BrowsableAttribute like so:

[Browsable(false)]
public SomeCustomType DontShowThisInTheDesigner
{
    get{/*do something*/}
}

告诉设计师没有在设计时,例如在属性窗口中的可用属性列出此。

Tells the designer not to list this in the available properties in the Properties window at design time for example.

您的可以的也用他们的code代,pre-编译操作(如后夏普)或运行时的操作,如Reflection.Emit的。 例如,你可以写一点$ C $下进行分析的透明包装的每一个打电话给你的code使得和时间吧。你可以退出的时机通过你放在特定方法的属性。

You could also use them for code-generation, pre-compile operations (such as Post-Sharp) or run-time operations such as Reflection.Emit. For example, you could write a bit of code for profiling that transparently wrapped every single call your code makes and times it. You could "opt-out" of the timing via an attribute that you place on particular methods.

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);
    }
}

声明他们是容易的,只是做一个类,它继承属性。

Declaring them is easy, just make a class that inherits from Attribute.

public class DisplayOrderAttribute : Attribute
{
    private int order;

    public DisplayOrderAttribute(int order)
    {
        this.order = order;
    }

    public int Order
    {
        get { return order; }
    }
}

请记住,当你使用该属性可以省略后缀属性,编译器将添加为你。

And remember that when you use the attribute you can omit the suffix "attribute" the compiler will add that for you.

这篇关于.NET:什么是属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆