mercredi 11 février 2015

.net and propertyinfo of property

i have some ressourcefile that defines some errorcodes. I'd like to use only strongtyping in my project. I also have to implement some Exception handling that prints the errors defined in the ressourcefile but also the name of the ressourceproperty. Think of it like this: Property-Name is Error1 (Type String) Property-Value is "This is the error"


Now i'd like to have some funktion that prints out: Error1: This is the error


Would it be possible to get the Property-Info of an info, without passing in the property-name as a string? That way i could use the name of the property and the value to output both data.


Something like:



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


Can use reflection, Extentions.


Any hint?






Aucun commentaire:

Enregistrer un commentaire