コード例 #1
0
        /// <summary>
        /// Converts field value to the type specified by Schema.FieldDef. For example converts GDID->ulong or ulong->GDID.
        /// This method can be overridden to perform custom handling of types,
        ///  for example one can assign bool field as "Si" that would convert to TRUE.
        /// This method is called by SetFieldValue(...) before assigning actual field buffer
        /// </summary>
        /// <param name="fdef">Field being converted</param>
        /// <param name="value">Value to convert</param>
        /// <returns>Converted value before assignment to field buffer</returns>
        public virtual object ConvertFieldValueToDef(Schema.FieldDef fdef, object value)
        {
            if (value == DBNull.Value)
            {
                value = null;
            }

            if (value != null)
            {
                var tv = value.GetType();

                if (tv != fdef.NonNullableType && !fdef.NonNullableType.IsAssignableFrom(tv))
                {
                    // 20150224 DKh, addedEra to GDID. Only GDIDS with ERA=0 can be converted to/from INT64
                    if (fdef.NonNullableType == typeof(NFX.DataAccess.Distributed.GDID))
                    {
                        if (tv == typeof(byte[]))//20151103 DKh GDID support for byte[]
                        {
                            value = new Distributed.GDID((byte[])value);
                        }
                        else
                        {
                            value = new Distributed.GDID(0, (UInt64)Convert.ChangeType(value, typeof(UInt64)));
                        }
                    }
                    else
                    {
                        if (tv == typeof(Distributed.GDID))
                        {
                            if (fdef.NonNullableType == typeof(byte[]))
                            {
                                value = ((Distributed.GDID)value).Bytes;
                            }
                            else
                            {
                                var gdid = (Distributed.GDID)value;
                                if (gdid.Era != 0)
                                {
                                    throw new CRUDException(StringConsts.CRUD_GDID_ERA_CONVERSION_ERROR.Args(fdef.Name, fdef.NonNullableType.Name));
                                }
                                value = gdid.ID;
                            }
                        }
                        else
                        {
                            value = Convert.ChangeType(value, fdef.NonNullableType);
                        }
                    }
                }    //Types Differ
            }

            return(value);
        }
コード例 #2
0
ファイル: Row.cs プロジェクト: zhabis/nfx
        /// <summary>
        /// Converts field value to the type specified by Schema.FieldDef. For example converts GDID->ulong or ulong->GDID.
        /// This method can be overridden to perform custom handling of types,
        ///  for example one can assign bool field as "Si" that would convert to TRUE.
        /// This method is called by SetFieldValue(...) before assigning actual field buffer
        /// </summary>
        /// <param name="fdef">Field being converted</param>
        /// <param name="value">Value to convert</param>
        /// <returns>Converted value before assignment to field buffer</returns>
        public virtual object ConvertFieldValueToDef(Schema.FieldDef fdef, object value)
        {
            if (value == DBNull.Value)
            {
                value = null;
            }

            if (value == null)
            {
                return(null);
            }

            var tv = value.GetType();

            if (tv != fdef.NonNullableType && !fdef.NonNullableType.IsAssignableFrom(tv))
            {
                if (value is ObjectValueConversion.TriStateBool)
                {
                    var tsb = (ObjectValueConversion.TriStateBool)value;
                    if (tsb == ObjectValueConversion.TriStateBool.Unspecified)
                    {
                        value = null;
                    }
                    else
                    {
                        value = tsb == ObjectValueConversion.TriStateBool.True;
                    }

                    return(value);
                }

                if (fdef.NonNullableType == typeof(ObjectValueConversion.TriStateBool))
                {
                    var nb = value.AsNullableBool();
                    if (!nb.HasValue)
                    {
                        value = ObjectValueConversion.TriStateBool.Unspecified;
                    }
                    else
                    {
                        value = nb.Value ? ObjectValueConversion.TriStateBool.True : ObjectValueConversion.TriStateBool.False;
                    }

                    return(value);
                }


                // 20150224 DKh, addedEra to GDID. Only GDIDS with ERA=0 can be converted to/from INT64
                if (fdef.NonNullableType == typeof(NFX.DataAccess.Distributed.GDID))
                {
                    if (tv == typeof(byte[]))//20151103 DKh GDID support for byte[]
                    {
                        value = new Distributed.GDID((byte[])value);
                    }
                    else if (tv == typeof(string))//20160504 Spol GDID support for string
                    {
                        var sv = (string)value;
                        if (sv.IsNotNullOrWhiteSpace())
                        {
                            value = Distributed.GDID.Parse((string)value);
                        }
                        else
                        {
                            value = fdef.Type == typeof(Distributed.GDID?) ? (Distributed.GDID?)null : Distributed.GDID.Zero;
                        }
                    }
                    else
                    {
                        value = new Distributed.GDID(0, (UInt64)Convert.ChangeType(value, typeof(UInt64)));
                    }

                    return(value);
                }

                if (tv == typeof(Distributed.GDID))
                {
                    if (fdef.NonNullableType == typeof(byte[]))
                    {
                        value = ((Distributed.GDID)value).Bytes;
                    }
                    else if (fdef.NonNullableType == typeof(string))
                    {
                        value = value.ToString();
                    }
                    else
                    {
                        var gdid = (Distributed.GDID)value;
                        if (gdid.Era != 0)
                        {
                            throw new CRUDException(StringConsts.CRUD_GDID_ERA_CONVERSION_ERROR.Args(fdef.Name, fdef.NonNullableType.Name));
                        }
                        value = gdid.ID;
                    }

                    return(value);
                }

                // 20161026 Serge: handle values of enumerated field types
                if (fdef.NonNullableType.IsEnum)
                {
                    value = value.AsString().AsType(fdef.NonNullableType);
                    return(value);
                }

                value = Convert.ChangeType(value, fdef.NonNullableType);
            }  //Types Differ

            return(value);
        }