Is there a way for a property to access its own name and type at runtime using reflection? I want to access this info without hard coding the name or index of the property in the class.
Simple Example Code:
Private ReadOnly Property MyProperyName() As String
Get
Console.WriteLine((Get Current Property Info).Type.ToString)
Console.WriteLine((Get Current Property Info).Name)
Return ""
End Get
End Property
Expected output:
System.String
MyPropertyName
You can use a
StackTraceto get the current method:If you can assume that you’re in a property, then you can strip the “get_” off of the front of the method name:
And use
ReturnTypefor the property type: