VB.net(.net 3.5)自定义属性和类型。GetCustomAttribute()始终为1

VB.net(.net 3.5)自定义属性和类型。GetCustomAttribute()始终为1,vb.net,getcustomattributes,Vb.net,Getcustomattributes,在以下场景中,我应该如何a)定义自定义属性和b)获得所述自定义属性分配 场景:我们想定义一个自定义属性(custatib1),由基类(myBase)的继承类(myClassFoo)使用。然后基类将检索分配给继承实例的自定义属性,然后执行一些操作 问题:每当在基类中针对继承的类调用GetCustomAttribute时,GetCustomAttibutes方法只返回一个结果(System.Runtime.CompilerServices.CompilerGlobalScopeAttribute)

在以下场景中,我应该如何a)定义自定义属性和b)获得所述自定义属性分配

场景:我们想定义一个自定义属性(custatib1),由基类(myBase)的继承类(myClassFoo)使用。然后基类将检索分配给继承实例的自定义属性,然后执行一些操作

问题:每当在基类中针对继承的类调用GetCustomAttribute时,GetCustomAttibutes方法只返回一个结果(System.Runtime.CompilerServices.CompilerGlobalScopeAttribute)

以下是如何定义属性/类:

属性: (文件:myFoo.vb)

继承类: (文件:myBar2.vb)

'-----------------------------------------------------------------
名称空间Foo.Bar
_
公共类myClassFoo
”“还有其他很酷的东西
公共子继承方法()
端接头
末级
结束命名空间
'-----------------------------------------------------------------

感谢您的帮助

代码中唯一的问题是您没有从基类继承。否则,它将正常工作

以下是我用来测试的示例,包括拼写错误的继承和更正:

Public Class Form1

    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Dim oClass = New myClassFoo
        oClass.someCoolCode()

    End Sub

End Class

<System.AttributeUsage(AttributeTargets.Class, AllowMultiple:=True, inherited:=False)> _
Public Class custAttrib1
    Inherits System.Attribute

    Public Property myAttributeInfo As String
End Class

Public Class MyBaseClass

    Public Sub someCoolCode()
        Dim myInstanceType As Type = Me.GetType()
        Dim custAttribs As Object() = myInstanceType.GetCustomAttributes(False)

        '-- at this time, only content of custAttribs array is System.Runtime.CompilerServices.CompilerGlobalScopeAttribute)

        Debug.WriteLine(DirectCast(custAttribs(0), custAttrib1).myAttributeInfo)
    End Sub

End Class

<custAttrib1(myAttributeInfo:="Coding is fun")> _
Public Class myClassFoo
    Inherits MyBaseClass

    '-- other cool stuff goes there
    Public Sub inheritedMethod()
    End Sub
