Retrieve the property name - How To?

A

Antonio Paglia

Imaginate you have one method that accept some arguments like:

1- a control
2- un Type
(MyType)
3- el nombre de una Propiedad "PropertyName"

Example:

.Add(Me.txtCustomerID, GetType(Customer), "CustomerID")

Inside this method, I use reflection tu scan all properties.

I want avoid to use literals like "CustomerID" to refer the properties. I
would wish another strong method to retrieve the Name of the property.

Any help will be appreciated

TIA
Antonio
 
T

Tyrant Mikey

We'll need a bit more information before we can trouble-shoot this one.
:) What is "CustomerID"? What is its relationship to the other
arguments?
 
A

Antonio Paglia

Sorry, I forgot to translate some text.....:-(

Customer : is a type of my Domain Model
CustomerID: is a Property of Customer ---> OneCustomer.CustomerID

The first argument (Control) it is not important
 
T

Tyrant Mikey

I'm assuming that you want to add each property on a domain model class
to a collection of some sort. (Not sure why, but that seems to be a
best guess.)

You need to use reflection to get the property names. You could use
something like this:

Public Function GetPropertyNames(ByVal item As Object) As
System.Collections.Specialized.StringCollection

Dim names As New System.Collections.Specialized.StringCollection

For Each p As PropertyInfo In item.GetType().GetProperties()
names.Add(p.Name)
Next

End Function
 
A

Antonio Paglia

Hi Tyrant

There is a function GetType that allow me get the Name of a type as
string. Ex:

Dim TypeName as String = GetType(MyType).Name --> return
"MyType"



what I am trying to do, but I not sure that is possible, is something like
this

Dim PropertyName as String = GetMemberInfo(MyType.MyProperty).Name --->
return "MyProperty"
 
T

Tyrant Mikey

Well, yes, you can do that. But your code sample makes it look like you
already know the name of the property. Why are you trying to use code
to programmatically determine a name that you already know?

But I digress.

The code I gave you earlier does just what you are describing. However,
for the sake of argument, let's get just one property's name.

----------------------------------------------------------------------
Dim name As String = "Name"
Dim itemType As Type = GetType(Employee)

If name = GetPropertyName(itemType, "Name") Then
MsgBox("Match!")
Else
MsgBox("No Match!")
End If

Public Class Employee
Private m_name As String
Public Property Name As String
Get
Return m_name
End Get
Set
m_name = Value
End Set
End Property
End Class

Public Class MyReflector

Public Shared Function GetPropertyName(ByVal item As Type) As
String

Dim ti As Type = item.GetType()
Dim pi As System.Reflection.PropertyInfo =
ti.GetProperty("Name")

Return pi.Name

End Function

End Class

----------------------------------------------------------------------

Again, it's tough to help you out because I don't understand the big
picture of what you're trying to do. Can you provide some context? That
is, can you describe completely what methods are calling your code, and
what data those methods need your code to return? There may be a better
solution, given the problem and its context.
 
A

Antonio Paglia

Well, Tyrant, thanks for your help.

Properties on my busines objects contains Custom Attributes like this:

<Serializable(), Entity(TableStorage:="ANALISIS", Description:="Analisis")>
_
Public Class Analisis : Inherits BusinessBase

.....

<Entity(DataField:="ANA_PUNTO_INGRESO", Key:=1, IsRequired:=True)> _
Public Property PuntoIngreso() As Integer
Get
Return mPuntoIngreso
End Get
Set(ByVal Value As Integer)
mPuntoIngreso = Value
End Set
End Property


End Class

I want to be able to retrieve the DataField attrib of any property without
using
the property name (as String). For this reason I need a function that does
this for me. I'm trying to write a generic code to do this.

Immagine you have a class with 2 properties:

<Entity(DataField:="ANA_KEY1")> _
Public Property Clave1() As String
Get
Return mClave1
End Get
Set(ByVal Value As String)
mClave1= Value
End Set
End Property

<Entity(DataField:="ANA_KEY2")> _
Public Property Clave2() As String
Get
Return mClave2
End Get
Set(ByVal Value As String)
mClave2= Value
End Set
End Property

Assume that ANA_KEY1 and ANA_KEY2 are Datafields of a table in my DB.


Then, in the client code you are writing something like this:


mWhereClause = "(" & entity.DataField("Clave1") & " = 'ABC') AND (" & _
entity.DataField("Clave2") & " =
'CBA')"


entity: is my bussines class that inherits from
BusinessBase
entity.DataField: is my own method in BusinessBase that return the value
of the Custom Attrib DataField of the Propertyname passed as argument. I
don`t like pass the property name as String. I'm looking for some method
that return the PropertyName (as string) for me. For example, something like
this....

Dim PropertyName as String = GetMemberInfo(MyType.MyProperty).Name --->
return "MyProperty"

Thanks
Antonio
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top