<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Extension methods for copying or cloning objects</title>
	<atom:link href="http://ox.no/posts/extension-methods-for-copying-or-cloning-objects/feed" rel="self" type="application/rss+xml" />
	<link>http://ox.no/posts/extension-methods-for-copying-or-cloning-objects</link>
	<description>Håvard Stranden&#039;s website</description>
	<lastBuildDate>Thu, 29 Mar 2012 00:08:08 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: .NET 对象深层克隆开源框架 &#124; 哈哈808,开心呵呵网</title>
		<link>http://ox.no/posts/extension-methods-for-copying-or-cloning-objects/comment-page-1#comment-1598</link>
		<dc:creator>.NET 对象深层克隆开源框架 &#124; 哈哈808,开心呵呵网</dc:creator>
		<pubDate>Fri, 03 Jun 2011 01:07:01 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/extension-methods-for-copying-or-cloning-objects#comment-1598</guid>
		<description>&lt;p&gt;[...] Copyable is a small framework for copying (or cloning, if you will) virtually any .NET object. It copies many objects automatically, and contains additional functionality for cases where it cannot automatically perform the copying. A detailed explanation and walkthrough is given in my post on Copyable. [...]&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>[...] Copyable is a small framework for copying (or cloning, if you will) virtually any .NET object. It copies many objects automatically, and contains additional functionality for cases where it cannot automatically perform the copying. A detailed explanation and walkthrough is given in my post on Copyable. [...]</p>]]></content:encoded>
	</item>
	<item>
		<title>By: MOhan</title>
		<link>http://ox.no/posts/extension-methods-for-copying-or-cloning-objects/comment-page-1#comment-1418</link>
		<dc:creator>MOhan</dc:creator>
		<pubDate>Wed, 04 May 2011 15:52:52 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/extension-methods-for-copying-or-cloning-objects#comment-1418</guid>
		<description>&lt;p&gt;Hi,&lt;/p&gt;

&lt;p&gt;I am trying to use this framework for an entity class which is passed to a client via wcf service, it&#039;s giving following error&lt;/p&gt;

&lt;p&gt;&quot;Type &#039;OX.Copyable.Copyable&#039; cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types. &quot;&lt;/p&gt;

&lt;p&gt;Please help me out in solving this problem.&lt;/p&gt;

&lt;p&gt;Thanks,
Mohan&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Hi,</p>

<p>I am trying to use this framework for an entity class which is passed to a client via wcf service, it&#8217;s giving following error</p>

<p>&#8220;Type &#8216;OX.Copyable.Copyable&#8217; cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types. &#8220;</p>

<p>Please help me out in solving this problem.</p>

<p>Thanks,
Mohan</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Håvard</title>
		<link>http://ox.no/posts/extension-methods-for-copying-or-cloning-objects/comment-page-1#comment-1349</link>
		<dc:creator>Håvard</dc:creator>
		<pubDate>Sun, 20 Mar 2011 20:30:10 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/extension-methods-for-copying-or-cloning-objects#comment-1349</guid>
		<description>&lt;p&gt;Gravitas: No, type converters are not supported. The concept of type conversion is different from the concept of copying, so there really is no obvious way of supporting conversion or &quot;mapping&quot;. AutoMapper and similar are a better match for conversion than Copyable is.&lt;/p&gt;

&lt;p&gt;Support for custom &quot;copyers&quot; could be desirable. I haven&#039;t seen any real use for it in my applications, but I would welcome any suggestions. I would expect a suggestion to include an interface that can be implemented to support custom copying, as well as modification of the &lt;code&gt;Copyable&lt;/code&gt; base class to support custom copy behavior.&lt;/p&gt;

&lt;p&gt;Just fork on GitHub and get on with it! :)&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Gravitas: No, type converters are not supported. The concept of type conversion is different from the concept of copying, so there really is no obvious way of supporting conversion or &#8220;mapping&#8221;. AutoMapper and similar are a better match for conversion than Copyable is.</p>

<p>Support for custom &#8220;copyers&#8221; could be desirable. I haven&#8217;t seen any real use for it in my applications, but I would welcome any suggestions. I would expect a suggestion to include an interface that can be implemented to support custom copying, as well as modification of the <code>Copyable</code> base class to support custom copy behavior.</p>

<p>Just fork on GitHub and get on with it! <img src='http://ox.no/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>]]></content:encoded>
	</item>
	<item>
		<title>By: Gravitas</title>
		<link>http://ox.no/posts/extension-methods-for-copying-or-cloning-objects/comment-page-1#comment-1348</link>
		<dc:creator>Gravitas</dc:creator>
		<pubDate>Sun, 20 Mar 2011 12:35:20 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/extension-methods-for-copying-or-cloning-objects#comment-1348</guid>
		<description>&lt;p&gt;Is it possible to write custom type converters, as described by AutoMapper here:
