/// <summary> /// The reflect object loosely. /// </summary> /// <param name="Source"> /// The source. /// </param> /// <param name="Destination"> /// The destination. /// </param> /// <param name="ReflectObjectCustom"> /// The reflect object custom. /// </param> public static void ReflectObjectLoosely( object Source, object Destination, ReflectObjectCustomHandler ReflectObjectCustom) { ReflectObjectLoosely(Source, Destination, ReflectObjectCustom, false); }
/// <summary> /// The reflect object loosely. /// </summary> /// <param name="Source"> /// The source. /// </param> /// <param name="Destination"> /// The destination. /// </param> /// <param name="ReflectObjectCustom"> /// The reflect object custom. /// </param> /// <param name="bReflectCollectionParams"> /// The b reflect collection params. /// </param> public static void ReflectObjectLoosely( object Source, object Destination, ReflectObjectCustomHandler ReflectObjectCustom, bool bReflectCollectionParams) { if ((Destination != null) && (Source != null)) { var allTypeMembers = GetAllTypeMembers(Destination.GetType()); if ((allTypeMembers != null) && (allTypeMembers.Count > 0)) { var is3 = GetAllTypeMembers(Source.GetType()); if ((is3 != null) && (is3.Count > 0)) { foreach (var info in allTypeMembers) { FieldInfo info5; PropertyInfo info6; Type propertyType; object obj2; Type[] interfaces; Type type3; Type type4; object obj3; IEnumerable enumerable; object obj5; MemberTypes types2; FieldInfo info2 = null; PropertyInfo info3 = null; var memberType = info.MemberType; if (memberType != MemberTypes.Field) { if (memberType == MemberTypes.Property) { goto Label_0091; } continue; } info2 = info as FieldInfo; if (info2 == null) { continue; } var fieldType = info2.FieldType; goto Label_00B5; Label_0091: info3 = info as PropertyInfo; if ((info3 == null) || !info3.CanWrite) { continue; } fieldType = info3.PropertyType; Label_00B5: Label_0337: foreach (var info4 in is3) { if (string.CompareOrdinal(info4.Name, info.Name) != 0) { goto Label_0337; } info5 = null; info6 = null; types2 = info4.MemberType; if (types2 != MemberTypes.Field) { if (types2 == MemberTypes.Property) { goto Label_011C; } goto Label_0337; } info5 = info4 as FieldInfo; if (info5 == null) { goto Label_0337; } propertyType = info5.FieldType; goto Label_0141; Label_011C: info6 = info4 as PropertyInfo; if ((info6 == null) || !info6.CanRead) { goto Label_0337; } propertyType = info6.PropertyType; Label_0141: if ((ReflectObjectCustom == null) || !ReflectObjectCustom(Source, Destination, info4, info)) { if (fieldType == propertyType) { if (typeof(Delegate).IsAssignableFrom(fieldType)) { goto Label_0337; } if (info5 != null) { obj2 = info5.GetValue(Source); } else { if (info6 == null) { goto Label_0337; } obj2 = info6.GetValue(Source, null); } if (info2 != null) { info2.SetValue(Destination, obj2); } else if (info3 != null) { info3.SetValue(Destination, obj2, null); } continue; } if ((!bReflectCollectionParams || !typeof(IEnumerable).IsAssignableFrom(propertyType)) || (fieldType.GetConstructor(Type.EmptyTypes) == null)) { goto Label_0337; } interfaces = fieldType.GetInterfaces(); if (interfaces == null) { goto Label_0337; } type3 = null; type4 = null; foreach (var type5 in interfaces) { if (type5.IsGenericType && typeof(ICollection <>).IsAssignableFrom(type5.GetGenericTypeDefinition())) { type4 = type5.GetGenericArguments()[0]; if (type4.IsValueType || (type4.GetConstructor(Type.EmptyTypes) != null)) { type3 = type5; break; } } } if (type3 == null) { goto Label_0337; } obj3 = Activator.CreateInstance(fieldType); if (info5 != null) { enumerable = info5.GetValue(Source) as IEnumerable; } else { if (info6 == null) { goto Label_0337; } enumerable = info6.GetValue(Source, null) as IEnumerable; } if (enumerable != null) { foreach (var obj4 in enumerable) { obj5 = Activator.CreateInstance(type4); ReflectObjectLoosely(obj4, obj5, ReflectObjectCustom, bReflectCollectionParams); type3.InvokeMember("Add", BindingFlags.InvokeMethod, null, obj3, new[] { obj5 }); } if (info2 != null) { info2.SetValue(Destination, obj3); } else if (info3 != null) { info3.SetValue(Destination, obj3, null); } } } } } } } } }