473,609 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using reflection to get instance name

(using .net 2.0)

Say you have a class structure like this:

class Address
....
end class

class Person
FirstName
LastName
MailingAddress as Address
BillingAddress as Address
end class

I'm using reflection to pull out the values at runtime, but I'm having
trouble when I dive into an address. When I'm traversing the object I know
when the type is address and I can get and set values on that instance, but I
can't figure a way to know if I'm looking at the mailing address or the
billing address. Is there a way to get the instance name of an object at
runtime? I've thought about setting a name property in the object, but
that's a bit of a hack and makes things much less reusable. Thanks in
advance.
May 9 '07 #1
4 3959
Jason,

Just out of curiosity, what kind of problem are you trying to solve where
you would need to use reflection in what looks like a typical business
application?

Kerry Moorman
"Jason Reynolds" wrote:
(using .net 2.0)

Say you have a class structure like this:

class Address
...
end class

class Person
FirstName
LastName
MailingAddress as Address
BillingAddress as Address
end class

I'm using reflection to pull out the values at runtime, but I'm having
trouble when I dive into an address. When I'm traversing the object I know
when the type is address and I can get and set values on that instance, but I
can't figure a way to know if I'm looking at the mailing address or the
billing address. Is there a way to get the instance name of an object at
runtime? I've thought about setting a name property in the object, but
that's a bit of a hack and makes things much less reusable. Thanks in
advance.
May 9 '07 #2
Thanks for responding so quickly, Kerry.

I'm building a library to do two-way databinding of business objects and web
forms. I'm not going the full scaffolding route, instead simply matching
property names with control names. I want it to be generic so I can simply
pass a business object, say Person, and the page control and have binding
done going in and coming out.

"Kerry Moorman" wrote:
Jason,

Just out of curiosity, what kind of problem are you trying to solve where
you would need to use reflection in what looks like a typical business
application?

Kerry Moorman
"Jason Reynolds" wrote:
(using .net 2.0)

Say you have a class structure like this:

class Address
...
end class

class Person
FirstName
LastName
MailingAddress as Address
BillingAddress as Address
end class

I'm using reflection to pull out the values at runtime, but I'm having
trouble when I dive into an address. When I'm traversing the object I know
when the type is address and I can get and set values on that instance, but I
can't figure a way to know if I'm looking at the mailing address or the
billing address. Is there a way to get the instance name of an object at
runtime? I've thought about setting a name property in the object, but
that's a bit of a hack and makes things much less reusable. Thanks in
advance.
May 9 '07 #3
Jason,
Is there a way to get the instance name of an object at
runtime?
Short answer NO!

Long answer:

Instances are not named in .NET per se, variables & fields are named...

What name would you expect when you do:

Dim jason As New Person
Dim nowhere As New Address
jason.MailingAd dress = nowhere
jason.BillingAd dress = nowhere

Remember Address is a reference object, so nowhere, jason.MailingAd dress and
jason.BillingAd dress all refer to the same instance.
I've thought about setting a name property in the object, but
that's a bit of a hack and makes things much less reusable.
If knowing the difference between a Mailing address & a Billing address is
important I would either define an AddressType property (an Enum) that had
the address types on it.

Enum AddressType
Billing
Mailing
End Enum
class Address
...
Type As AddressType
end class

Or I would make Address abstract (MustInherit) and have MailingAddress &
BillingAddress inherit from Address.
class MustInherit Address
...
end class
class MailingAddress
Inherits Address
...
end class

class BillingAddress
Inherits Address
...
end class
class Person
FirstName
LastName
MailingAddress as MailingAddress
BillingAddress as BillingAddress
end class
I would use the second if I had "significan t" polymorphic behavior between
mailing addresses & billing addresses

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Jason Reynolds" <Ja***********@ discussions.mic rosoft.comwrote in message
news:3E******** *************** ***********@mic rosoft.com...
(using .net 2.0)

Say you have a class structure like this:

class Address
...
end class

class Person
FirstName
LastName
MailingAddress as Address
BillingAddress as Address
end class

I'm using reflection to pull out the values at runtime, but I'm having
trouble when I dive into an address. When I'm traversing the object I
know
when the type is address and I can get and set values on that instance,
but I
can't figure a way to know if I'm looking at the mailing address or the
billing address. Is there a way to get the instance name of an object at
runtime? I've thought about setting a name property in the object, but
that's a bit of a hack and makes things much less reusable. Thanks in
advance.
May 11 '07 #4
Thanks for your reply, Jay. Adding an enum or abstracting the class isn't
quite what I'm looking for. Your explanation of instances and reference
objects is very helpful and crystallized the concept in my mind. Because of
this I think I'll try another approach. Thanks a bunch for your time.

"Jay B. Harlow [MVP - Outlook]" wrote:
Jason,
Is there a way to get the instance name of an object at
runtime?
Short answer NO!

