コード例 #1
0
ファイル: DeepClone.cs プロジェクト: urbrioche/FastDeepCloner
 /// <summary>
 /// Convert Value from Type to Type
 /// when fail a default value will be loaded.
 /// can handle all known types like datetime, time span, string, long etc
 /// ex
 ///  "1115rd" to int? will return null
 ///  "152" to int? 152
 /// </summary>
 /// <param name="value"></param>
 /// <param name="datatype">eg typeof(int?)</param>
 /// <param name="defaultValue"></param>
 /// <returns></returns>
 public static object ValueConverter(this object value, Type datatype, object defaultValue = null)
 {
     return(FastDeepClonerCachedItems.Value(value, datatype, true, defaultValue));
 }
コード例 #2
0
        internal object CloneTo(object itemToClone, object CloneToItem)
        {
            var identifier = CloneToItem.GetFastDeepClonerIdentifier();

            if (identifier != null && _alreadyCloned.ContainsKey(identifier))
            {
                return(_alreadyCloned[identifier]);
            }

            if (identifier != null)
            {
                _alreadyCloned.Add(identifier, CloneToItem);
            }

            var type2  = CloneToItem.GetType();
            var props2 = FastDeepClonerCachedItems.GetFastDeepClonerProperties(type2);

            foreach (var prop2 in props2)
            {
                if (!prop2.Value.CanWrite)
                {
                    continue;
                }
                var item = itemToClone;
                var prop = item.GetType().GetProperty(prop2.Key);
                foreach (var attr in prop2.Value.GetCustomAttributes <FastDeepClonerColumn>())
                {
                    var strSplit = attr.ColumnName.Split('.');
                    for (var i = 0; i < strSplit.Length; i++)
                    {
                        var p = item.GetType().GetProperty(strSplit[i]);
                        if (p != null)
                        {
                            prop = p;

                            if (i != strSplit.Length - 1)
                            {
                                item = p.GetValue(item);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }

                if (prop != null)
                {
                    var value = prop.GetValue(item);
                    if (value == null)
                    {
                        continue;
                    }
                    if (prop.PropertyType.IsInternalType())
                    {
                        prop2.Value.SetValue(CloneToItem, FastDeepClonerCachedItems.Value(value, prop2.Value.PropertyType, true));
                    }
                    else if (prop.PropertyType == prop2.Value.PropertyType)
                    {
                        prop2.Value.SetValue(CloneToItem, value.Clone());
                    }
                    else if (prop.PropertyType.GetIListType() == prop.PropertyType && prop2.Value.PropertyType.GetIListType() == prop2.Value.PropertyType) // if not list
                    {
                        var value2 = prop2.Value.GetValue(item);
                        if (value2 == null)
                        {
                            value2 = prop2.Value.PropertyType.CreateInstance();
                        }
                        prop2.Value.SetValue(CloneToItem, CloneTo(value, value2));
                    }
                }
            }

            return(CloneToItem);
        }
コード例 #3
0
ファイル: DeepClone.cs プロジェクト: urbrioche/FastDeepCloner
 /// <summary>
 /// Convert Value from Type to Type
 /// when fail a default value will be loaded.
 /// can handle all known types like datetime, time span, string, long etc
 /// ex
 ///  "1115rd" to int? will return null
 ///  "152" to int? 152
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="value"></param>
 /// <param name="defaultValue"></param>
 /// <returns></returns>
 public static T ValueConverter <T>(this object value, object defaultValue = null)
 {
     return((T)FastDeepClonerCachedItems.Value(value, typeof(T), true, defaultValue));
 }