http://automapper.codeplex.com/wikipage?title=Custom%20Type%20Converters&amp;referringTitle=Home&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Is it possible to write custom type converters, as described by AutoMapper here:
<a href="http://automapper.codeplex.com/wikipage?title=Custom%20Type%20Converters&#038;referringTitle=Home" rel="nofollow">http://automapper.codeplex.com/wikipage?title=Custom%20Type%20Converters&#038;referringTitle=Home</a></p>]]></content:encoded>
	</item>
	<item>
		<title>By: Mike FitzPatrick</title>
		<link>http://ox.no/posts/extension-methods-for-copying-or-cloning-objects/comment-page-1#comment-1333</link>
		<dc:creator>Mike FitzPatrick</dc:creator>
		<pubDate>Tue, 15 Mar 2011 18:53:34 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/extension-methods-for-copying-or-cloning-objects#comment-1333</guid>
		<description>&lt;p&gt;I had an issue when the class to be copied had either DataSets or DataTables as member properties.  I was able to modify the ObjectExtensions.cs file and add support for these to object types.&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;&lt;code&gt;    private static object Clone(this object instance, VisitedGraph visited, object copy)
    {
        if (visited.ContainsKey(instance))
            return visited[instance];
        else
            visited.Add(instance, copy);&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    Type type = instance.GetType();

    if (instance.GetType().Name == &quot;DataTable&quot;)
    {
        DataTable dt = (DataTable)instance;
        copy = dt.Copy();
    }
    else if (instance.GetType().Name == &quot;DataSet&quot;)
    {
        DataSet ds = (DataSet)instance;
        copy = ds.Copy();
    }
    else
    {
        while (type != null)
        {
            foreach (FieldInfo field in type.GetFields(BindingFlags.Public &#124; BindingFlags.NonPublic &#124; BindingFlags.Instance))
            {
                object value = field.GetValue(instance);
                if (visited.ContainsKey(value))
                    field.SetValue(copy, visited[value]);
                else
                    field.SetValue(copy, value.Clone(visited));
            }

            type = type.BaseType;
        }
    }

    return copy;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I had an issue when the class to be copied had either DataSets or DataTables as member properties.  I was able to modify the ObjectExtensions.cs file and add support for these to object types.</p>

<p><pre><code>    private static object Clone(this object instance, VisitedGraph visited, object copy)
    {
        if (visited.ContainsKey(instance))
            return visited[instance];
        else
            visited.Add(instance, copy);</code></pre></p>

<pre><code>    Type type = instance.GetType();

    if (instance.GetType().Name == "DataTable")
    {
        DataTable dt = (DataTable)instance;
        copy = dt.Copy();
    }
    else if (instance.GetType().Name == "DataSet")
    {
        DataSet ds = (DataSet)instance;
        copy = ds.Copy();
    }
    else
    {
        while (type != null)
        {
            foreach (FieldInfo field in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                object value = field.GetValue(instance);
                if (visited.ContainsKey(value))
                    field.SetValue(copy, visited[value]);
                else
                    field.SetValue(copy, value.Clone(visited));
            }

            type = type.BaseType;
        }
    }

    return copy;
}
</code></pre>

<p></p>]]></content:encoded>
	</item>
	<item>
		<title>By: Daniele Fusi</title>
		<link>http://ox.no/posts/extension-methods-for-copying-or-cloning-objects/comment-page-1#comment-1328</link>
		<dc:creator>Daniele Fusi</dc:creator>
		<pubDate>Sat, 12 Mar 2011 10:53:57 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/extension-methods-for-copying-or-cloning-objects#comment-1328</guid>
		<description>&lt;p&gt;Sorry guys, I had not seen another post like mine in comments, they were shown all only after I posted. So it seems there is no support for SL. Thanks anyway.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Sorry guys, I had not seen another post like mine in comments, they were shown all only after I posted. So it seems there is no support for SL. Thanks anyway.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Daniele Fusi</title>
		<link>http://ox.no/posts/extension-methods-for-copying-or-cloning-objects/comment-page-1#comment-1327</link>
		<dc:creator>Daniele Fusi</dc:creator>
		<pubDate>Sat, 12 Mar 2011 10:51:59 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/extension-methods-for-copying-or-cloning-objects#comment-1327</guid>
		<description>&lt;p&gt;Great code, thanks! I&#039;d like to use it in Silverlight (4), where it would be especially useful as deep-cloning objects using serialization is excluded by the lack of SerializableAttribute. Anyway, I tried to compile it under SL 4 and I get an error about AssemblyLoadEventArgs, which seems not public under this platform. Do you plan a SL compatible version? Or can you suggest any workaround for this?&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Great code, thanks! I&#8217;d like to use it in Silverlight (4), where it would be especially useful as deep-cloning objects using serialization is excluded by the lack of SerializableAttribute. Anyway, I tried to compile it under SL 4 and I get an error about AssemblyLoadEventArgs, which seems not public under this platform. Do you plan a SL compatible version? Or can you suggest any workaround for this?</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Marc</title>
		<link>http://ox.no/posts/extension-methods-for-copying-or-cloning-objects/comment-page-1#comment-912</link>
		<dc:creator>Marc</dc:creator>
		<pubDate>Fri, 26 Nov 2010 15:33:09 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/extension-methods-for-copying-or-cloning-objects#comment-912</guid>
		<description>&lt;p&gt;Thank You Havard,
it was a great introduction to deep copy. For others - please remove the link to your old project in this post:http://ox.no/software/copyable. It is an old version, for me only a shallow copy was possible and it failed with arrays. The version of Github works. I know this is a silly mistake, but stuff like this happens :-).
Again, very valueable framework.
Cheers,
Marc&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Thank You Havard,
it was a great introduction to deep copy. For others &#8211; please remove the link to your old project in this post:<a href="http://ox.no/software/copyable" rel="nofollow">http://ox.no/software/copyable</a>. It is an old version, for me only a shallow copy was possible and it failed with arrays. The version of Github works. I know this is a silly mistake, but stuff like this happens <img src='http://ox.no/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .
Again, very valueable framework.
Cheers,
Marc</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Håvard</title>
		<link>http://ox.no/posts/extension-methods-for-copying-or-cloning-objects/comment-page-1#comment-888</link>
		<dc:creator>Håvard</dc:creator>
		<pubDate>Mon, 18 Oct 2010 20:17:51 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/extension-methods-for-copying-or-cloning-objects#comment-888</guid>
		<description>&lt;p&gt;André,&lt;/p&gt;

&lt;p&gt;thanks a lot for noticing the issue, and sharing a solution. It would be great if you could fork Copyable on Github, implement your changes, and send me a pull request so we can get it into the master branch (preferably also with a unit test that reproduces the specific problem you had).&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>André,</p>

<p>thanks a lot for noticing the issue, and sharing a solution. It would be great if you could fork Copyable on Github, implement your changes, and send me a pull request so we can get it into the master branch (preferably also with a unit test that reproduces the specific problem you had).</p>]]></content:encoded>
	</item>
	<item>
		<title>By: André Müller</title>
		<link>http://ox.no/posts/extension-methods-for-copying-or-cloning-objects/comment-page-1#comment-886</link>
		<dc:creator>André Müller</dc:creator>
		<pubDate>Fri, 15 Oct 2010 13:26:41 +0000</pubDate>
		<guid isPermaLink="false">http://ox.no/posts/extension-methods-for-copying-or-cloning-objects#comment-886</guid>
		<description>&lt;p&gt;Ok. I think I solved the problem. The class initializer of ObjectExtensions throws when it hits the Microsoft.VisualStudio.Enterprise.ASPNetHelper assembly and calls GetTypes(). So another try catch around that and everything is fine.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;
        private static IEnumerable GetInstanceProviders(Assembly assembly)
        {
            System.Type[] types = null;
            try
            {
                types = assembly.GetTypes();
            }
            catch { }&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;&lt;code&gt;        if (types != null)
        { 
            foreach (Type t in types)
            {
                if (typeof(IInstanceProvider).IsAssignableFrom(t))
                {
                    IInstanceProvider instance = null;
                    try
                    {
                        instance = (IInstanceProvider)Activator.CreateInstance(t);
                    }
                    catch { } // Ignore provider if it cannot be created
                    if (instance != null)
                        yield return instance;
                }
            }
        }
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;&lt;/code&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Ok. I think I solved the problem. The class initializer of ObjectExtensions throws when it hits the Microsoft.VisualStudio.Enterprise.ASPNetHelper assembly and calls GetTypes(). So another try catch around that and everything is fine.</p>

<p><code>
        private static IEnumerable GetInstanceProviders(Assembly assembly)
        {
            System.Type[] types = null;
            try
            {
                types = assembly.GetTypes();
            }
            catch { }</code></p>

<p><pre><code>        if (types != null)
        { 
            foreach (Type t in types)
            {
                if (typeof(IInstanceProvider).IsAssignableFrom(t))
                {
                    IInstanceProvider instance = null;
                    try
                    {
                        instance = (IInstanceProvider)Activator.CreateInstance(t);
                    }
                    catch { } // Ignore provider if it cannot be created
                    if (instance != null)
                        yield return instance;
                }
            }
        }
    }
</code></pre></p>

<p></p>]]></content:encoded>
	</item>
</channel>
</rss>

