Exemplo n.º 1
0
 public static T CopyTo <T>(this ICopiable source) where T : class, new()
 {
     return(EntityHelper.CopyTo <T>(source));
 }
Exemplo n.º 2
0
 public static void UpdateFrom(this ICopiable target, object source)
 {
     EntityHelper.UpdateFrom(target, source);
 }
Exemplo n.º 3
0
        private void SetObjectMemberValue(object obj, MemberInfo memberInfo, object[] memberArgs, object value)
        {
            if (memberInfo == null)
            {
                throw new ArgumentNullException("memberInfo");
            }

            switch (memberInfo.MemberType)
            {
            case MemberTypes.Field:
            {
                FieldInfo fieldInfo  = (FieldInfo)memberInfo;
                object    fieldValue = Get();

                // If field implements ICopiable, it is meant as the right way to assign field
                ICopiable copiable = fieldValue as ICopiable;
                if (copiable != null)
                {
                    copiable.Copy(value);
                    return;
                }

                // Field could be an array: memberArgs are the indices for accessing to array elements.
                if (fieldInfo.FieldType.IsArray && memberArgs != null && memberArgs.Length > 0)
                {
                    int[] arrayArgs  = ConvertArrayArguments(fieldInfo, memberArgs);
                    Array arrayField = (Array)fieldInfo.GetValue(obj);

                    if (arrayField == null)
                    {
                        throw new InvalidOperationException(String.Format("Array '{0}' is null", memberInfo.Name));
                    }

                    arrayField.SetValue(value, arrayArgs);
                    return;
                }

                fieldInfo.SetValue(mObject, value);
            } break;

            case MemberTypes.Property:
            {
                PropertyInfo propertyInfo = (PropertyInfo)mMember;

                if (propertyInfo.CanRead)
                {
                    object propertyValue = Get();

                    // If property implements ICopiable, it is meant as the right way to assign property
                    ICopiable copiable = propertyValue as ICopiable;
                    if (copiable != null)
                    {
                        copiable.Copy(value);
                        return;
                    }
                }

                // Field could be an array: memberArgs are the indices for accessing to array elements.
                if (propertyInfo.PropertyType.IsArray && memberArgs != null && memberArgs.Length > 0)
                {
                    if (propertyInfo.CanRead == false)
                    {
                        throw new InvalidOperationException("write-only property");
                    }

                    int[] arrayArgs     = ConvertArrayArguments(propertyInfo, memberArgs);
                    Array arrayProperty = (Array)propertyInfo.GetValue(obj, null);

                    if (arrayProperty == null)
                    {
                        throw new InvalidOperationException(String.Format("Array '{0}' is null", memberInfo.Name));
                    }

                    arrayProperty.SetValue(value, arrayArgs);
                    return;
                }

                if (propertyInfo.CanWrite == false)
                {
                    throw new InvalidOperationException("read-only property");
                }

                propertyInfo.SetValue(mObject, value, mMemberArgs);
            } break;

            default:
                throw new NotSupportedException(mMember.MemberType + " is not supported");
            }
        }