pascalabcnet/SyntaxVisitors/YieldVisitors/ObjectCopier/ObjectCopier.cs
Бондарев Иван 65a2566223 bug fix #347
add missing copyrights
change year in copyright
2017-02-19 14:29:24 +01:00

55 lines
1.8 KiB
C#

// Copyright (c) Ivan Bondarev, Stanislav Mihalkovich (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PascalABCCompiler.SyntaxTree;
namespace SyntaxVisitors
{
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
/// Reference Article http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx
/// Provides a method for performing a deep copy of an object.
/// Binary Serialization is used to perform the copy.
/// </summary>
public static class ObjectCopier
{
/// <summary>
/// Perform a deep Copy of the object.
/// </summary>
/// <typeparam className="T">The type of object being copied.</typeparam>
/// <param className="source">The object instance to copy.</param>
/// <returns>The copied object.</returns>
public static T Clone<T>(T source)
where T: syntax_tree_node
{
if (Object.ReferenceEquals(source, null))
{
return default(T);
}
/*if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable.", "source");
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}*/
return (T)source.Clone();
}
}
}