Re: Retrieve the property name - How To? by Antonio
Antonio
Tue Sep 27 14:15:04 CDT 2005
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
"Tyrant Mikey" <kchighland@gmail.com> escribió en el mensaje
news:1127831120.871549.193240@z14g2000cwz.googlegroups.com...
> 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.
>