End Class
公共类表单1
次新
'设计器需要此调用。
初始化组件()
'在InitializeComponent()调用之后添加任何初始化。
Dim oClass=新myClassFoo
oClass.someCoolCode()
端接头
末级
_
公共类custAttrib1
继承System.Attribute
公共属性myAttributeInfo作为字符串
末级
公共类MyBaseClass
公共子代码()
Dim myInstanceType As Type=Me.GetType()
Dim custAttribs As Object()=myInstanceType.GetCustomAttributes(False)
'--此时,custAttribs数组的唯一内容是System.Runtime.CompilerServices.CompilerGlobalScopeAttribute)
Debug.WriteLine(DirectCast(custAttribs(0),custAttrib1.myAttributeInfo)
端接头
末级
_
公共类myClassFoo
继承MyBaseClass
”“还有其他很酷的东西
公共子继承方法()
端接头
末级

代码中唯一的问题是没有从基类继承。否则,它将正常工作

以下是我用来测试的示例,包括拼写错误的继承和更正:

Public Class Form1

    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Dim oClass = New myClassFoo
        oClass.someCoolCode()

    End Sub

End Class

<System.AttributeUsage(AttributeTargets.Class, AllowMultiple:=True, inherited:=False)> _
Public Class custAttrib1
    Inherits System.Attribute

    Public Property myAttributeInfo As String
End Class

Public Class MyBaseClass

    Public Sub someCoolCode()
        Dim myInstanceType As Type = Me.GetType()
        Dim custAttribs As Object() = myInstanceType.GetCustomAttributes(False)

        '-- at this time, only content of custAttribs array is System.Runtime.CompilerServices.CompilerGlobalScopeAttribute)

        Debug.WriteLine(DirectCast(custAttribs(0), custAttrib1).myAttributeInfo)
    End Sub

End Class

<custAttrib1(myAttributeInfo:="Coding is fun")> _
Public Class myClassFoo
    Inherits MyBaseClass

    '-- other cool stuff goes there
    Public Sub inheritedMethod()
    End Sub
End Class
公共类表单1
次新
'设计器需要此调用。
初始化组件()
'在InitializeComponent()调用之后添加任何初始化。
Dim oClass=新myClassFoo
oClass.someCoolCode()
端接头
末级
_
公共类custAttrib1
继承System.Attribute
公共属性myAttributeInfo作为字符串
末级
公共类MyBaseClass
公共子代码()
Dim myInstanceType As Type=Me.GetType()
Dim custAttribs As Object()=myInstanceType.GetCustomAttributes(False)
'--此时,custAttribs数组的唯一内容是System.Runtime.CompilerServices.CompilerGlobalScopeAttribute)
Debug.WriteLine(DirectCast(custAttribs(0),custAttrib1.myAttributeInfo)
端接头
末级
_
公共类myClassFoo
继承MyBaseClass
”“还有其他很酷的东西
公共子继承方法()
端接头
末级

您的基类没有用属性修饰,因此它不会出现在
Me
中。您的
myclassFoo
没有从mybase继承属性。如果向属性添加构造函数,可以简化属性值的设置。另外,属性的命名约定不符合最佳做法。考虑将您的属性类命名如下:<代码>公共类CuStRuTB1Atgult请注意结尾处的代码<代码>属性< /代码>。使用该属性时,可以禁用该选项。另外,类名应该大写。谢谢你的建议!您的基类没有用属性修饰,因此它不会出现在
Me
中。您的
myclassFoo
没有从mybase继承属性。如果向属性添加构造函数,可以简化属性值的设置。另外,属性的命名约定不符合最佳做法。考虑将您的属性类命名如下:<代码>公共类CuStRuTB1Atgult请注意结尾处的代码<代码>属性< /代码>。使用该属性时,可以禁用该选项。另外,类名应该大写。谢谢你的建议!我的另一个问题是myBaseClass继承自System.Web.UI.Page。我不知道ASP.NET页面的实例类型与关联的*.ASP.vb文件中的类定义不同。相反,我需要查看页面的“BaseType”,例如me.GetType().BaseType。一旦我有了正确类型的类型引用,我就能够获得必要的属性列表。我不知道ASP.NET页面的实例类型与关联的*.ASP.vb文件中的类定义不同。相反,我需要查看页面的“BaseType”,例如me.GetType().BaseType。一旦我有了正确类型的类型引用,我就能够得到必要的属性列表。
'-----------------------------------------------------------------
Namespace Foo.Bar
<Foo.CustomAttributes.custAttrib1(myAttributeInfo:="Coding if fun")> _
Public Class myClassFoo 
      '-- other cool stuff goes there
    Public Sub inheritedMethod()
    End Sub
End Class
End Namespace
'-----------------------------------------------------------------
Public Class Form1

    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Dim oClass = New myClassFoo
        oClass.someCoolCode()

    End Sub

End Class

<System.AttributeUsage(AttributeTargets.Class, AllowMultiple:=True, inherited:=False)> _
Public Class custAttrib1
    Inherits System.Attribute

    Public Property myAttributeInfo As String
End Class

Public Class MyBaseClass

    Public Sub someCoolCode()
        Dim myInstanceType As Type = Me.GetType()
        Dim custAttribs As Object() = myInstanceType.GetCustomAttributes(False)

        '-- at this time, only content of custAttribs array is System.Runtime.CompilerServices.CompilerGlobalScopeAttribute)

        Debug.WriteLine(DirectCast(custAttribs(0), custAttrib1).myAttributeInfo)
    End Sub

End Class

<custAttrib1(myAttributeInfo:="Coding is fun")> _
Public Class myClassFoo
    Inherits MyBaseClass

    '-- other cool stuff goes there
    Public Sub inheritedMethod()
    End Sub
End Class