/// <summary>
        /// Converts the specified object from a basic type to another type as specified.
        /// It is meant by basic types, primitive data types, strings, and enums.
        /// </summary>
        /// <param name="value">The object to be converted.</param>
        /// <param name="dstType">the destination type of conversion.</param>
        /// <returns>the converted object</returns>
        public static object ConvertBasicType(object value, Type dstType)
        {
            //if (!ReflectionUtils.IsBasicType(dstType))
            //{
            //    throw new ArgumentException("Destination type must be a basic type", "dstType");
            //}

            object convertedObj = null;

            if (dstType.IsEnum)
            {
                UdtWrapper typeWrapper = TypeWrappersPool.Pool.GetTypeWrapper(dstType, null);
                convertedObj = typeWrapper.EnumWrapper.ParseAlias(value.ToString());
            }
            else if (dstType == typeof(decimal))
            {
                // to fix the asymetry of used locales for this type between serialization and deseralization
                convertedObj = Convert.ChangeType(value, dstType, CultureInfo.InvariantCulture);
            }
            else if (dstType == typeof(bool))
            {
                string strValue = value.ToString().Trim().ToLower();
                if (strValue == "false" || strValue == "no" || strValue == "0")
                {
                    convertedObj = false;
                }
                else if (strValue == "true" || strValue == "yes" || strValue == "1")
                {
                    convertedObj = true;
                }
                else
                {
                    int boolIntValue = 0;
                    if (Int32.TryParse(strValue, out boolIntValue))
                    {
                        convertedObj = boolIntValue == 0 ? false : true;
                    }
                    else
                    {
                        throw new Exception("The specified value is not recognized as boolean: " + strValue);
                    }
                }
            }
            else
            {
                Type nullableType;
                if (IsNullable(dstType, out nullableType))
                {
                    return(ConvertBasicType(value, nullableType));
                }

                IFormatProvider ifProvider = CultureInfo.InvariantCulture;
                convertedObj = Convert.ChangeType(value, dstType, ifProvider);
            }

            return(convertedObj);
        }
예제 #2
0
        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">
        /// The <paramref name="obj"/> parameter is null.
        /// </exception>
        public override bool Equals(object obj)
        {
            if (obj is UdtWrapper)
            {
                UdtWrapper other = obj as UdtWrapper;
                return(this.m_udtType == other.m_udtType);
            }

            return(false);
        }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MemberWrapper"/> class.
        /// </summary>
        /// <param name="memberInfo">The member-info to build this instance from.</param>
        /// <param name="callerSerializer">The caller serializer.</param>
        public MemberWrapper(MemberInfo memberInfo, YAXSerializer callerSerializer)
        {
            if (!(memberInfo.MemberType == MemberTypes.Property || memberInfo.MemberType == MemberTypes.Field))
            {
                throw new Exception("Member must be either property or field");
            }

            m_memberInfo = memberInfo;
            m_isProperty = (memberInfo.MemberType == MemberTypes.Property);

            this.Alias = m_memberInfo.Name;

            if (m_isProperty)
            {
                m_propertyInfoInstance = (PropertyInfo)memberInfo;
            }
            else
            {
                m_fieldInfoInstance = (FieldInfo)memberInfo;
            }

            if (m_isProperty)
            {
                m_memberType = m_propertyInfoInstance.PropertyType;
            }
            else
            {
                m_memberType = m_fieldInfoInstance.FieldType;
            }

            m_memberTypeWrapper = TypeWrappersPool.Pool.GetTypeWrapper(this.MemberType, callerSerializer);

            InitInstance();

            if (callerSerializer != null)
            {
                this.TreatErrorsAs = callerSerializer.DefaultExceptionType;
            }
            else
            {
                this.TreatErrorsAs = YAXExceptionTypes.Error;
            }

            foreach (var attr in m_memberInfo.GetCustomAttributes(false))
            {
                if (attr is YAXBaseAttribute)
                {
                    ProcessYAXAttribute(attr);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Gets the type wrapper corresponding to the specified type.
        /// </summary>
        /// <param name="t">The type whose wrapper is needed.</param>
        /// <param name="caller">reference to the serializer instance which called this method.</param>
        /// <returns>the type wrapper corresponding to the specified type</returns>
        public UdtWrapper GetTypeWrapper(Type t, YAXSerializer caller)
        {
            lock (m_lockDic)
            {
                UdtWrapper result;
                if (!m_dicTypes.TryGetValue(t, out result))
                {
                    result = new UdtWrapper(t, caller);
                    m_dicTypes.Add(t, result);
                }
                else
                {
                    result.SetYAXSerializerOptions(caller);
                }

                return(result);
            }
        }