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

Custom Surrogate

Joe
I need to copy a class which derives from CollectionBase but I don't want to
include the items in the collection. I only want to copy the
fields/properties.

I was thinking of creating a Surrogate for this but I'm not too sure how
GetObjectData and SetObjectData works.

Are there any examples around doing something similar?

Thanks,
Joe

Aug 18 '06 #1
9 2218
Do you mean:
a: you want to copy the properties of your collection class
or
b: you want to create copies of the individual objects, not have a new
collection to the same (reference-type) objects?

If a, well CollectionBase only has 2 interesting properties, and one of
them is defined by the data, so just clone your custom properties
yourself.

If b, you mean a deep-clone; here's a 2.0 solution (untested) - could
be ported to 1.1 reasobly easily:

public class DeepCloneableCollection<T: Collection<T>, ICloneable
where T : ICloneable
{
public object Clone() {
int count = Count, index = 0;
T[] clonedItems = new T[count];
// use an array to prevent resizing of the new collection
foreach (T item in this) {
clonedItems[index++] = (T) item.Clone();
}
return new Collection<T>(clonedItems);
}
}

Marc

Aug 18 '06 #2
Corrections:

a: "default" .Clone() is typed correctly (ICloneable.Clone is still
"object" of course)
b: ctors are more typical
c: returns an object of the same type!

(I /said/ it was untested...)

public DeepCloneableCollection() : base() { }
public DeepCloneableCollection(IList<Titems) : base(items) {
}
public DeepCloneableCollection<TClone() {
int count = Count, index = 0;
T[] clonedItems = new T[count];
// use an array to prevent resizing of the new collection
foreach (T item in this) {
clonedItems[index++] = (T) item.Clone();
}
return new DeepCloneableCollection<T>(clonedItems);
}
object ICloneable.Clone() {
return Clone();
}
}

Aug 18 '06 #3
Joe
Thanks Marc but I don't want to copy the items in the collection just the
properties that belong to the collection.
For example:
class A : CollectionBase
{
public string Property1 {...}
public AnotherClass Property2 {...}
}

so I want to copy Property1 (well the field it returns) and a deep copy on
Property2.

-Joe

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Do you mean:
a: you want to copy the properties of your collection class
or
b: you want to create copies of the individual objects, not have a new
collection to the same (reference-type) objects?

If a, well CollectionBase only has 2 interesting properties, and one of
them is defined by the data, so just clone your custom properties
yourself.

If b, you mean a deep-clone; here's a 2.0 solution (untested) - could
be ported to 1.1 reasobly easily:

public class DeepCloneableCollection<T: Collection<T>, ICloneable
where T : ICloneable
{
public object Clone() {
int count = Count, index = 0;
T[] clonedItems = new T[count];
// use an array to prevent resizing of the new collection
foreach (T item in this) {
clonedItems[index++] = (T) item.Clone();
}
return new Collection<T>(clonedItems);
}
}

Marc

Aug 18 '06 #4
Hi Joe,

Firstly I would like to check with you what do you mean by "Copy"/
Based on my research, the method GetObjectData/SetObjectData is used when
we are trying to serialize/deserialize an object to/from a stream.
Here is link for you reference.
ISerializable.GetObjectData Method
http://msdn2.microsoft.com/en-us/lib...ization.iseria
lizable.getobjectdata.aspx

You may check if the Copy method is what you want.
class Class1 : CollectionBase
{
public Class1 Copy()
{
Class1 oClass1 = this.MemberwiseClone();
oClass1.Clear();
}
}

NOTE: the MemberwiseClone will do a shadow copy.
For detailed information, please check the link below.
Remarks
The MemberwiseClone method creates a shallow copy by creating a new object,
and then copying the nonstatic fields of the current object to the new
object. If a field is a value type, a bit-by-bit copy of the field is
performed. If a field is a reference type, the reference is copied but the
referred object is not; therefore, the original object and its clone refer
to the same object.

For example, consider an object called X that references objects A and B.
Object B, in turn, references object C. A shallow copy of X creates new
object X2 that also references objects A and B. In contrast, a deep copy of
X creates a new object X2 that references the new objects A2 and B2, which
are copies of A and B. B2, in turn, references the new object C2, which is
a copy C. Use a class that implements the ICloneable interface to perform a
deep or shallow copy of an object.

Object.MemberwiseClone Method
http://msdn2.microsoft.com/en-us/lib...wiseclone.aspx

So it is important how will you deal with a field/property which is a
reference type(e.g. another collection), will you generate another copy of
the collection item? Is the MemberwiseClone method enough for your scenario?
Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 21 '06 #5
Joe
Hi Peter,

I need to do a deep copy of my object but I do NOT want to copy the objects
within the collection just the fields of the collection itself.
The reason I was thinking serialization is because it automatically does a
deep copy of all the objects. I just don't know how to use GetObjectData to
exclude the objects in the collection.

-Joe

""Peter Huang" [MSFT]" <v-******@online.microsoft.comwrote in message
news:DT**************@TK2MSFTNGXA01.phx.gbl...
Hi Joe,

Firstly I would like to check with you what do you mean by "Copy"/
Based on my research, the method GetObjectData/SetObjectData is used when
we are trying to serialize/deserialize an object to/from a stream.
Here is link for you reference.
ISerializable.GetObjectData Method
http://msdn2.microsoft.com/en-us/lib...ization.iseria
lizable.getobjectdata.aspx

You may check if the Copy method is what you want.
class Class1 : CollectionBase
{
public Class1 Copy()
{
Class1 oClass1 = this.MemberwiseClone();
oClass1.Clear();
}
}

