### Clone with default settings (FastDeepCloner.DeepCloner.Clone) Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/Clone.md Use this when you want the default cloning behavior. FastDeepCloner validates the type and checks for a default constructor, using IL for fastest performance or GetUninitializedObject if no default constructor exists. ```csharp var mycar = FastDeepCloner.DeepCloner.Clone(car); ``` -------------------------------- ### CloneTo usage example Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/CloneTo.md Clones a User object to a CloneToTest object using FastDeepCloner.DeepCloner.CloneTo, then asserts that the mapped FullName property matches the source Name. ```csharp var user = new User() { Name = "alen toma" }; var cloneTo =new CloneToTest(); FastDeepCloner.DeepCloner.CloneTo(user, cloneTo); Assert.AreEqual(user.Name, cloneTo.FullName); ``` -------------------------------- ### Clone with custom settings (FastDeepClonerSettings) Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/Clone.md Use this when you need to override the default object creation handler. The settings specify FieldType as FieldInfo and provide a custom CreateInstance delegate that uses FormatterServices.GetUninitializedObject to bypass constructors. This is useful when the type lacks a default constructor or you want to ignore constructors. ```csharp var settings = new FastDeepCloner.FastDeepClonerSettings() { FieldType = FastDeepCloner.FieldType.FieldInfo, OnCreateInstance = new FastDeepCloner.Extensions.CreateInstance((Type type) => { return FormatterServices.GetUninitializedObject(type); }) }; var mycar = FastDeepCloner.DeepCloner.Clone(car,settings); ``` -------------------------------- ### Convert User or anonymous type to IUser with ActAsInterface Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/ActAsInterface.md Shows converting a User instance or an anonymous type to IUser using ActAsInterface(). The anonymous type example works because the library handles property matching. ```csharp User user = new User() { Name="Test" }; IUser iUser = user.ActAsInterface(); // You could even use a AnonymousType IUser iUser = new { Name= "Test" }.ActAsInterface(); ``` -------------------------------- ### Define User and CloneToTest classes for CloneTo mapping Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/CloneTo.md Two classes used in the CloneTo example: User is the source object, CloneToTest is the target. CloneToTest uses FastDeepClonerColumn attributes to map properties, including a path for nested properties and type conversion with default fallback. ```csharp public class User { public virtual string Name { get; set; } = "sdjh"; public virtual int PasswordLength { get; set; } = 6; public Circular Test { get; set; } = new Circular(); } public class CloneToTest { [FastDeepCloner.FastDeepClonerColumn("Name")] [FastDeepCloner.FastDeepClonerColumn("LastName")] // Or public string FullName { get; set; } // You see here the type could be difrrent then the orginal type. // FastDeepCloner will try to convert it, if it fail then a default value will be inserted insted public string PasswordLength { get; set; } // You could add a path insted, remember this only work on none list items. [FastDeepClonerColumn("Test.myBar.Id")] public int Id { get; set; } public Circular Test { get; set; } } ``` -------------------------------- ### Using NoneCloneableAttribute to exclude GUI properties from cloning Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/NoneCloneableAttribute.md Define a class with a property marked [NoneCloneable] so that cloning copies the property reference instead of attempting to deep clone it. This is useful for GUI controls like ImageSource that cannot be cloned. The example shows a TestControls class with an Image property marked [NoneCloneable] and a Name property that will be cloned normally. ```csharp public class TestControls { [NoneCloneable] public ImageSource Image { get; set; } public String Name {get; set;} public TestControls() { // Create a BitmapSource BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = new Uri("https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRiwabCDDHq1NT1VHBsG4fLi8a5FhcEiiloEdCk23lTDYkst8Mz&usqp=CAU"); bitmap.EndInit(); Image = bitmap; } } } ``` -------------------------------- ### Create proxy instance and bind PropertyChanged event Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/ProxyInstance.md Create a proxy instance using DeepCloner.CreateProxyInstance(). If the class does not include the PropertyChanged method, bind the event directly on the returned instance. Setting a virtual property triggers the event. ```csharp User pUser = DeepCloner.CreateProxyInstance(); /// if you choose to not include the PropertyChanged in the class, you could simple bind it here (pUser as INotifyPropertyChanged).PropertyChanged += ((sender, e)=> { // your code here }); pUser.Name = "testo"; ``` -------------------------------- ### Define a class with virtual properties and optional PropertyChanged method Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/ProxyInstance.md Define a class with virtual properties to be included in the proxy. Non-virtual properties are excluded. Optionally include a PropertyChanged method with the exact signature (object sender, PropertyChangedEventArgs e) that FastDeepCloner will detect. ```csharp public class User { public virtual string Name { get; set; } /// none virtual properties wont be included public string LastName { get; set; } // this is optional, FastDeepCloner will detect this method if it exist. // the name is also optional the importend things is that it has those parameters below public void PropertyChanged(object sender, PropertyChangedEventArgs e) { // your code here } } ``` -------------------------------- ### Define interfaces and classes for ActAsInterface conversion Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/ActAsInterface.md Defines IUserGroup and IUser interfaces plus User and Role classes. The library only maps matching properties and adds extra interface properties to the new object; non-interface types like Role must be represented as interfaces (IUserGroup) in the interface. ```csharp // public interface IUserGroup { string Name { get; set; } } public interface IUser { string Name { get; set; } // As you can see class user dose not have FirstName, but still // not all properties have to match the class, the library will handle only found property and add the extra one // to the new created object string FirstName { get; set; } // You cant inc none interface in the operations // So if you choose to have class Role in user, then you have to inc it in the interface as Interface and not class IUserGroup Role { get; set; } } public class User { public string Name { get; set; } = "sdjh"; public int PasswordLength { get; set; } = 6; public Role Role { get; set; } = new Role(); } public class Role { public string Name { get; set; } = "Test"; public string RoleType { get; set; } = "Admin"; } ``` -------------------------------- ### Convert List or ObservableCollection to interface collections Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/ActAsInterface.md Converts a List to List or ObservableCollection using ActAsInterface(). The same source list is used for both conversions. ```csharp // as for list or ObservableCollection var lst = new List(); List iUserList = lst.ActAsInterface>(); // Or ObservableCollection iUserList = lst.ActAsInterface>(); ``` -------------------------------- ### Convert values with DeepCloner.ValueConverter Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/Converter.md Shows how to convert strings and byte arrays to various types using DeepCloner.ValueConverter. Returns default values (e.g., null for nullable types, 0 for int) when parsing fails. Supports types like int, decimal, DateTime, and base64 string/byte conversions. ```csharp int? data = DeepCloner.ValueConverter("120jm") // null int data = DeepCloner.ValueConverter("120jm") // 0 int data = DeepCloner.ValueConverter("120") // 120 decimal? data = DeepCloner.ValueConverter("12,5") // 12.5 decimal? data = DeepCloner.ValueConverter("552602.25") // 552602.25 DateTime? data = DeepCloner.ValueConverter("2015-01-01sd") // null DateTime? data = DeepCloner.ValueConverter("2015-01-01") // DateTime string base64String =DeepCloner.ValueConverter(new byte[] { 116, 101, 115, 116 }); // base64string byte[] base64String = DeepCloner.ValueConverter("dGVzdA==") // array ``` -------------------------------- ### Cloning an object with NoneCloneableAttribute Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/NoneCloneableAttribute.md Clone an instance of TestControls. The Name property will be deep cloned, while the Image property (marked [NoneCloneable]) will be copied by reference. ```csharp var test = new TestControls(); var cloned = test.Clone(); ``` -------------------------------- ### List of convertible types Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/Converter.md Lists the types supported by the ValueConverter: Enums, Boolean, int, Decimal, Double, Float, Long, DateTime, TimeSpan, String, and base64 string/byte conversions. ```csharp Enums Boolean int Decimal Double Float Long DateTime TimeSpan String base64string(string) to bytes bytes to base64string(string) ``` -------------------------------- ### Clone an object with circular references Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/Circular_References.md Create an instance of Circular and clone it using FastDeepCloner.DeepCloner.Clone. The library uses the primary identifier attributes to handle circular references during cloning. ```csharp var item = new Circular(); var cloned = FastDeepCloner.DeepCloner.Clone(item); ``` -------------------------------- ### Define classes with circular references using FastDeepClonerPrimaryIdentifireAttribute Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/Circular_References.md Define a Circular class and a Bar class that reference each other. Add [FastDeepClonerPrimaryIdentifire] to the Id property of each class so the library keeps track of already cloned objects and handles circular references. ```csharp public class Circular { // By adding this attribute the library will keep tracks of the objects that has already been cloned [FastDeepClonerPrimaryIdentifire] public int Id { get; set; } = 1; private Bar _bar; public Bar myBar { get { if (_bar == null) _bar = new Bar(); return _bar; } set => _bar = value; } public Circular() { myBar = new Bar(); } } public class Bar { // By adding this attribute the library will keep tracks of the objects that has already been cloned [FastDeepClonerPrimaryIdentifire] public int Id { get; set; } = 105; private Circular _myFoo; public Circular myFoo { get { if (_myFoo == null) _myFoo = new Circular(); return _myFoo; } set => _myFoo = value; } } ``` -------------------------------- ### Define internal type dictionary and IsInternalType extension method Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/InternalTypes.md Defines a static dictionary of internal types and an extension method to check if a type is internal (not a class or interface). The dictionary includes common value types, nullable types, string, Enum, and byte[]. The method returns true if the type is in the dictionary or is not a class, and is not an interface. ```csharp private static readonly Dictionary TypeDict = new Dictionary { {typeof(int),0}, {typeof(double),0}, {typeof(float),0}, {typeof(bool),0}, {typeof(decimal),0}, {typeof(long),0}, {typeof(DateTime),0}, {typeof(ushort),0}, {typeof(short),0}, {typeof(sbyte),0}, {typeof(byte),0}, {typeof(ulong),0}, {typeof(uint),0}, {typeof(char),0}, {typeof(TimeSpan),0}, {typeof(decimal?),0}, {typeof(int?),0}, {typeof(double?),0}, {typeof(float?),0}, {typeof(bool?),0}, {typeof(long?),0}, {typeof(DateTime?),0}, {typeof(ushort?),0}, {typeof(short?),0}, {typeof(sbyte?),0}, {typeof(byte?),0}, {typeof(ulong?),0}, {typeof(uint?),0}, {typeof(char?),0}, {typeof(TimeSpan?),0}, {typeof(string),0}, {typeof(Enum),0}, {typeof(byte[]),0}, }; ``` ```csharp /// /// Determines if the specified type is a none Class type. /// /// /// true if type is internal, else false. public static bool IsInternalType(this Type underlyingSystemType) { return (TypeDict.ContainsKey(underlyingSystemType) || !underlyingSystemType.GetTypeInfo().IsClass) && !underlyingSystemType.GetTypeInfo().IsInterface; } ``` -------------------------------- ### DeepCloner.ValueConverter Source: https://github.com/alentoma/fastdeepcloner/blob/master/Documentations/Converter.md Converts a value to the specified type T. If the value cannot be parsed, a default value is returned. Supports various types including primitives, DateTime, TimeSpan, enums, and byte arrays. ```APIDOC ## DeepCloner.ValueConverter ### Description Converts a value to the specified type T. If the value cannot be parsed, a default value is returned. Supports various types including primitives, DateTime, TimeSpan, enums, and byte arrays. ### Method ValueConverter(object value) ### Parameters - **value** (object) - Required - The value to convert. ### Returns - **T** - The converted value, or the default value of T if conversion fails. ### Supported Types - Enums - Boolean - int - Decimal - Double - Float - Long - DateTime - TimeSpan - String - base64string (string) to bytes - bytes to base64string (string) ### Examples ```csharp int? data = DeepCloner.ValueConverter("120jm") // null int data = DeepCloner.ValueConverter("120jm") // 0 int data = DeepCloner.ValueConverter("120") // 120 decimal? data = DeepCloner.ValueConverter("12,5") // 12.5 decimal? data = DeepCloner.ValueConverter("552602.25") // 552602.25 DateTime? data = DeepCloner.ValueConverter("2015-01-01sd") // null DateTime? data = DeepCloner.ValueConverter("2015-01-01") // DateTime string base64String = DeepCloner.ValueConverter(new byte[] { 116, 101, 115, 116 }); // base64string byte[] base64String = DeepCloner.ValueConverter("dGVzdA==") // array ``` ```