Detecting Object type and creating readonly copy at runtime

D

Danielb

I need to create a read-only copy of Object X at run time, I know how
to find the type of Object X (using GetType) but how do I go about
creating a readonly copy of X?

What I want to do is this:
System.Type ValueType = Value.GetType();
create readonly object of type ValuteType and copy Value into it.

Cheers,

DanielB
 
G

Guest

Hi Danielb,

It can not be that simple :)
You can not do copy object of any type!

Only classe that implements IClonable interface supports
object cloning.

You can provide something like "read-only" by:
public readonly int y = 5;
but it works with value types (structs) only.
And you can safely get value type variable by
the property e.g.:

private int intVal=4;

public int CopyOfIntVal {
get {
return intVal;
}
}

....but it wont work with classes.
If you want to make an object "read-only" you've
got to provide its "lock".

If you've got more questions then don't afraid of feedback.

Cheers!
Marcin
 

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

Top