Thanks for your followup Param,
If the member variable is a class field rather than Property, we should use
the System.Reflection.FieldInfo to query that value. e.g:
============================
protected void Page_Load(object sender, EventArgs e)
{
Class1 cls1 = new Class1();
cls1.Name = "cls1";
cls1.Lengh = 1000;
object obj = cls1;
FieldInfo fi = obj.GetType().GetField("Name", BindingFlags.Public |
BindingFlags.Instance);
if (fi != null)
{
Response.Write("<br>Name: " + fi.GetValue(obj));
}
fi = obj.GetType().GetField("Lengh", BindingFlags.Public |
BindingFlags.Instance);
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***@community.nospam>
| References: <Oo**************@tk2msftngp13.phx.gbl>
<Wx**************@TK2MSFTNGXA02.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.public.dotnet.languages.vb
| NNTP-Posting-Host: corp.lazardgroup.com 69.2.40.60
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP14.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.languages.vb:310094
| X-Tomcat-NG: microsoft.public.dotnet.languages.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 PropertyCollection is empty.
|
| Thanks!
|
| "Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
| news:Wx**************@TK2MSFTNGXA02.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
| > PropertyDescriptor class under System.ComponentModel 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
| > PropertyDescriptor 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.FirstName = "Mike"
| > cc.Name.LastName = "Lorn"
| >
| > DoReflection(cc)
| >
| > End Sub
| >
| > Sub DoReflection(ByVal obj As Object)
| >
| > Dim pd As System.ComponentModel.PropertyDescriptor
| >
| > pd = TypeDescriptor.GetProperties(obj).Item("Name")
| >
| >
| > Dim name As MyUserName = CType(pd.GetValue(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***@community.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.public.dotnet.languages.vb
| > | NNTP-Posting-Host: corp.lazardgroup.com 69.2.40.60
| > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msft ngp13.phx.gbl
| > | Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.languages.vb:309085
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.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.StateCode". 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.StateCode = "TX"
| > | Dim str as string = "State.StateCode"
| > | Echo ValueOff(add, str)
| > |
| > | TIA!
| > |
| > |
| > |
| >
|
|
|