Long answer:

Instances are not named in .NET per se, variables & fields are named...

What name would you expect when you do:

Dim jason As New Person
Dim nowhere As New Address
jason.MailingAd dress = nowhere
jason.BillingAd dress = nowhere

Remember Address is a reference object, so nowhere, jason.MailingAd dress and
jason.BillingAd dress all refer to the same instance.
I've thought about setting a name property in the object, but
that's a bit of a hack and makes things much less reusable.
If knowing the difference between a Mailing address & a Billing address is
important I would either define an AddressType property (an Enum) that had
the address types on it.

Enum AddressType
Billing
Mailing
End Enum
class Address
...
Type As AddressType
end class


Or I would make Address abstract (MustInherit) and have MailingAddress &
BillingAddress inherit from Address.
class MustInherit Address
...
end class

class MailingAddress
Inherits Address
...
end class

class BillingAddress
Inherits Address
...
end class
class Person
FirstName
LastName
MailingAddress as MailingAddress
BillingAddress as BillingAddress
end class

I would use the second if I had "significan t" polymorphic behavior between
mailing addresses & billing addresses

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Jason Reynolds" <Ja***********@ discussions.mic rosoft.comwrote in message
news:3E******** *************** ***********@mic rosoft.com...
(using .net 2.0)

Say you have a class structure like this:

class Address
...
end class

class Person
FirstName
LastName
MailingAddress as Address
BillingAddress as Address
end class

I'm using reflection to pull out the values at runtime, but I'm having
trouble when I dive into an address. When I'm traversing the object I
know
when the type is address and I can get and set values on that instance,
but I
can't figure a way to know if I'm looking at the mailing address or the
billing address. Is there a way to get the instance name of an object at
runtime? I've thought about setting a name property in the object, but
that's a bit of a hack and makes things much less reusable. Thanks in
advance.
May 11 '07 #5

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

Similar topics

3
5074
by: J E E | last post by:
Hi! Is it possible to access fields in a derived class using reflection? Code below works fine when I access it as a private member in the Page class, but not when accessing base class member through an interface reference. I have tried to change the snd argement to SetAttribute method from 'Name', 'set_Name' to '_name'. That doesn't seem to be the problem. I
6
2289
by: Joanna Carter \(TeamB\) | last post by:
Hi folks I have a Generic Value Type and I want to detect when the internal value changes. /////////////////////////////// public delegate void ValueTypeValidationHandler<T>(T oldValue, T newValue); public struct TestType<T> {
1
24189
by: Mudassar | last post by:
i want to get the property value using reflection. Scenerio: i have a status bar on MDI form. it has property named "Panels" and i want to get a specific panel from that panels collection using reflection. Please let me know. Thanks C# Developer
8
16887
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. This works fine if all of the properties are at the top (root level) of the model but I'd like to keep them in nested classes to organize them better. So, for example, part of my data model looks like this (simplified) : public class MainClass
6
4781
by: Ken Varn | last post by:
I have an ASP.NET application that is calling a custom class that is trying to parse all of the members of my Page object using Type.GetMembers(). The problem that I am having is that private members are not returned. I did some digging and the MSDN documentation states that the caller must have ReflectionPermission in order to get the private members of a class. I am a little unfamiliar with this stipulation. I have checked the docs on...
2
1081
by: Scott Eguires | last post by:
Is there a way to determine the name of a class instance using reflection or by some other means? For example suppose a delegate is fired, I would like to know the name of the Instance of the suscriber class whose method is being called: Say I have two suscriber classes: dim clsSub1 as CSuscriber dim clsSub2 as CSuscriber
5
1294
by: BluDog | last post by:
Hi I have a custom control that displays text, I am trying to work out how to access the name of the instance of the control to display at design time (similar to TextBox). The name property returns the Control name rather than the instance e.g. MyControl rather than MyControl1. Any ideas?
12
12155
by: Eric | last post by:
I have a custom component that can be dragged from the ToolBox onto a Windows Form (which means I can't modify it's constructor to add a name parameter). VS assigns it an instance name, but the developer can modify it's name. The name shows up in the property window as "(name)", I want to find a way to access that name programatically, so that instance of the control wil be able to determine it's own name. I have the source for the control...
6
2642
by: =?Utf-8?B?SmFzb24gUmV5bm9sZHM=?= | last post by:
(using .net 2.0) Say you have a class structure like this: class Address .... end class class Person FirstName
6
2927
by: chandramohanp | last post by:
Hi I am trying to modify class instance members using reflection. I am having problem when trying to add/remove/display elements related to List<int> member. Following is the code. class TestClass { public int i = 0; public int IValue
0
8588
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...
0
8556
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7030
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...
1
6068
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5526
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
4037
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...
0
4103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2541
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
1
1690
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.