On Mon, 26 May 2008 07:36:21 -0700, csharpula csharp <cs*******@yaho o.com>
wrote:
I would like to know how can I copy from BindingList<obj to some other
BindingList<obj >?
I tried to pass one binding list to other via constractor but this is
copying it by reference and I want by value.
Passing one instance into the constructor of a new instance _is_ copying
the _BindingList_. So you don't seem to be asking the question that you
really want the answer to.
It sounds like what you really mean is that you want to clone each of the
objects in the BindingList when making the new BindingList. This may or
may not be possible, depending on the actual type of the object. If it
implements ICloneable, then you need to enumerate the original list and
call Clone() on each object to make a new instance to add to your new
BindingList.
If the type doesn't implement ICloneable, then there's not necessarily a
reliable way to do this.
You can try serializing each object to a MemoryStream, and then
deserializing it back as a new object. But whether this works for any
given type depends on whether it's serializable, and whether you really
get an exact copy of the original when it's deserialized (many types will
fail to meet one or both of those criteria).
Using reflection, you could manually do a deep copy, but this isn't a very
good general-purpose solution, as many kinds of types just aren't meant to
be duplicated. Reflection is powerful, but you run the risk of copying
something that really shouldn't have been copied if you use it.
The best solution is simply to make sure that the type of the object in
your BindingList implements ICloneable, and then clone each object
manually while creating a new BindingList.
Pete