473,511 Members | 16,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reflecting on custom object

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!
Dec 17 '05 #1
3 1749
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!
|
|
|

Dec 19 '05 #2
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!
|
|
|

Dec 26 '05 #3
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!
| > |
| > |
| > |
| >
|
|
|

Dec 27 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
714
by: DotNetJunkies User | last post by:
I have to deserialize an XML document to objects and then serialize it back to XML to pass to the stored proc. I am attaching partial code. After this , I also have to serialize Here is the XML :...
0
963
by: Steven C | last post by:
Hello: I have a c# class library that contains custom base form object definitions such as base textbox, base label, base grid, etc. I added a base listbox to the definition class and...
3
3521
by: The Developer | last post by:
Hi All, I have a web application where I am adding a custom attribute to my ASP.NET text box control and changing value of that attribute at client side using JavaScript. My problem is that...
2
2565
by: Suzanne | last post by:
Hi all, I'm reposting this message as I'm experiencing this problem more and more frequently : I really hope someone out there can help me as I've been tearing my hair out on this one for a...
4
4626
by: MattBell | last post by:
I've tried to search for an answer to this without much success, and I think it's probably a common thing to do: I have a web service I want to accept an XmlDocument as an argument which conforms...
9
3771
by: Greger | last post by:
Hi, I am building an architecture that passes my custom objects to and from webservices. (Our internal architecture requires me to use webservices to any suggestion to use other remoting...
1
18065
by: Paul E Collins | last post by:
I'm using XmlSerializer.Serialize method from System.Xml.Serialization on one of my own classes and getting the following error: "An unhandled exception of type 'System.InvalidOperationException'...
24
5791
by: ManjunathRatakonda | last post by:
Hi, Am assigning the innerHTML of a div to another div, the problem is that the assigned innerHTML is not reflected when i display the second div to which i have assigned the innerHTML of the...
1
2457
by: OrionLee | last post by:
I am using C# to work with a 3rd party DLL (Nevron Charts), and attempting to serialise it. The serialisation itself is handled somewhere inside the DLL, so to get it to happen you call the Nevron's...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.