473,785 Members | 2,812 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.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!
Dec 17 '05 #1
3 1761
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!
|
|
|

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 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!
|
|
|

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.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!
| > |
| > |
| > |
| >
|
|
|

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 : <data> <orders> <order> <order_id>0</order_id> <issue_id>4460</issue_id> <action>add</action>
0
976
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 recompiled the solution. I entered the toolbox browse feature, and added the custom object class ..dll again, but the toolbox does not include the new custom listbox. What the heck? Why aren't changes being reflected in the toolbox in the dotnet IDE?
3
3538
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 changed value of that custom attribute is not reflecting back at server side. Any ideas about this problem? Server side code: private void Page_Load(object sender, EventArgs e) { if (this.IsPostBack == false)
2
2596
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 good while and I'm getting really frustrated now! My problem is this - my custom controls periodically disappear from my
4
4650
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 to a specific XSD that is defined. Right now when I declare XmlDocument as my argument, it puts the old xml:any type in. How do I change that to reflect the XSD that I'm looking for? Thanks for any Help!
9
3803
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 techniques are not feasible) The question is; Given that I have a Person object with a private set for id. What is the recommended approac in passing that object to the web service
1
18209
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' occurred in system.xml.dll Additional information: There was an error reflecting type '<MyProjectName>.<MyClassName>'." My class contains various instances of other classes (such as Uri and Regex), and I know that some of them can't be...
24
5833
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 first div in firefox, but it works fine in othe browsers. I also observed that, the assigned innerHTML to the second div is reflecting after some time. Why's it reflecting after some time.
1
2483
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 serialiser and then SaveToStream() which will serialises the chart object into a stream for you, which is all well and good... Now for the problem: If I create a standalone application and use the serialiser to serialise charts it all works fine....
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10090
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6739
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4050
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 we have to send another system

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.