473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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 3950
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.MailingAddress = nowhere
jason.BillingAddress = nowhere

Remember Address is a reference object, so nowhere, jason.MailingAddress and
jason.BillingAddress 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 "significant" 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.microsoft.comwrote in message
news:3E**********************************@microsof t.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.MailingAddress = nowhere
jason.BillingAddress = nowhere

Remember Address is a reference object, so nowhere, jason.MailingAddress and
jason.BillingAddress 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 "significant" 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.microsoft.comwrote in message
news:3E**********************************@microsof t.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
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...
6
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...
1
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...
8
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. ...
6
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...
2
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...
5
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...
12
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...
6
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
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,...

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.