NOTE: the MemberwiseClone will do a shadow copy.
For detailed information, please check the link below.
Remarks
The MemberwiseClone method creates a shallow copy by creating a new
object,
and then copying the nonstatic fields of the current object to the new
object. If a field is a value type, a bit-by-bit copy of the field is
performed. If a field is a reference type, the reference is copied but the
referred object is not; therefore, the original object and its clone refer
to the same object.

For example, consider an object called X that references objects A and B.
Object B, in turn, references object C. A shallow copy of X creates new
object X2 that also references objects A and B. In contrast, a deep copy
of
X creates a new object X2 that references the new objects A2 and B2, which
are copies of A and B. B2, in turn, references the new object C2, which is
a copy C. Use a class that implements the ICloneable interface to perform
a
deep or shallow copy of an object.

Object.MemberwiseClone Method
http://msdn2.microsoft.com/en-us/lib...wiseclone.aspx

So it is important how will you deal with a field/property which is a
reference type(e.g. another collection), will you generate another copy of
the collection item? Is the MemberwiseClone method enough for your
scenario?
Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Aug 21 '06 #6
Hi Joe,

So far I understand your scenario that you want to implement the Copy/Clone
via Serialize the object into a memorystream and deserialize it back to a
new object.
Now you want to leverage the .NET Serialization feature to customize that
it will not serialize the objects in the collection.

Based on my research, the .NET serialization did not have such feature to
exactly reach your goal.
1. We have an attribute NonSerializedAttribute, which can be used to
indicates that a field of a serializable class should not be serialized.
This class cannot be inherited.
But in your scenario, the List we want to prevent from serializing is in
the CollectionBase build class but not our class.

NonSerializedAttribute Members
http://msdn2.microsoft.com/en-us/lib...ttribute_membe
rs.aspx

2. By implement ISerializable, we can control how to serialize an object by
implementing GetObjectData, but that methods works as the pattern building
from start.
e.g.
void ISerializable.GetObjectData(
SerializationInfo info, StreamingContext context) {

// Serialize the desired values for this class
info.AddValue("title", title);

// Get the set of serializable members for our class and base classes
Type thisType = this.GetType();
MemberInfo[] mi =
FormatterServices.GetSerializableMembers(thisType, context);

// Serialize the base class's fields to the info object
for (Int32 i = 0 ; i < mi.Length; i++) {
// Don't serialize fields for this class
if (mi[i].DeclaringType == thisType) continue;
info.AddValue(mi[i].Name, ((FieldInfo) mi[i]).GetValue(this));
}
}

We need do all the job about what we want to serialize to a stream,
including the fields in the collectionbase.

3. So for your scenario, a simple resolution was to create a copy method
which will serialize the collection into memorystream and deserialize to a
new object and then clear the objects in collection.

If you have any concern, please feel free to let me know.

Here are some good article about serialization.
http://msdn.microsoft.com/msdnmag/is...t/default.aspx
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/is...t/default.aspx

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 22 '06 #7
Hi Joe,

Did you have any concern on this issue?
If so, please feel free to post here.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 24 '06 #8
Joe
I'm going to take another approach. I thought that in GetObjectData if I
didn't include the Item property of the collection then that would give me
the results I wanted but I haven't fully gotten down the info.AddValue yet.

-Joe

""Peter Huang" [MSFT]" <v-******@online.microsoft.comwrote in message
news:wm**************@TK2MSFTNGXA01.phx.gbl...
Hi Joe,

Did you have any concern on this issue?
If so, please feel free to post here.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Aug 24 '06 #9
Hi Joe,

Thanks very much for your reply.
Also if you have any other concern, please feel free to post here and I am
happy to be of assistance.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 25 '06 #10

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

Similar topics

0
by: Patrick Mezenberg | last post by:
Hi all, It should be possible to let inproc COM-components live in a surrogate process (for instance in the default Dllhost.exe surrogate process, for more info see http://msdn.microsoft.com...
4
by: DCM Fan | last post by:
{CREATE TABLEs and INSERTs follow...} Gents, I have a main table that is in ONE-MANY with many other tables. For example, if the main table is named A, there are these realtionships: A-->B...
3
by: Chris Mullins | last post by:
I've got a big unicode character, and i'm trying to build it into a string. The unicode character is in the range "0x10400", so it's going to require a surrogate pair. I've been through all...
2
by: Chris Mullins | last post by:
I've spent a bit of time over the last year trying to implement RFC 3454 (Preparation of Internationalized Strings, aka 'StringPrep'). This RFC is also a dependency for RFC 3491...
2
by: neilt100 | last post by:
Am running Win XP SP2 with Classic ASP+VB6 dll's alongside ASP.NET+C# dll's. I keep getting the following error … ‘COM Surrogate encountered a problem and needs to shut down’. This requires...
6
by: guy | last post by:
if a string contains surrogate chars (i.e. Unicode characters that consiste of more than 1 char) do functions that use an indexer or a string length into the string e.g. Mid, Len work correctly? ...
3
by: Sakcee | last post by:
Hi In one of the data files that I have , I am seeing these characters \xed\xa0\xa0 . They seem to break the xsl. --------------------------------------------------------------- Extra...
2
by: JohnnyDeep | last post by:
Hi, I am creating an unique index on surrogate key column. The surrogate key is a generate number always increasing on my dimension table. The only operation on the table is insert (no update...
1
by: Alexander Higgins | last post by:
>>Thanks for the response.... Point Taken but this is not the case. Thus, if a person writes a text file on her or his computer and does not use UNICODE to save it, the current code page is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.