Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace System
{
public static class ObjectExtensions
{
[AttributeUsage(AttributeTargets.Class|System.AttributeTargets.Field|System.AttributeTargets.Property)]
public class NoCopy : Attribute { }

private static readonly MethodInfo CloneMethod = typeof(Object).GetMethod("MemberwiseClone", BindingFlags.NonPublic | BindingFlags.Instance);

public static bool IsPrimitive(this Type type)
Expand All @@ -22,6 +25,12 @@ private static Object InternalCopy(Object originalObject, IDictionary<Object, Ob
{
if (originalObject == null) return null;
var typeToReflect = originalObject.GetType();
var attributes = System.Attribute.GetCustomAttributes(typeToReflect);
foreach (var attribute in attributes)
{
if (attribute is NoCopy)
return originalObject;
}
if (IsPrimitive(typeToReflect)) return originalObject;
if (visited.ContainsKey(originalObject)) return visited[originalObject];
if (typeof(Delegate).IsAssignableFrom(typeToReflect)) return null;
Expand Down