473,398 Members | 2,393 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,398 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
6 2630
Not sure how you are getting the properties and types but assuming you are
using GetProperties to get an array of "PropertyInfo" objects you could use
the PropertyInfo property "Name". I do not recommend this though as you would
be hard coding property names to compare to. I would add a custom attribute
to the property that has a Name property itself (or some other marker you can
use to distinguish between which Address. Then when you get a property of
type Address to a GetCustomAttributes and iterate over these looking for the
one you added. This should make the code more readable and supportable too.
Hope this helps.
--
Thom
"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 the response. I like the attribute idea. I was thinking along
the lines of having a name property in the address class, but didn't like the
idea at all. If I understand you correctly, you are suggesting putting
attributes on the MailingAddress and BillingAddress properties in the Person
class. I like this approach much better, but it's still not as clean as I'm
hoping for.

Going back to the PropertyInfo technique, when I loop through the
PropertyInfo array and access name I get something like
"CSQ.BusinessObjects.Address" (this is my namespace) which is the type not
the instance name. What I'm looking for is "Person.MailingAddress".

"tbain" wrote:
Not sure how you are getting the properties and types but assuming you are
using GetProperties to get an array of "PropertyInfo" objects you could use
the PropertyInfo property "Name". I do not recommend this though as you would
be hard coding property names to compare to. I would add a custom attribute
to the property that has a Name property itself (or some other marker you can
use to distinguish between which Address. Then when you get a property of
type Address to a GetCustomAttributes and iterate over these looking for the
one you added. This should make the code more readable and supportable too.
Hope this helps.
--
Thom
"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
On May 9, 3:07 pm, Jason Reynolds
<JasonReyno...@discussions.microsoft.comwrote:

<snip>
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.
There's no such thing as an "instance name" of an object. In your
above example, MailingAddress and BillingAddress could both be
references to the same object. What would you want your code to do in
that case?

Jon

May 9 '07 #4
On May 9, 4:01 pm, Jason Reynolds
<JasonReyno...@discussions.microsoft.comwrote:
Thanks for the response. I like the attribute idea. I was thinking along
the lines of having a name property in the address class, but didn't like the
idea at all. If I understand you correctly, you are suggesting putting
attributes on the MailingAddress and BillingAddress properties in the Person
class. I like this approach much better, but it's still not as clean as I'm
hoping for.

Going back to the PropertyInfo technique, when I loop through the
PropertyInfo array and access name I get something like
"CSQ.BusinessObjects.Address" (this is my namespace) which is the type not
the instance name. What I'm looking for is "Person.MailingAddress".
You shouldn't get the type name if you ask PropertyInfo for its name -
you'll get the property name. That won't be "Person.MailingAddress",
it'll just be "MailingAddress".

Check that you're not using PropertyInfo.PropertyType.Name.

Jon

May 9 '07 #5
Is there such thing as an instance? If so, would what I'm calling the
instance name actually be a property which points to an instance?

What my problem boils down to is having controls on a page such as text
boxes with the following ids:

person_firstname
person_mailingaddress_city
person_billingaddress_city

and matching these at run time with properties in business objects.

It would be incredibly handy to have a solution to this problem, it would
save a lot of time by eliminating a lot of brain dead type coding, but
perhaps I need to take a step back and consider a different approach.

"Jon Skeet [C# MVP]" wrote:
On May 9, 3:07 pm, Jason Reynolds
<JasonReyno...@discussions.microsoft.comwrote:

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

There's no such thing as an "instance name" of an object. In your
above example, MailingAddress and BillingAddress could both be
references to the same object. What would you want your code to do in
that case?

Jon

May 9 '07 #6
On May 9, 4:47 pm, Jason Reynolds
<JasonReyno...@discussions.microsoft.comwrote:
Is there such thing as an instance?
Yes - the instance is the object.
If so, would what I'm calling the
instance name actually be a property which points to an instance?
I suspect so. The *property* certainly has a name, but the object
(instance) itself doesn't.

It sounds like using the name of the property is what you're after.

Jon

May 9 '07 #7

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

Similar topics

6
by: kapilp | last post by:
hi, i have a program that used reflection to execute methods. now i want to execute the reflected method on a new thread but cant figure out how or if it can be done. take the below code, for...
4
by: Lucas Sain | last post by:
Hi, I think thta for this I have to use reflection... but I'm not shure. How can I find/get an object at runtime by looking for its name that is stored in a variable. For example: I have a...
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. ...
1
by: Edward Wilde | last post by:
Hi, Does anyone know how to examine items in the ASP.NET cache that are added using the OutputCache directive in framework 1.1 i.e. <%@ OutputCache Duration="300" VaryByParam="None"...
0
by: Shawn Hogan | last post by:
Hi everyone, I've been trying to execute a control's private event code via reflection from another class with the goal of potentially doing some unit testing. The examples below are trying to...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
3
by: Mark R. Dawson | last post by:
Hi all, I am trying to get custom attributes from a property. I can do this if I pass in the name of the property i.e. "Name" to the reflection methods, but if I pass in set_Name which is what...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
4
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
5
by: | last post by:
I am having problems with casting or converting a class to to an interface from which it derives. I'm certain that it's due to how it's being loaded, but I'm not sure how to get past the problem....
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?
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
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
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
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...

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.