Skip to content

Cloner class #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
76 changes: 76 additions & 0 deletions DeepCloner/Cloner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Force.DeepCloner.Helpers;

namespace Force.DeepCloner
{
/// <summary>
/// Interface for classes capable to perform copy of object
/// </summary>
public interface ICloner
{
/// <summary>
/// Performs deep (full) copy of object and related graph
/// </summary>
T DeepClone<T>(T obj);

/// <summary>
/// Performs deep (full) copy of object and related graph to existing object
/// </summary>
/// <returns>existing filled object</returns>
/// <remarks>Method is valid only for classes, classes should be descendants in reality, not in declaration</remarks>
TTo DeepCloneTo<TFrom, TTo>(TFrom objFrom, TTo objTo) where TTo : class, TFrom;

/// <summary>
/// Performs shallow copy of object to existing object
/// </summary>
/// <returns>existing filled object</returns>
/// <remarks>Method is valid only for classes, classes should be descendants in reality, not in declaration</remarks>
TTo ShallowCloneTo<TFrom, TTo>(TFrom objFrom, TTo objTo) where TTo : class, TFrom;
}

/// <summary>
/// Performs copy of object
/// </summary>
public class Cloner : ICloner
{
public Cloner()
{
PermissionChecker.ThrowIfNoPermission();
}

/// <summary>
/// Performs deep (full) copy of object and related graph
/// </summary>
public T DeepClone<T>(T obj)
{
return DeepClonerGenerator.CloneObject(obj);
}

/// <summary>
/// Performs deep (full) copy of object and related graph to existing object
/// </summary>
/// <returns>existing filled object</returns>
/// <remarks>Method is valid only for classes, classes should be descendants in reality, not in declaration</remarks>
public TTo DeepCloneTo<TFrom, TTo>(TFrom objFrom, TTo objTo) where TTo : class, TFrom
{
return (TTo)DeepClonerGenerator.CloneObjectTo(objFrom, objTo, true);
}

/// <summary>
/// Performs shallow copy of object to existing object
/// </summary>
/// <returns>existing filled object</returns>
/// <remarks>Method is valid only for classes, classes should be descendants in reality, not in declaration</remarks>
public TTo ShallowCloneTo<TFrom, TTo>(TFrom objFrom, TTo objTo) where TTo : class, TFrom
{
return (TTo)DeepClonerGenerator.CloneObjectTo(objFrom, objTo, false);
}

/// <summary>
/// Performs shallow (only new object returned, without cloning of dependencies) copy of object
/// </summary>
public static T ShallowClone<T>(T obj)
{
return ShallowClonerGenerator.CloneObject(obj);
}
}
}
2 changes: 2 additions & 0 deletions DeepCloner/DeepCloner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="Cloner.cs" />
<Compile Include="DeepClonerExtensions.cs" />
<Compile Include="Helpers\ClonerToExprGenerator.cs" />
<Compile Include="Helpers\DeepClonerExprGenerator.cs" />
Expand All @@ -53,6 +54,7 @@
<Compile Include="Helpers\DeepClonerMsilHelper.cs" />
<Compile Include="Helpers\DeepClonerSafeTypes.cs" />
<Compile Include="Helpers\DeepCloneState.cs" />
<Compile Include="Helpers\PermissionChecker.cs" />
<Compile Include="Helpers\ReflectionHelper.cs" />
<Compile Include="Helpers\ShallowClonerGenerator.cs" />
<Compile Include="Helpers\ShallowObjectCloner.cs" />
Expand Down
30 changes: 2 additions & 28 deletions DeepCloner/DeepClonerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Security;

using Force.DeepCloner.Helpers;
using Force.DeepCloner.Helpers;

namespace Force.DeepCloner
{
Expand Down Expand Up @@ -48,30 +45,7 @@ public static T ShallowClone<T>(this T obj)

static DeepClonerExtensions()
{
if (!PermissionCheck())
{
throw new SecurityException("DeepCloner should have enough permissions to run. Grant FullTrust or Reflection permission.");
}
}

private static bool PermissionCheck()
{
// best way to check required permission: execute something and receive exception
// .net security policy is weird for normal usage
try
{
new object().ShallowClone();
}
catch (VerificationException)
{
return false;
}
catch (MemberAccessException)
{
return false;
}

return true;
PermissionChecker.ThrowIfNoPermission();
}
}
}
36 changes: 36 additions & 0 deletions DeepCloner/Helpers/PermissionChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Security;

namespace Force.DeepCloner.Helpers
{
internal static class PermissionChecker
{
public static void ThrowIfNoPermission()
{
if (!CheckPermission())
{
throw new SecurityException("DeepCloner should have enough permissions to run. Grant FullTrust or Reflection permission.");
}
}

public static bool CheckPermission()
{
// best way to check required permission: execute something and receive exception
// .net security policy is weird for normal usage
try
{
DeepClonerGenerator.CloneObject(new object());
}
catch (VerificationException)
{
return false;
}
catch (MemberAccessException)
{
return false;
}

return true;
}
}
}