Thanks for your followup Param,
If the member variable is a class field rather than Property, we should use
the System.Reflecti on.FieldInfo to query that value. e.g:
=============== =============
protected void Page_Load(objec t sender, EventArgs e)
{
Class1 cls1 = new Class1();
cls1.Name = "cls1";
cls1.Lengh = 1000;
object obj = cls1;
FieldInfo fi = obj.GetType().G etField("Name", BindingFlags.Pu blic |
BindingFlags.In stance);
if (fi != null)
{
Response.Write( "<br>Name: " + fi.GetValue(obj ));
}
fi = obj.GetType().G etField("Lengh" , BindingFlags.Pu blic |
BindingFlags.In stance);
if (fi != null)
{
Response.Write( "<br>Length : " + fi.GetValue(obj ));
}
}
=============== =============== =====
Hope helps. Thanks,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: <pa***@communit y.nospam>
| References: <Oo************ **@tk2msftngp13 .phx.gbl>
<Wx************ **@TK2MSFTNGXA0 2.phx.gbl>
| Subject: Re: reflecting on custom object
| Date: Mon, 26 Dec 2005 17:07:30 -0600
| Lines: 155
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
| X-RFC2646: Format=Flowed; Original
| Message-ID: <O5************ **@TK2MSFTNGP14 .phx.gbl>
| Newsgroups: microsoft.publi c.dotnet.langua ges.vb
| NNTP-Posting-Host: corp.lazardgrou p.com 69.2.40.60
| Path: TK2MSFTNGXA02.p hx.gbl!TK2MSFTN GP08.phx.gbl!TK 2MSFTNGP14.phx. gbl
| Xref: TK2MSFTNGXA02.p hx.gbl microsoft.publi c.dotnet.langua ges.vb:310094
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vb
|
| Steve:
|
| Here is the problem. The custom object is a Web Service Client Proxy
class
| in C#. It implements all the properties as member variables rather than
| properties. The PropertyCollect ion is empty.
|
| Thanks!
|
| "Steven Cheng[MSFT]" <st*****@online .microsoft.com> wrote in message
| news:Wx******** ******@TK2MSFTN GXA02.phx.gbl.. .
| > Hi Param,
| >
| > Welcome to VBNET newsgroup.
| > For the getting property value from custom class dynamically through
| > reflection api question, I think you can try using the TypeDescriptor
and
| > PropertyDescrip tor class under System.Componen tModel namespace......
| > (Relfection api are mainly used for get class's structure or metadata
| > info....). We can use TypeDescriptor class's GetProperties to get all
the
| > properties collection from a certain type or object, then, use the
| > PropertyDescrip tor we want to get the certain class instance's property
| > value by calling the "GetValue" method. Here is a simple example which
| > retrieve a property value from a custom class:
| >
| >
| > =========custom class code===========
| > Public Class MyCustomClass
| >
| > Public Sub New()
| > _name = New MyUserName
| > _name.FirstName = "Default First Name"
| > _name.LastName = "Default Last Name"
| > End Sub
| >
| > Private _name As MyUserName
| >
| > Public Property Name() As MyUserName
| > Get
| > Return _name
| > End Get
| > Set(ByVal Value As MyUserName)
| > _name = Value
| > End Set
| > End Property
| >
| >
| > End Class
| >
| > Public Class MyUserName
| >
| > Private _firstname As String
| > Private _lastname As String
| >
| > Public Property FirstName() As String
| > Get
| > Return _firstname
| > End Get
| > Set(ByVal Value As String)
| > _lastname = Value
| > End Set
| > End Property
| >
| > Public Property LastName() As String
| > Get
| > Return _lastname
| > End Get
| > Set(ByVal Value As String)
| > _lastname = Value
| > End Set
| > End Property
| >
| > End Class
| > =============== =============
| >
| >
| > =======main app code==========
| > Module Module1
| >
| > Sub Main()
| >
| > Dim cc As New MyCustomClass
| > cc.Name.FirstNa me = "Mike"
| > cc.Name.LastNam e = "Lorn"
| >
| > DoReflection(cc )
| >
| > End Sub
| >
| > Sub DoReflection(By Val obj As Object)
| >
| > Dim pd As System.Componen tModel.Property Descriptor
| >
| > pd = TypeDescriptor. GetProperties(o bj).Item("Name" )
| >
| >
| > Dim name As MyUserName = CType(pd.GetVal ue(obj), MyUserName)
| >
| > Console.Write(" FirstName: {0}, LastName: {1}", name.FirstName,
| > name.LastName)
| >
| >
| > End Sub
| > End Module
| > =============== ===
| >
| > Hope helps. Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure!
www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| >
| >
| >
| > --------------------
| > | From: <pa***@communit y.nospam>
| > | Subject: reflecting on custom object
| > | Date: Sat, 17 Dec 2005 16:32:49 -0600
| > | Lines: 15
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
| > | X-RFC2646: Format=Flowed; Original
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
| > | Message-ID: <Oo************ **@tk2msftngp13 .phx.gbl>
| > | Newsgroups: microsoft.publi c.dotnet.langua ges.vb
| > | NNTP-Posting-Host: corp.lazardgrou p.com 69.2.40.60
| > | Path: TK2MSFTNGXA02.p hx.gbl!TK2MSFTN GP08.phx.gbl!tk 2msftngp13.phx. gbl
| > | Xref: TK2MSFTNGXA02.p hx.gbl
microsoft.publi c.dotnet.langua ges.vb:309085
| > | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vb
| > |
| > | Hi all, I have a question on reflection
| > |
| > | Lets say I have a custom object called Address. Now, lets say I have a
| > | string variable that holds the name of a variable in the object such
as
| > | "State.StateCod e". How can I reflect upon the Address object so as to
| > echo
| > | the value contained in the string variable. Something like:-
| > |
| > | Dim add as new Address
| > | add.State.State Code = "TX"
| > | Dim str as string = "State.StateCod e"
| > | Echo ValueOff(add, str)
| > |
| > | TIA!
| > |
| > |
| > |
| >
|
|
|