Question How Can You Get The Name Of A Class Property?

Mark Century

New member
Joined
Jun 24, 2011
Messages
4
Programming Experience
10+
How can I get a string with the name of a property at run time?

Obviously using GetProperty, as shown below, is NOT a solution since it requires knowing the property name to begin with:

Imports System.Reflection
Module Module1

Public Class Country
Public Property Capital As String
Public Property Population As Long
End Class

Public Sub ShowName()
Dim Nation As New Country
Dim PropertyName As String = Nation.GetType().GetProperty("Capital").Name
Console.WriteLine(PropertyName)
End Sub

End Module

What I want is to get the name from an instance (Nation.Capital for example) or from the type (Country.Capital).

Thanks

Mark
 

John,

Thank you for responding, however it appears you didn't even read my question. I went to trouble of providing an example of using the GetProperty Method in order to show what IS NOT an acceptable solution, since it requires you to supply the name of the property. Of course GetProperties is the same thing - except that it provides an array of PropertyInfo objects - and again you would need to provide the name of the property in order to locate the desired record.

If you or anyone else is aware of a way to get the name of a class property from an instance of that property without first supplying the name, please let me know. Thanks.

Mark
 
aware of a way to get the name of a class property from an instance of that property without first supplying the name

:confused:
Doesn't
do just that? This is from the example shown on that page, rewritten slightly

''Outputs:
'Properties of System.Type are:
'Capital
'Population

Module Module1
    Public Class Country
        Public Property Capital As String
        Public Property Population As Long
    End Class

    Sub main()
        Dim Nation As New Country

        Dim myPropertyInfo() As System.Reflection.PropertyInfo
        ' Get the properties of 'Type' class object.
        myPropertyInfo = Nation.GetType().GetProperties()

        Console.WriteLine("Properties of System.Type are:")

        For Each propertyInfo In myPropertyInfo
            Console.WriteLine(PropertyInfo.Name.ToString())
        Next
    End Sub
End Module
 
Thanks Flipped, but as I said in my previous comment, the only way to get the particular PropertyInfo element you want from the array that GetProperties() returns is by using the property name -- which isn't useful if the property name is what you're after.

I've researched this for several hours and concluded that this is a gaping hole in .NET reflection. There appears to be no way to get the name of a property from an instance of that property, nor is there any way to get the name of any variable.

Obviously you could store the name of a property or variable in a constant, but then your code would break the first time that property or variable was renamed.

Here's a simple example of where this would be useful:

When populating a DataGridView from a List(of Type) the column names are automatically assigned from the property names. Once the datagridview is populated, referencing a particular cell within a row requires knowing the column name. If this name is stored in a constant (or heaven forbid supplied as a literal) your code will break the first time you rename your property.
 
Last edited:
Reflection isn't designed for that purpose, it's for discovery of type system, ie finding information based on type, and invoking that on object instances of the type.
Referring to objects or their members by literals/variables in code is designed to resolve to values in memory at runtime, these only exist as symbols in the call stack then.
I agree it would make things simpler in some cases if you could do something like GetReflectedMemberInfo(InstanceOrType.Member), but it would require a change to compiler that may not even be possible.
 

Latest posts

Back
Top