Beispiel #1
0
        internal static Action GetSimpleAssignment(PropertyInfo toPi, object toObj, PropertyInfo fromPi, object fromObj, out string logInfo)
        {
            Action noop = () => { };

            logInfo = null;
            if (toPi == null || toObj == null || fromPi == null || fromObj == null)
            {
                return(noop);
            }

            //only deal in value types to value types
            if (!NfReflect.IsValueTypeProperty(toPi, true) || !NfReflect.IsValueTypeProperty(fromPi, true))
            {
                return(noop);
            }

            //enums require alot of special handling especially when wrapped in Nullable`1[
            var cpiIsEnum    = NfReflect.IsPropertyEnum(toPi);
            var fromPiIsEnum = NfReflect.IsPropertyEnum(fromPi);

            //get each pi's type
            var cpiType    = cpiIsEnum ? NfReflect.GetEnumType(toPi) : NfReflect.GetPropertyValueType(toPi);
            var fromPiType = fromPiIsEnum ? NfReflect.GetEnumType(fromPi) : NfReflect.GetPropertyValueType(fromPi);

            //get each pi's type's full name
            var cpiTypeFullname    = cpiType.FullName;
            var fromPiTypeFullname = fromPiType.FullName;

            logInfo = String.Join("->", fromPiTypeFullname, cpiTypeFullname);

            //easy assignment for same types
            if (cpiTypeFullname == fromPiTypeFullname)
            {
                return(() => toPi.SetValue(toObj, fromPi.GetValue(fromObj)));
            }

            //going from Enum to a string
            if (fromPiIsEnum && cpiTypeFullname == STR_FN)
            {
                //take enum value and get its name
                return(() =>
                {
                    var enumName = Enum.GetName(fromPiType, fromPi.GetValue(fromObj));
                    if (enumName != null)
                    {
                        toPi.SetValue(toObj, enumName);
                    }
                });
            }

            //going from a string to an enum
            if (cpiIsEnum && fromPiTypeFullname == STR_FN)
            {
                return(() =>
                {
                    var val = fromPi.GetValue(fromObj);
                    if (val != null && !String.IsNullOrWhiteSpace(val.ToString()) &&
                        Enum.GetNames(cpiType)
                        .Any(x => String.Equals(x, val.ToString(), StringComparison.OrdinalIgnoreCase)))
                    {
                        var enumVal = Enum.Parse(cpiType, val.ToString(), true);
                        toPi.SetValue(toObj, enumVal);
                    }
                });
            }

            //going from enum to enum
            if (fromPiIsEnum && cpiIsEnum)
            {
                //will this require any cast?
                return(() =>
                {
                    toPi.SetValue(toObj, fromPi.GetValue(fromObj));
                });
            }

            //going from some value-type to a string
            if (cpiTypeFullname == STR_FN)
            {
                return(() => toPi.SetValue(toObj, fromPi.GetValue(fromObj).ToString()));
            }

            //going from something to value-type
            switch (cpiTypeFullname)
            {
            case "System.Byte":
                return(() =>
                {
                    byte bout;
                    var bpiv = fromPi.GetValue(fromObj);
                    var byteString = bpiv == null ? "0" : bpiv.ToString();
                    if (Byte.TryParse(byteString, out bout))
                    {
                        toPi.SetValue(toObj, bout);
                    }
                });

            case "System.Int16":
                return(() =>
                {
                    short vout;
                    var piv = fromPi.GetValue(fromObj);
                    var vStr = piv == null ? "0" : piv.ToString();
                    if (Int16.TryParse(vStr, out vout))
                    {
                        toPi.SetValue(toObj, vout);
                    }
                });

            case "System.Int32":
                return(() =>
                {
                    int vout;
                    var piv = fromPi.GetValue(fromObj);
                    var vStr = piv == null ? "0" : piv.ToString();
                    if (Int32.TryParse(vStr, out vout))
                    {
                        toPi.SetValue(toObj, vout);
                    }
                });

            case "System.DateTime":
                return(() =>
                {
                    DateTime vout;
                    var piv = fromPi.GetValue(fromObj);
                    var vStr = piv == null ? "0" : piv.ToString();
                    if (DateTime.TryParse(vStr, out vout))
                    {
                        toPi.SetValue(toObj, vout);
                    }
                });

            case "System.Decimal":
                return(() =>
                {
                    decimal vout;
                    var piv = fromPi.GetValue(fromObj);
                    var vStr = piv == null ? "0" : piv.ToString();
                    if (Decimal.TryParse(vStr, out vout))
                    {
                        toPi.SetValue(toObj, vout);
                    }
                });

            case "System.Single":
                return(() =>
                {
                    float vout;
                    var piv = fromPi.GetValue(fromObj);
                    var vStr = piv == null ? "0" : piv.ToString();
                    if (Single.TryParse(vStr, out vout))
                    {
                        toPi.SetValue(toObj, vout);
                    }
                });

            case "System.Double":
                return(() =>
                {
                    double vout;
                    var piv = fromPi.GetValue(fromObj);
                    var vStr = piv == null ? "0" : piv.ToString();
                    if (Double.TryParse(vStr, out vout))
                    {
                        toPi.SetValue(toObj, vout);
                    }
                });
            }

            //default out to no operation
            return(noop);
        }