我想使用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
我想知道的是,有没有其他方法可以做到这一点?
发布于 2019-01-26 12:33:22
你可以将字符串反序列化为你想要的任何东西。在这种情况下,您可以使用代码作为CodeDom (需要编译器可用)或Reflection.Emit (不需要,但需要IL)的源代码。
无论哪种方式,似乎没有太多的回报,似乎有很多麻烦。我会编写我需要的特定案例,然后使用字典查找或类来查找它。
https://stackoverflow.com/questions/54377901
复制相似问题