getting a ref froma struct field via fieldIinfo

  • Thread starter Thread starter colin
  • Start date Start date
C

colin

How can I access a field contained in fieldInfo
by reference so I can pass it to a function ?

ive tried __refvalue but this needs a type known at compile time,
I can access it with SetValue and GetValue but in one case
where the field is a structure I need to pass it as a ref to a function.

as it is a structure i cant make a 'new' copy and pass that
as a ref and then copy it back.

but ive been stuck on this for ages now, cant seem to find a way
anyone help ?

Am I missing something ?

Colin =^.^=
 
How can I access a field contained in fieldInfo
by reference so I can pass it to a function ?

You simply use it as the "ref" parameter to the function.
ive tried __refvalue but this needs a type known at compile time,
I can access it with SetValue and GetValue but in one case
where the field is a structure I need to pass it as a ref to a function.

It should work the same whether the field is a member of a class or a struct.
as it is a structure i cant make a 'new' copy and pass that
as a ref and then copy it back.

I don't see why not, but you don't really have to:

struct MyStruct
{
public int i;
}

void MyRefMethod(ref int i)
{
i = 5;
}

void MyTestMethod()
{
MyStruct mystruct = new MyStruct();

MyRefMethod(ref mystruct.i);
}
but ive been stuck on this for ages now, cant seem to find a way
anyone help ?

Am I missing something ?

I don't know. Your question as it's presented sounds like you're
missing something. See above. But if the above isn't want you're
looking for, you will do better to post some actual code demonstrating
what you're trying to do and explaining what about it doesn't work.

Pete
 
Peter Duniho said:
You simply use it as the "ref" parameter to the function.


It should work the same whether the field is a member of a class or a
struct.


I don't see why not, but you don't really have to:

struct MyStruct
{
public int i;
}

void MyRefMethod(ref int i)
{
i = 5;
}

void MyTestMethod()
{
MyStruct mystruct = new MyStruct();

MyRefMethod(ref mystruct.i);
}


I don't know. Your question as it's presented sounds like you're missing
something. See above. But if the above isn't want you're looking for,
you will do better to post some actual code demonstrating what you're
trying to do and explaining what about it doesn't work.

ah ok perhaps it wasnt as obvious what I was trying to do as I thought,
ive made a couple of other posts on the same function but with diferent
problems

it has to fill in the fields of unknown or anonymous structure types
heres the code ive got now, I resorted to make a copy first then pas as a
ref ..

public void GetFields2(ref object destObj,Type type )
{
System.Reflection.FieldInfo[] fields = type.GetFields();
object fieldObj;
foreach (System.Reflection.FieldInfo field in fields)
{
if (field.IsStructure)
fieldObj = field.GetValue(destObj);
else
fieldObj = null;
GetFields2(ref fieldObj,field.FieldType);
field.SetValue(destObj, fieldObj);
}
}

but it would be nice not to have to do the copy.

Colin =^.^=
 
ah ok perhaps it wasnt as obvious what I was trying to do as I thought,
ive made a couple of other posts on the same function but with diferent
problems

Different thread, different context. You can't assume that everyone
has read every thread.
it has to fill in the fields of unknown or anonymous structure types
heres the code ive got now, I resorted to make a copy first then pas as a
ref ..

Well, my first thought is: you're using Reflection. You deserve to
have some headaches. :)

That said, given how Reflection works I'm not sure what alternative you
expected. It probably won't help answer your question, but would you
mind posting an example of code that you'd like to compile and run, but
which doesn't work? That would probably at least help me (if not
someone else) understand the question better.

One thing I'm also not really clear on is, in the code you posted, why
are you using a "ref" parameter anyway? You never change the value of
"destObj", so I'm not understanding why you made it "ref".

Pete
 
Peter Duniho said:
Different thread, different context. You can't assume that everyone has
read every thread.

yes I quite agree, I initialy meant to follow
on from the other thread but was a very diferent aspect
of the same problem. sorry for not being so clear first time.
I gues I assumed if someone saw __refvalue they would
realise I was doing something a bit more complicated.
Well, my first thought is: you're using Reflection. You deserve to have
some headaches. :)

eek ! well its better than the mess you can get in with pointers in c
when things start to get out off step.
That said, given how Reflection works I'm not sure what alternative you
expected. It probably won't help answer your question, but would you mind
posting an example of code that you'd like to compile and run, but which
doesn't work? That would probably at least help me (if not someone else)
understand the question better.

One thing I'm also not really clear on is, in the code you posted, why are
you using a "ref" parameter anyway? You never change the value of
"destObj", so I'm not understanding why you made it "ref".

thanks, the code I posted now does work,
but obviously there a bit more to it than that,
theres a whole load of code that actually does putting
in the variables, such as strings, bools and compact longs,

but like I said I end up doing a copy for structures.
I gues its too much to hope for to avoid that.

other things like Int32, and some packed structurs can be
binarily copied.

theres custom attributes wich control the
whole process. some fields need to read themsevles via an interface
other fields need to clean up before the next field is read etc.
and the custom atributes can be put for the class definition and
for each field as required.

so I cut down the code I posted,
I could post the whole thing I dont mind that but its been choped
about so much youd probably be falling over things wich arnt
bothering me (much)
i think its probably too much for some news servers to cope with
the whole lot

the only reason I need a ref at all is some fields
are structures wich anrnt suitable to be copiied with
with PtrToStructure, so i need to go through each field by calling
recursivly,
so I need to pass it as a ref to fill in the fields
without a ref it just fills in the fields of a copy wich then gets thrown
away.

public void GetFields2(ref object destObj,Type type )
{
<<<whole load of other stuff to see if theres attributes >>>
<<<to control the reading of the class >>>
if(type==typeof(string))
{ /// string is stored almost the same but not quite.
destObj=file.ReadString(file.readByte());
file.seek(1,current);
return;
}
else if ....
<<do other field types as well as packed structures>>>
System.Reflection.FieldInfo[] fields = type.GetFields();
object fieldObj;
foreach (System.Reflection.FieldInfo field in fields)
{
<<< more attribute to controll reading of each field>>>
<<< such as skipping fields, special reading functions >>>
<<< cleaning up after fields etc >>>
if (field.IsStructure)
fieldObj = field.GetValue(destObj);
else
fieldObj = null;
///recursivly do all fields and sub fields
GetFields2(ref fieldObj,field.FieldType);
field.SetValue(destObj, fieldObj);
}
}


Colin =^.^=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top