.net 和 属性属性 的信息
.net and propertyinfo of property
我有一些定义了一些错误代码的资源文件。
我只想在我的项目中使用强类型。
我还必须执行一些异常处理,打印 ressourcefile 中定义的错误以及 ressource属性 的名称。
可以这样想:
属性-名称为 Error1(类型字符串)
属性-值为"This is the error"
现在我想要打印出一些功能:
Error1:这是错误
是否可以在不将 属性-name 作为字符串传递的情况下获取信息的 属性-Info?这样我就可以使用 属性 的名称和值来输出这两个数据。
类似于:
Public Class Form1
Public Class PropTestClass
Public Shared ReadOnly Property Prop1 As String
Get
Return "Test1"
End Get
End Property
Public Shared ReadOnly Property Prop2 As String
Get
Return "Test2"
End Get
End Property
End Class
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
TestPrint(PropTestClass.Prop1)
End Sub
Public Sub TestPrint(prop As Object)
Debug.Print(prop) '--> need to output: "Prop1: Test1"
End Sub
End Class
可以使用反射、扩展。
有什么提示吗?
通过使用 Linq-Expressions 得到这个工作
'declare all ressources in Meldungen
'part of class SmartException::Exception
Private m_Message As String
Public Overrides ReadOnly Property Message As String
Get
Return m_Message
End Get
End Property
Public Function SetResMessage(Of TProp)(expression As Expressions.Expression(Of Func(Of Meldungen, TProp)), ParamArray params As Object()) As MySmartException
Dim body As MemberExpression = TryCast(expression.Body, MemberExpression)
If body Is Nothing Then
Throw New ArgumentException("'expression' should be a member expression")
End If
RessourceString = GetType(Meldungen).GetProperty(body.Member.Name).GetValue(Nothing, Nothing).ToString
RessourceID = body.Member.Name
m_Message = Useful.StringFormat(RessourceID & " : " & RessourceString, params)
Return Me
End Function
'then use it like this
Throw New SmartException().SetResMessage(Function(f) Meldungen.ID_1084, "testval")
如果有人需要的话:)
我有一些定义了一些错误代码的资源文件。 我只想在我的项目中使用强类型。 我还必须执行一些异常处理,打印 ressourcefile 中定义的错误以及 ressource属性 的名称。 可以这样想: 属性-名称为 Error1(类型字符串) 属性-值为"This is the error"
现在我想要打印出一些功能: Error1:这是错误
是否可以在不将 属性-name 作为字符串传递的情况下获取信息的 属性-Info?这样我就可以使用 属性 的名称和值来输出这两个数据。
类似于:
Public Class Form1
Public Class PropTestClass
Public Shared ReadOnly Property Prop1 As String
Get
Return "Test1"
End Get
End Property
Public Shared ReadOnly Property Prop2 As String
Get
Return "Test2"
End Get
End Property
End Class
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
TestPrint(PropTestClass.Prop1)
End Sub
Public Sub TestPrint(prop As Object)
Debug.Print(prop) '--> need to output: "Prop1: Test1"
End Sub
End Class
可以使用反射、扩展。
有什么提示吗?
通过使用 Linq-Expressions 得到这个工作
'declare all ressources in Meldungen
'part of class SmartException::Exception
Private m_Message As String
Public Overrides ReadOnly Property Message As String
Get
Return m_Message
End Get
End Property
Public Function SetResMessage(Of TProp)(expression As Expressions.Expression(Of Func(Of Meldungen, TProp)), ParamArray params As Object()) As MySmartException
Dim body As MemberExpression = TryCast(expression.Body, MemberExpression)
If body Is Nothing Then
Throw New ArgumentException("'expression' should be a member expression")
End If
RessourceString = GetType(Meldungen).GetProperty(body.Member.Name).GetValue(Nothing, Nothing).ToString
RessourceID = body.Member.Name
m_Message = Useful.StringFormat(RessourceID & " : " & RessourceString, params)
Return Me
End Function
'then use it like this
Throw New SmartException().SetResMessage(Function(f) Meldungen.ID_1084, "testval")
如果有人需要的话:)