Is a reference a special type in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi

Is a reference a special type in C#. For example when i d

MyClass aRef

what are the classes that are available that i can use to probe aRef as a reference. My real goal is to make a serializable reference and to recreate the link during deserialization.

Thanks
 
tham said:
Is a reference a special type in C#. For example when i do

MyClass aRef;

what are the classes that are available that i can use to probe aRef
as a reference. My real goal is to make a serializable reference and
to recreate the link during deserialization..

Really not sure what you mean here. In what way do you want to probe
aRef as a reference? You can find out what type of object aRef refers
to (if any) using aRef.GetType(), if that's the kind of thing you were
after.
 
Hi. It seem to me that you want to make use of unsafe code...

Alejandro Penate-Diaz.

tham said:
hi,

Is a reference a special type in C#. For example when i do

MyClass aRef;

what are the classes that are available that i can use to probe aRef as a
reference. My real goal is to make a serializable reference and to recreate
the link during deserialization..
 
Did I get you right: In "C-speech", you want to store a pointer to an object
in a file, reload it later so it points to the same object again?
That won't work in managed code because the GC will move your object around
in memory, so your "pointer" will point to the wrong location.
You could however store references to all "serialized objects" in a list,
and save the indices into that list in your file. That should be possible
using custom serilization.

Niki

tham said:
hi,

Is a reference a special type in C#. For example when i do

MyClass aRef;

what are the classes that are available that i can use to probe aRef as a
reference. My real goal is to make a serializable reference and to recreate
the link during deserialization..
 
Ok lets say..

class A1{

A1 aRef

since aRef is not pointing to anything... can i still call aRef.GetType()... if not how can i tell aRef is a reference to what type??? Is there also a unique identifier to every instance of any class created. i need these infomation so i can serialize and deserial aRef to and from some format like xm

exampl

serializing aRef will generat
<Ref><type>A1</type><uuid>"some unique id"</uuid></Ref

when i deserial from this xml chunk i can then relink the aRef with the type of object (A1) and the instance (identified using the uuid) ..

another way of asking is if aRef is pointing to null. what kind of information can i find out about it...???

hope i havent made my question worse!!

many thanks for the repl


----- Jon Skeet [C# MVP] wrote: ----

tham said:
Is a reference a special type in C#. For example when i d
as a reference. My real goal is to make a serializable reference an
to recreate the link during deserialization.

Really not sure what you mean here. In what way do you want to probe
aRef as a reference? You can find out what type of object aRef refers
to (if any) using aRef.GetType(), if that's the kind of thing you were
after
 
yes very close i understand how gc move things around. therefore i will 'index' the objects using some GUID.
can i box and unbox a reference so i change it in another class ... sort of like how u can do the same for basic types?

i want to do this so that the relinking of the references is dont by my core system..

----- Niki Estner wrote: ----

Did I get you right: In "C-speech", you want to store a pointer to an objec
in a file, reload it later so it points to the same object again
That won't work in managed code because the GC will move your object aroun
in memory, so your "pointer" will point to the wrong location
You could however store references to all "serialized objects" in a list
and save the indices into that list in your file. That should be possibl
using custom serilization

Nik

tham said:
reference. My real goal is to make a serializable reference and to recreat
the link during deserialization.
 
tham said:
Ok lets say...

class A1{}

A1 aRef;

since aRef is not pointing to anything... can i still call
aRef.GetType()...

No. You'll get a NullReferenceException.
if not how can i tell aRef is a reference to what
type???

It's not - if it's not pointing to anything, it's null.
Is there also a unique identifier to every instance of any
class created. i need these infomation so i can serialize and
deserial aRef to and from some format like xml

No, but you could put one in for yourself, either in a separate list or
by including a "serial number" in each instance.
example

serializing aRef will generate
<Ref><type>A1</type><uuid>"some unique id"</uuid></Ref>

when i deserial from this xml chunk i can then relink the aRef with
the type of object (A1) and the instance (identified using the uuid)
...

I suppose it's possible...
another way of asking is if aRef is pointing to null. what kind of
information can i find out about it...???

If the value of aRef is null, there *is* no more information about it.
 

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