473,387 Members | 1,486 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,387 software developers and data experts.

How to use SetValueDirect (and TypedReference) with reflection

TEK
Hello

Have anyone succesfully used the SetValueDirect(TypedReference, object)
method?

I'm having a object wich again holds a struct that holds the objects
internal data.
I need to set a object value on the struct.

A way to do this for a value field would be:

_subfield.SetValue(_field.GetValue(obj), value);

where _field is a FieldInfo for the struct within the class, and _subField
is a FieldInfo for the actual field to set the value for.

However, if I the value to set is a object (not derivied from ValueType),
this will not work.
The _field.GetValue(obj) method will return a copy of the actual object, so
the _subfield.SetValue method will only modify the copy. The original object
will stay untouched.

A possible workaround is the following:

object holderObj = _field.GetValue(obj);
_subfield.SetValue(holderObj, value);
_field.SetValue(obj, holderObj);

this work for all types, however as this copy the whole struct into the
memory, and then back to the class, if the struct contains a lot of data
(witch it often will), this will lead to a very large overhead.

According to the documentation, the following should do exactly what I want,
however, I'm not able to make it work, it's telling me that "non-static
methods needs an object"

FieldInfo[] fieldArray = new FieldInfo[]{_field, _subfield};
targetref = TypedReference.MakeTypedReference(obj, fieldArray);
_subfield.SetValueDirect(targetref, value);

Any input would be nice....

Regards, TEK
Nov 15 '05 #1
0 4093

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

Similar topics

0
by: Joe Kraft | last post by:
I'm hoping someone can help me out. We're running a website that uses XML/XSLT transformations, VB.Net and an Oracle database. Currently the site cannot support more than 6-7 users at a time...
0
by: samlee | last post by:
Hi All, I'm learning how to write C# using reflection, but don't know how to code using reflection this.Controls.Add(this.label1); Could anyone help, Thank in advance. ...
2
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
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...
4
by: jack | last post by:
Hello, I found a reply by Peter Huang to a previous post quite valuable with regard to referencing controls on a form by name using reflection. It made me wonder when I would not use Reflection...
0
by: Qwert | last post by:
Hello, why does the following code work for a CLASS, but not for a STRUCTURE? (The first MsgBox returns '0,0', but I expected it to be '100,0'.) (The second MsgBox returns 'Hello', as...
5
by: =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post by:
Hi, is it faster to access the fields from an object directly by using reflection than accessing them by their properties using reflection? Thanks Christian
0
by: Gustavo Arriola | last post by:
Hola a todos! Estoy intentando ejecutar un método usando Reflection. El código es el siguiente: public static void SoapHandler(Exception Error) { try { Type assemblyType;
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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...

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.