473,484 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

getting a reference to a property of an object

So the line of code I'm working on is something like this:

func(ref obj.GetType().GetProperty(PopertyNameSring).GetVal ue(obj,
null))

This ofcourse doesn't work since the GetValue method gets me a read
only value, not a reference to the property.

I can't drop the 'ref' due to boxing. obj might be a primetive like an
int.

There is no interface supported by obj, I'm using configuration data to
learn about the properties and the configuration data is provided at
runtime.

So I'm wondering if anyone knows a work around for this, I have to
admit I'm not sure how to get around this problem.

Feb 10 '06 #1
5 1509
PropertyInfo pi = obj.GetType().GetProperty(PropertyNameString);
object propValue = pi.GetValue(obj, null);
func(ref propValue);
pi.SetValue(obj, propValue, null);

Feb 10 '06 #2
<ha****@gmail.com> wrote:
So the line of code I'm working on is something like this:

func(ref obj.GetType().GetProperty(PopertyNameSring).GetVal ue(obj,
null))

This ofcourse doesn't work since the GetValue method gets me a read
only value, not a reference to the property.

I can't drop the 'ref' due to boxing. obj might be a primetive like an
int.


What exactly are you expecting the above to do? Are you really sure you
understand what "ref" means?

See http://www.pobox.com/~skeet/csharp/parameters.html

In particular, what would you expect to happen if you were fetching a
read-only property, but "func" changed the value of the parameter?

I suspect you just don't need the "ref" at all.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 11 '06 #3
> I can't drop the 'ref' due to boxing. obj might be a primetive like an int.

I just read Jon's response, and then re-read this line in your post. I
agree with Jon: I suspect that you don't need the "ref".

You see, when you fetch the property value:

object val =
obj.GetType().GetProperty(PropertyNameString).GetV alue(obj, null);

"val" will already be boxed if it's a primitive. If "obj" is a
primitive, then it is also already boxed, since you declared it as
"object". The "ref" doesn't help you in any way at all, except, as my
earlier post shows, it allows you to change the value that you pass in,
which presumably you want to then change the value of the property.

If you don't want to change the property's value, just fetch it and
pass it to "func", then you don't need "ref".

Feb 11 '06 #4
I kind of know what you are referring to. What I do is create a
delegate like this:

public delegate T GetInstance<T>();

Then, I create a structure like this:

public struct PropertyReference<T>
{
public GetInstance<T> Get;
public Action<T> Set;
}

Then, you can write a function like this:

public static PropertyReference<T> GetPropertyReference<T>(object instance,
string property)
{
// Get the type.
Type t = typeof(T);

// Get the property.
PropertyInfo p = t.GetProperty(property);

// Create the return value.
PropertyReference<T> retVal = new PropertyRefernece<T>();

// Set the delegates.
retVal.Get = (GetInstance<T>)
Delegate.CreateDelegate(typeof(GetInstance<T>), instance, p.GetGetMethod());
retVal.Set = (Action<T>) Delegate.CreateDelegate(typeof(Action<T>),
instance, p.GetSetMethod());

// Return the reference.
return retVal;
}

Then, you can basically pass the property around, and get/set it
whenever you want. It's ^somewhat^ similar to setting a reference, but not
exactly the same, and there is no type safety here, as you can't validate
whether or not the property exists at compile-time.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ha****@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
So the line of code I'm working on is something like this:

func(ref obj.GetType().GetProperty(PopertyNameSring).GetVal ue(obj,
null))

This ofcourse doesn't work since the GetValue method gets me a read
only value, not a reference to the property.

I can't drop the 'ref' due to boxing. obj might be a primetive like an
int.

There is no interface supported by obj, I'm using configuration data to
learn about the properties and the configuration data is provided at
runtime.

So I'm wondering if anyone knows a work around for this, I have to
admit I'm not sure how to get around this problem.

Feb 11 '06 #5
The reason i'm going this is for a decoder that decodes a stream into
an object based soly on configuration data.

All decoders support a singe interface, and the operate on an object, a
stream, config data and a decoder dictionary. The goal is to make the
deocders only bound to the configuration data and not the actual
objects I'm populating so the decoders can be reused for multiple
devices (they send the streams).

The reason I ran into the 'ref' problem was due to the problem that
some of the members might just be value types, this is where boxing
kicks in and I'm failing at actually setting the value on the object.

and when anything fails the exception will shutdown the system, this is
actually required behaviour. Uncertain behavior is considered much
worse as a non-functional system for our design so making the thing
nice and cranky is a benefit.

anyway thanks for the responese they have been a great help.

Feb 12 '06 #6

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

Similar topics

6
22486
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
21
3924
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
6
1877
by: Joel | last post by:
I need to inspect the current AppDomain for an object that implements a certain interface and get a property from the object. I know how to find out what class implements my interface but...
7
6907
by: Robin Tucker | last post by:
Hiya, I'm using mshtml and the web browser control to embed an OLE object in my ..NET application. What I want to do is get a reference to the "object" embedded therein: The HTML looks...
32
4960
by: paul | last post by:
HI! I keep on getting this error and I have tried different things but I am not sure how to send the expiring date. The error that I am getting in Firefox 1.5 is "Error: expires.toGMTString is...
3
2495
by: Grant Schenck | last post by:
Hello, I have a Windows Service developed in C# .NET. I'm making it a remote server and I can, via an IPC Channel, expose methods and call them from a client. However, I now want my remoted...
5
2456
by: reycri | last post by:
Hi, I need to be able to do this: var func = new Function("var me = <selfRef>; alert(me.params);"); func.params = "This is a test parameter"; window.setTimeout(func, 500); Basically, I...
2
2302
by: RSH | last post by:
I have a situation where I am creating a class (oValue) that contains two properties "Name" and "Value". I am using a thrid party grid control which has a DropDownList Control. The...
0
1469
by: banderson777 | last post by:
Hello, I'm a bit new at in-depth IE programming, and am having a bit of trouble with a couple of IE browser extensions (getting them to talk to each other). The first one is a C++ Browser Helper...
0
7082
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
6953
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
7105
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
5407
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,...
0
4529
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
3046
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...
0
3041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1359
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
592
muto222
php
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.