Get Name of property (Type Safe)

  • Thread starter Thread starter Thomas Jespersen
  • Start date Start date
T

Thomas Jespersen

Hello

How can I read the name of a property?

The following pseudo which doesn't work will illustrate what I'm after.

Class MyClasss
Public MyClassID As Guid
Public FullName As String
End Class


Dim myClass as MyClass

'I don't know how or which reflection procdure to use ?Propperty? is the
pseudo code ;-)
myThirdPartyExtraGridControl.MyClassIDGridColumn.FieldName =
CType(myClass.MyClassID, System.Reflection.?Property?).GetName
myThirdPartyExtraGridControl.FullNameGridColumn.FieldName =
CType(myClass.FullnName, System.Reflection.?Property?).GetName


The reason I want this is because we have a Third Party Grid Control, which
can take a collection of objects. And if FieldName is a valid property it
will display the value. I can use the following code (which works). But this
is not type safe... that is if I change the name of MyClassID to ID I don't
get a compile error but a runtime error.

myThirdPartyExtraGridControl.MyClassIDGridColumn.FieldName = "MyClassID"
myThirdPartyExtraGridControl.MyClassIDGridColumn.FieldName = "FullName"

Any help?

Best. regards.
Thomas
 
Hi Thomas,

Thanks for posting in the community.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to use reflection to
retrieve an fields name of an class instance.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

Type Members
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemtypememberstopic.asp

Here is the code snippet which will write all the field in a class instance.

Imports System.Reflection
Module Module1
<STAThread()> _
Public Sub Main()
Dim o As New MyClasss
Dim fis As FieldInfo() = o.GetType().GetFields()
For Each fi As FieldInfo In fis
Console.WriteLine(fi.Name.ToString())
Next
'Write out MyClassID FullName
End Sub
Class MyClasss
Public MyClassID As Guid
Public FullName As String
End Class
End Module

Please apply my suggestion above and let me know if it helps resolve your
problem.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello Peter

Thanks for answering.

You understood me correct but what you suggestion doesn't help me. I don't
what to iterate through the properties... because this will never give me a
compile error if I change a property name.

Maybe I should try explaining what I'm after in another way. I still have
this class:

Class MyClasss
Public MyClassID As Guid
Public FullName As String
End Class

How do I get a text representation of a property name, in such a way, that I
will get a compile error if I change the name of the property? That is I
what the string "FullName".

Why do I want that? Because we have a huge project and the third-party grid
component we use can fill the grid by data binding to the properties. For
each column in the grid we write the name of the property in plain text.
Because it is just text and not a reference to the property, we do NOT get a
compile error if we change the property name.

Normally when we a change a property name or a method name we get compile
errors, and this way we can se all places in the code where we refer to the
property, method, class etc.

Is this possible?

Best. regards.
Thomas
 
Hi Thomas,

Thanks for your quickly reply!

I am sorry that from my experience we can not do that so far.

As a workaround, you can utilize the Find function buildin .NET IDE to mark
all the places where you need to change the field.

e.g.
You can Press Ctrl+F to open the Find dialog check the Match Case and Match
Whole Word, input the MyClassID into the Find What: textbox and Click Mark
All this will toggle the bookmarks on all the place match the find
condition.

After that you can use the Ctrl + K, Ctrl + N to go to the next bookmark
and use the Ctrl + K , Ctrl + P to back to the previous BookMark.
You may also use the the submenu of Edit/BookMarks to do the stuff.

Thank for your understanding.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello Peter

Well... I guess I'll have to live with that.

Thanks for you support.

Thomas
 
Back
Top