Vb.net 更复杂的属性参数
我想使用Vb.net 更复杂的属性参数,vb.net,attributes,Vb.net,Attributes,我想使用System.Attribute为我的类创建一些元数据。 我需要的是在获取名称时使用编译器的优势,而不是使用strings。但我的方法无法实现,因为attibutes不接受复杂的参数,就像我在这里写的: Public Class Foo Public Property Id as Integer <Attribute(AttributeInputA.Create(of Bar)(Function(item) item.Name))> Public Prope
System.Attribute
为我的类创建一些元数据。
我需要的是在获取名称时使用编译器的优势,而不是使用string
s。但我的方法无法实现,因为attibutes不接受复杂的参数,就像我在这里写的:
Public Class Foo
Public Property Id as Integer
<Attribute(AttributeInputA.Create(of Bar)(Function(item) item.Name))>
Public Property Name as String
End Class
Public Class Bar
Public Property Name as String
End Class
<AttributeUsage(AttributeTargets.Property Or
AttributeTargets.Field,
AllowMultiple:=True)>
Public Class Attribute
Public Sub New(attibuteInputA as AttributeInputA)
End Sub
End Class
Public Class AttributeInputA
Public Share Function Create(Of T)(System.Linq.Expressions.Expression(Of Func(Of Object , T)) AS AttributeInputA
End Function
End Class
公共类Foo
作为整数的公共属性Id
作为字符串的公共属性名称
末级
公共类酒吧
作为字符串的公共属性名称
末级
公共类属性
公共子新建(attibuteInputA作为AttributeInputA)
端接头
末级
公共类属性inputa
公共共享函数Create(Of T)(System.Linq.Expressions.Expression(Of Func(Of Object,T))作为AttributeInputA
端函数
末级
我想知道的是,还有其他方法可以实现这一点吗?您可以将字符串反序列化为任何您想要的内容。在这种情况下,您可以将代码用作CodeDom(需要编译器可用)或Reflection.Emit(不需要但需要IL)的源代码
不管是哪种方式,没有太多的回报似乎会带来很多麻烦。我会编写我需要的特定案例,然后使用字典查找或类来查找。实际上我不想使用字符串,我希望编译器为我检查一些先决条件,并抱怨我在编译时可能出现的一些错误。你别无选择,只能使用str对于属性,该字符串表示什么取决于您。