.NET自定义属性的属性?属性、自定义、NET

2023-09-04 12:23:08 作者:霸气菇凉前途无量丶

编辑: 我最好改一下: 如何转移GET-执行类属性到的/使用自定义属性?(我加instantation瓦尔(类名,propertyName的)的属性,但是我宁愿有这些ofcourse自动获取。)

 公共类CustomClass
    < CustomAttributeClass(类名:=CustomClass,属性名=SomeProperty)> _
    公共财产SomeProperty()为String
        获得()作为字符串
            //这个实现应该由属性类来处理
        最终获取

        设置(BYVAL值作为字符串)
            Me._someProperty =价值
        结束设定
    高端物业
末级
 

老问题:

我想创建一个自定义属性属性类。我可以创建一个从属性派生的类,而标记带有属性的属性,但哪里何去何从?

我有一个资料库,我可以快速获得基于属性值的数据。我想概括在该属性的属性的行为,但我不知道如何从这里走...任何帮助会接受!

 公共类CustomDataAttribute:继承属性
    私人_name作为字符串

    公共子新(BYVAL名作为字符串)
        Me.Name =名称
    结束小组

    属性名称()作为字符串
        得到
            返回_name
        最终获取
        设置(BYVAL值作为字符串)
            Me._name =价值
        结束设定
    高端物业
末级


公共类CustomClass
    < CustomDataAttribute(名称:=的CustomField)> _
    公共财产的CustomField()
    高端物业
末级
 

解决方案 如何利用log4Net自定义属性配置功能记录完整的日志信息

您将不得不使用反射来发现的属性。在你的情况,你会从PropertyInfo.GetCustomAttributes()得到它。

使用属性的比较困难的部分是要找到一个合适的执行模型实际使用它们。就像一个编译器,一个设计师或序列化对象的类就是一个明显的例子。属性的可用性迅速从那里螺旋下降。它几乎总是错误的选择,当您尝试使用其中虚拟财产的实际需要的属性。检索属性值的幅度高于检索属性值更昂贵的价格昂贵,很多订单。使用它们仅在反射code在人类的时间运行(如编译器),或者相比,利益或开销(常见于任何类型的I / O操作)的成本是微不足道的。

EDIT: I'd better rephrase: How can I shift the GET-implementation of a Class property to a / using a custom attribute? (I've added instantation vars (classname, propertyname) to the attribute, however I'd rather have these automatically fetched ofcourse.)

Public Class CustomClass
    <CustomAttributeClass(ClassName:="CustomClass", PropertyName = "SomeProperty")> _
    Public Property SomeProperty() as String
        Get() as String
            //This implementation should be handled by the attribute class
        End Get

        Set(Byval value as String)
            Me._someProperty = value
        End Set
    End Property
End Class

Old question:

I want to create a custom property attribute for classes. I can create a class derived from Attribute, and 'mark' the property with the attribute, but where to go from here?

I have a repository where I can quickly get data based on the attributes values. I would like to generalize the behaviour of the property in the attribute but I don't know how to go from here... Any help would be greatly accepted!

Public Class CustomDataAttribute : Inherits Attribute
    Private _name As String

    Public Sub New(ByVal name As String)
        Me.Name = name
    End Sub

    Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            Me._name = value
        End Set
    End Property
End Class


Public Class CustomClass
    <CustomDataAttribute(Name:="CustomField")> _ 
    Public Property CustomField()
    End Property
End Class

解决方案

You will have to use Reflection to discover the attribute. In your case, you'll get it from PropertyInfo.GetCustomAttributes().

The harder part of using attributes is to find a suitable execution model to actually use them. Something like a compiler, a designer or a class that serializes objects is an obvious one. The usability of attributes quickly spirals down from there. It is almost always the wrong choice when you try to use an attribute where a virtual property is actually needed. Retrieving attribute values is very expensive, many orders of magnitude more expensive than retrieving a property value. Use them only when the reflection code runs at human time (like compilers) or when the cost is insignificant compared to the benefit or the overhead (common in any kind of I/O operation).