Click here to Skip to main content
Click here to Skip to main content

Encapsulating value types into reference types

, 27 Sep 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
Value Types can not be referenced (boxing is another thing) as fields of objects, this prevents you fetching FieldInfo of the value type instance. Here is the 'worst' solution I'v found until now.

Introduction

On .NET Framework, we simply use Reflection to retrieve the members info of a given object regardless of its type. This certainly helps us to build up a human-readable structure of the class itself, it provides us a way to serialize the object into or from the human-readable format file.

Why we have to serialize the object independently? Why we don't just use XML serialization mechanism of the Framework?

There're reasons. But the most important for me, to serialize and to deserialize the object using explicit codes are clearer than the implicit implementation of the Framework while serializing a tree structure. In fact, I can not use default serialization to serialize my tree correctly - too many constraints to prevent serializing!

When I started my serializer, things appeared all about serialization of Value Types. The problem is that you can use your reference to fetch the object (as a field of another object), then you can get the FieldInfo precisely, but you can never get the FieldInfo of an int type field using value (e.g. 135), because of no reference and different fields holding the same value!

Even all attempts have been performed. As I don't have the details on the internal implementation, a better serializer was delayed and abandoned finally.

At last, I directly wrapped the Value Type which is the base of int, float, double, bool and enum etc, with a referenced type Attributeric.

Attributeric means "attribute of" which can be commonly used to replace the Value Types. Unfortunately, it means you won't use int but IntegerNumeric, string but Literal when constructing your fields as well.

Disadvantage is obverse. Yet it has advantages.

These substitutes of the basic Value Types work perfectly as referenced types, object too. You can retrieve the FieldInfo when you just give the name or the reference.

This bi-directional conversion will help you fully control your serialization of any object containing fields of Attributeric Types across the platform. Serializer would smell out the serialization name automatically during serialization and the deserializer would fetch the field just by the given name (or qualified-name).

That's why I wrote Attributeric, and it's all about it.

Here is the code:

namespace MyProject.Utils.Attributeric
{
    using System;
    using System.Reflection;
    using System.Reflection.Emit;
    using System.Runtime;
    using System.Runtime.InteropServices;
    /// <summary>
    /// </summary>
    /// <remarks>    
    /// </remarks>
    public class Attributeric
    {
        #region Helppers
        public static FieldInfo GetAttributeFieldOf(object classInstance, 
                                                    object fieldInstance)
        {
            if (classInstance == null)
                throw new ArgumentNullException("classInstance", 
                              "classInstance can not be null!");
            if (fieldInstance == null)
                throw new ArgumentNullException("fieldInstance", 
                              "fieldInstance can not be null!");
            Type classType = classInstance.GetType();
            Type fieldType = fieldInstance.GetType();
            if (fieldType.IsValueType)
                throw new ArgumentException("fieldInstance", 
                      "fieldInstance can not be ValueType!" + 
                      " Because on .Net, ValueType has no valid field pointer!");
            FieldInfo[] fields = classType.GetFields();
            if (fields != null && fields.Length > 0)
            {
                foreach (FieldInfo fi in fields)
                {
                    object fo = fi.GetValue(classInstance);
                    if ((object.ReferenceEquals(fo, fieldInstance)))
                    {
                        return fi;
                    }
                }
            }
            return null;
        }
        #endregion
        #region Fields
        protected object value = null;
        #endregion
        #region Properties
        public virtual object Value
        {
            get { return this.OnGetValue(); }
        }
        #endregion
        #region Constructors
        public Attributeric():this(new object()) { }
        public Attributeric(object value)
        {
            this.OnSetValue(value);
        }
        #endregion
        #region Virtuals
        public virtual string GetAttributeNameOf(object classInstance)
        {
            FieldInfo fieldinfo = this.GetAttributeFieldOf(classInstance);
            return (fieldinfo == null) ? string.Empty : fieldinfo.Name;
        }
        public virtual FieldInfo GetAttributeFieldOf(object classInstance)
        {
            return GetAttributeFieldOf(classInstance, this);
        }
        public virtual Attributeric IsAssignedto(Attributeric knownObject)
        {
            return 
              this.IsAssignedto((knownObject == null) ? null : knownObject.Value);
        }
        public virtual Attributeric IsAssignedto(object value)
        {
            this.OnSetValue(value);
            return this;
        }
        public virtual void Assignto(Attributeric knownObject)
        {
            if (knownObject != null)
                this.Assignto(ref knownObject.value);
        }
        public virtual void Assignto(ref object value)
        {
            value = this.Value;
        }
        public virtual Type GetValueType()
        {
            return this.OnGetValue().GetType();
        }
        protected virtual object OnSetValue(object value)
        {
            return this.value = ((value == null) ? this.OnCreateBody() : value);
        }
        protected virtual object OnCreateBody()
        {
            return new object();
        }
        protected virtual object OnGetValue()
        {
            return this.value;
        }
        #endregion
        #region Overrides
        public override bool Equals(object obj)
        {
            return this.Value.Equals(obj);
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override string ToString()
        {
            return this.Value.ToString();
        }
        #endregion
    }
    /// <summary>   
    /// </summary>
    public class Literals : Attributeric
    {
        #region Shortcuts
        public static Literals of(string literals)
        {
            return new Literals(literals);
        }
        public static implicit operator string(Literals literals)
        {
            return literals.Charactors;
        }
        #endregion
        #region Properties
        public virtual string Charactors
        {
            get { return this.OnGetString(); }
        }
        public virtual int Length
        {
            get
            {
                return this.Charactors.Length;
            }
        }
        #endregion
        #region Constructors
        public Literals():base(string.Empty) { }
        public Literals(string chars):base(chars) { }
        public Literals(params char[] chars):base(new string(chars)) { }
        #endregion
        #region Virtuals
        #region String wrappers
        public virtual int CompareTo(string strB)
        {
            return this.Charactors.CompareTo(strB);
        }
        public virtual object Clone()
        {
            return Literals.of(this.Charactors);
        }
        public virtual void CopyTo(int sourceIndex, 
          char[] destination, int destinationIndex, int count)
        {
            this.Charactors.CopyTo(sourceIndex, 
              destination, destinationIndex, count);
        }
        public virtual bool EndsWith(string value)
        {
            return this.Charactors.EndsWith(value);
        }
        public virtual bool StartsWith(string value)
        {
            return this.Charactors.StartsWith(value);
        }
        public virtual CharEnumerator GetEnumerator()
        {
            return this.Charactors.GetEnumerator();
        }
        public virtual int IndexOf(string value)
        {
            return this.Charactors.IndexOf(value);
        }
        public virtual int IndexOf(string value, int startIndex)
        {
            return this.IndexOf(value, startIndex);
        }
        public virtual int IndexOf(string value, int startIndex, int count)
        {
            return this.Charactors.IndexOf(value, startIndex, count);
        }
        public virtual int IndexOf(char value)
        {
            return this.Charactors.IndexOf(value);
        }
        public virtual int IndexOf(char value, int startIndex)
        {
            return this.Charactors.IndexOf(value, startIndex);
        }
        public virtual int IndexOf(char value, int startIndex, int count)
        {
            return this.Charactors.IndexOf(value, startIndex, count);
        }
        public virtual int IndexOfAny(char[] anyOf)
        {
            return this.Charactors.IndexOfAny(anyOf);
        }
        public virtual int IndexOfAny(char[] anyOf, int startIndex)
        {
            return this.IndexOfAny(anyOf, startIndex);
        }
        public virtual int IndexOfAny(char[] anyOf, int startIndex, int count)
        {
            return this.Charactors.IndexOfAny(anyOf, startIndex, count);
        }
        public virtual Literals Insert(int startIndex, string value)
        {
            return Literals.of(this.Charactors.Insert(startIndex, value));
        }
        public virtual Literals Insert(int startIndex, Literals value)
        {
            return this.Insert(startIndex, (string)value);
        }
        public virtual int LastIndexOf(string value)
        {
            return this.Charactors.LastIndexOf(value);
        }
        public virtual int LastIndexOf(string value, int startIndex)
        {
            return this.LastIndexOf(value, startIndex);
        }
        public virtual int LastIndexOf(string value, int startIndex, int count)
        {
            return this.Charactors.LastIndexOf(value, startIndex, count);
        }
        public virtual int LastIndexOf(char value)
        {
            return this.Charactors.LastIndexOf(value);
        }
        public virtual int LastIndexOf(char value, int startIndex)
        {
            return this.Charactors.LastIndexOf(value, startIndex);
        }
        public virtual int LastIndexOf(char value, int startIndex, int count)
        {
            return this.Charactors.LastIndexOf(value, startIndex, count);
        }
        public virtual int LastIndexOfAny(char[] anyOf)
        {
            return this.Charactors.LastIndexOfAny(anyOf);
        }
        public virtual int LastIndexOfAny(char[] anyOf, int startIndex)
        {
            return this.LastIndexOfAny(anyOf, startIndex);
        }
        public virtual int LastIndexOfAny(char[] anyOf, int startIndex, int count)
        {
            return this.Charactors.LastIndexOfAny(anyOf, startIndex, count);
        }
        public virtual Literals PadLeft(int totalWidth, char paddingChar)
        {
            return Literals.of(this.Charactors.PadLeft(totalWidth, paddingChar));
        }
        public virtual Literals PadLeft(int totalWidth)
        {
            return Literals.of(this.Charactors.PadLeft(totalWidth));
        }
        public virtual Literals PadRight(int totalWidth, char paddingChar)
        {
            return Literals.of(this.Charactors.PadRight(totalWidth, paddingChar));
        }
        public virtual Literals PadRight(int totalWidth)
        {
            return Literals.of(this.Charactors.PadRight(totalWidth));
        }
        public virtual Literals Remove(int startIndex, int count)
        {
            return Literals.of(this.Charactors.Remove(startIndex, count));
        }
        public virtual Literals Replace(char oldChar, char newChar)
        {
            return Literals.of(this.Charactors.Replace(oldChar, newChar));
        }
        public virtual Literals Replace(string oldString, string newString)
        {
            return Literals.of(this.Charactors.Replace(oldString, newString));
        }
        public virtual Literals[] Split(params char[] separator)
        {
            string[] strings = this.Charactors.Split(separator);
            Literals[] literals = new Literals[strings.Length];
            for (int i = 0; i < strings.Length; i++)
            {
                literals[i] = Literals.of(strings[i]);
            }
            return literals;
        }
        public virtual Literals[] Split(char[] separator, int count)
        {
            string[] strings = this.Charactors.Split(separator, count);
            Literals[] literals = new Literals[strings.Length];
            for (int i = 0; i < strings.Length; i++)
            {
                literals[i] = Literals.of(strings[i]);
            }
            return literals;
        }
        public virtual Literals Substring(int startIndex, int length)
        {
            return Literals.of(this.Charactors.Substring(startIndex, length));
        }
        public virtual Literals Substring(int startIndex)
        {
            return Literals.of(this.Charactors.Substring(startIndex));
        }
        public virtual char[] ToCharArray(int startIndex, int length)
        {
            return this.Charactors.ToCharArray(startIndex, length);
        }
        public virtual char[] ToCharArray()
        {
            return this.Charactors.ToCharArray();
        }
        public virtual Literals ToLower()
        {
            return Literals.of(this.Charactors.ToLower());
        }
        public virtual Literals ToUpper()
        {
            return Literals.of(this.Charactors.ToUpper());
        }
        public virtual Literals Trim()
        {
            return Literals.of(this.Charactors.Trim());
        }
        public virtual Literals Trim(params char[] trimChars)
        {
            return Literals.of(this.Charactors.Trim(trimChars));
        }
        public virtual Literals TrimEnd(params char[] trimChars)
        {
            return Literals.of(this.Charactors.TrimEnd(trimChars));
        }
        public virtual Literals TrimStart(params char[] trimChars)
        {
            return Literals.of(this.Charactors.TrimStart(trimChars));
        }
        #endregion
        #region Setter and getters
        protected virtual string OnSetString(string chars)
        {
            return this.OnSetValue(chars) as string;
        }
        protected virtual string OnGetString()
        {
            string str = this.OnGetValue() as string;
            return (str == null) ? string.Empty : str;
        }
        #endregion
        #endregion
        #region Overrides
        public override Attributeric IsAssignedto(object value)
        {
            if (!(value is string))
                throw new ArgumentException("value", 
                  "value can not be something other than string!");
            this.OnSetString(value as string);
            return this;
        }
        public override void Assignto(ref object value)
        {
            value = this.OnGetString();
        }
        #endregion
    }
    /// <summary>
    /// </summary>
    /// <remarks>
    /// </remarks>
    public class Valueric : Attributeric
    {
        #region Shortcuts
        public static Valueric like(Valueric valueric)
        {
            if (valueric == null)
                throw new ArgumentNullException("valueric", 
                            "valueric can not be null!");
            return new Valueric(valueric.GetValueType());
        }
        public static Valueric like(ValueType value)
        {
            if (value == null)
                throw new ArgumentNullException("value", 
                       "value can not be null!");
            return new Valueric(value.GetType());
        }
        public static Valueric of(Valueric valueric)
        {
            return new Valueric(valueric);
        }
        public static Valueric of(ValueType value)
        {
            return new Valueric(value);
        }
        public static implicit operator ValueType(Valueric valueric)
        {
            if (valueric == null)
                throw new ArgumentNullException("valueric", 
                              "valueric can not be null!");
            return valueric.Value;
        }
        #endregion
        #region Properties
        public new virtual ValueType Value
        {
            get { return this.OnGetValue(); }
        }
        #endregion
        #region Constructors
        public Valueric():base(0) { }
        public Valueric(ValueType value):base()
        {
            this.ValueIsAssignedto(value);
        }
        public Valueric(Valueric valueric)
        {
            if (valueric == null)
                throw new ArgumentNullException("valueric", 
                              "valueric can not be null!");
            this.OnSetValue(valueric.Value);
        }
        public Valueric(Type valueType)
        {
            if (valueType == null)
                throw new ArgumentNullException("type", 
                              "type can not be null!");
            if (valueType.IsValueType)
            {
                this.OnSetValue(this.OnCreateValueOf(valueType));
            }
            else
            {
                throw new ArgumentException("type", 
                   "type should be of ValueType!");
            }
        }
        #endregion
        #region Virtuals
        public virtual Valueric ValueIsAssignedto(ValueType value)
        {
            if (value == null)
                throw new ArgumentNullException("value", 
                              "value can not be null!");
            this.OnSetValue(value);
            return this;
        }
        public virtual Valueric ValueIsAssignedto(Valueric valueric)
        {
            if (valueric == null)
                throw new ArgumentNullException("valueric", 
                              "valueric can not be null!");
            this.ValueIsAssignedto(valueric.Value);
            return this;
        }
        public virtual void AssignValueTo(Valueric valueric)
        {
            valueric.ValueIsAssignedto(this.OnExportValue(this.Value, 
                                           valueric.GetValueType()));
        }
        public virtual void AssignValueTo(ref ValueType value)
        {
            value = this.OnGetValue();
        }
        protected virtual ValueType OnCreateValueOf(Type type)
        {
            ConstructorInfo constructor = type.GetConstructor(new Type[0]);
            return constructor.Invoke(new object[0]) as ValueType;
        }
        protected virtual ValueType OnSetValue(ValueType value)
        {
            return base.OnSetValue(value) as ValueType;
        }
        protected new virtual ValueType OnGetValue()
        {
            return base.OnGetValue() as ValueType;
        }
        protected virtual ValueType 
                  OnExportValue(ValueType toExport, Type exportType)
        {
            return toExport;
        }
        protected virtual ValueType OnImportValue(object value)
        {
            if (!(value is ValueType))
                throw new ArgumentException("value", 
                      "value can not be something other than ValueType!");
            return value as ValueType;
        }
        #endregion
        #region Overrides
        public override Attributeric IsAssignedto(object value)
        {
            return this.ValueIsAssignedto(this.OnImportValue(value));
        }
        public override void Assignto(ref object value)
        {
            value = this.OnGetValue();
        }
        #endregion
    }
    /// <summary>
    /// </summary>
    public class NumericConvertException : Exception
    {
        #region Statics
        public static string NCETitle = "Can not convert:";
        #endregion
        #region Constructors
        public NumericConvertException() { }
        public NumericConvertException(string message):base(message) { }
        public NumericConvertException(string message, 
               Exception innerException):base(message,innerException) { }
        public NumericConvertException(Type fromType, 
               Type toType):base(NCETitle+"("+fromType.Name+", 
                                                   "+toType.Name+")") { }
        #endregion
    }
    /// <summary>
    /// </summary>
    public class IllegalNumericException : Exception
    {
        #region Statics
        public static string INETitle = "Can not accept type:";
        #endregion
        #region Constructors
        public IllegalNumericException() { }
        public IllegalNumericException(string message):base(message) { }
        public IllegalNumericException(string message, 
              Exception innerException):base(message,innerException) { }
        public IllegalNumericException(Type 
                          numberType):base(INETitle+numberType.Name) { }
        #endregion
    }
    /// <summary>
    /// </summary>
    /// <remarks>
    /// bool,byte,sbyte,char
    /// decimal,double,float,
    /// int,uint,long,ulong,
    /// short,ushort
    /// </remarks>
    public class Numeric : Valueric
    {
        #region Shortcuts
        public static bool IsNumeric(Valueric valueric)
        {
            if (valueric == null)
                throw new ArgumentNullException("valueric", 
                              "valueric can not be null!");
            return IsNumeric(valueric.Value);
        }
        public static bool IsNumeric(ValueType value)
        {
            if (value != null)
            {
                Type valueType = value.GetType();
                if (valueType == typeof(bool)
                    || valueType == typeof(byte)
                    || valueType == typeof(sbyte)
                    || valueType == typeof(char)
                    || valueType == typeof(double)
                    || valueType == typeof(float)
                    || valueType == typeof(int)
                    || valueType == typeof(uint)
                    || valueType == typeof(long)
                    || valueType == typeof(ulong)
                    || valueType == typeof(short)
                    || valueType == typeof(ushort)
                    )
                    return true;
            }
            return false;
        }
        #endregion
        #region Constructors
        public Numeric():base() { }
        public Numeric(ValueType value):base(value) { }
        public Numeric(Numeric numeric):this(numeric.Value) { }
        public Numeric(Type valueType):base(valueType) { }
        #endregion
        #region Overrides
        protected override ValueType OnSetValue(ValueType value)
        {
            if (IsNumeric(value) && this.OnAccpetValueOf(value))
            {
                return base.OnSetValue(value);
            }
            else
            {
                throw new IllegalNumericException(value.GetType());
            }
        }
        protected override ValueType OnImportValue(object value)
        {
            return (value is string) ? this.OnParseString(this.GetValueType(), 
                    value as string) : base.OnImportValue(value);
        }
        protected override ValueType 
              OnExportValue(ValueType toExport, Type exportType)
        {
            Type toExportType = toExport.GetType();
            if (exportType == typeof(bool))
            {
                if (toExportType == typeof(bool))
                {
                    return toExport;
                }
                else if (
                    toExportType == typeof(byte) ||
                    toExportType == typeof(sbyte) ||
                    toExportType == typeof(char) ||
                    toExportType == typeof(decimal) ||
                    toExportType == typeof(int) ||
                    toExportType == typeof(uint) ||
                    toExportType == typeof(long) ||
                    toExportType == typeof(ulong) ||
                    toExportType == typeof(short) ||
                    toExportType == typeof(ushort)
                    )
                {
                    //false: toExport == 0
                    //true:  toExport != 0
                    return ((ulong)toExport) != 0L;
                }
            }
            else if (exportType == typeof(byte))
            {
                return (byte)toExport;
            }
            else if (exportType == typeof(sbyte))
            {
                return (sbyte)toExport;
            }
            else if (exportType == typeof(char))
            {
                return (char)toExport;
            }
            else if (exportType == typeof(decimal))
            {
                return (decimal)toExport;
            }
            else if (exportType == typeof(double))
            {
                return (double)toExport;
            }
            else if (exportType == typeof(float))
            {
                return (float)toExport;
            }
            else if (exportType == typeof(int))
            {
                return (int)toExport;
            }
            else if (exportType == typeof(uint))
            {
                return (uint)toExport;
            }
            else if (exportType == typeof(long))
            {
                return (long)toExport;
            }
            else if (exportType == typeof(ulong))
            {
                return (ulong)toExport;
            }
            else if (exportType == typeof(short))
            {
                return (short)toExport;
            }
            else if (exportType == typeof(ushort))
            {
                return (ushort)toExport;
            }
            else
            {
                throw new 
                  NumericConvertException(this.GetValueType(), exportType);
            }
            return toExport;
        }
        #endregion
        #region Virtuals
        public virtual Numeric RepresentsValueOf(string value)
        {
            this.OnSetValue(this.OnParseString(this.GetValueType(), value));
            return this;
        }
        protected virtual ValueType 
                   OnParseString(Type valueType, string value)
        {
            if (valueType == typeof(bool))
            {
                return bool.Parse(value);
            }
            else if (valueType == typeof(byte))
            {
                return byte.Parse(value);
            }
            else if (valueType == typeof(sbyte))
            {
                return sbyte.Parse(value);
            }
            else if (valueType == typeof(char))
            {
                return char.Parse(value);
            }
            else if (valueType == typeof(decimal))
            {
                return decimal.Parse(value);
            }
            else if (valueType == typeof(double))
            {
                return double.Parse(value);
            }
            else if (valueType == typeof(float))
            {
                return float.Parse(value);
            }
            else if (valueType == typeof(int))
            {
                return int.Parse(value);
            }
            else if (valueType == typeof(uint))
            {
                return uint.Parse(value);
            }
            else if (valueType == typeof(long))
            {
                return long.Parse(value);
            }
            else if (valueType == typeof(ulong))
            {
                return ulong.Parse(value);
            }
            else if (valueType == typeof(short))
            {
                return short.Parse(value);
            }
            else if (valueType == typeof(ushort))
            {
                return ushort.Parse(value);
            }
            else
            {
                throw new NumericConvertException(typeof(string), valueType);
            }
        }
        protected virtual bool OnAccpetValueOf(ValueType value)
        {
            return true;
        }
        #endregion
    }
    /// <summary>
    /// </summary>
    /// <remarks>
    /// </remarks>
    public class LogicNumeric : Numeric
    {
        #region Shortcuts
        public static implicit operator bool(LogicNumeric nLogic)
        {
            return (bool)nLogic.Value;
        }
        public static bool IsLogicNumeric(Valueric valueric)
        {
            if (valueric == null)
                throw new ArgumentNullException("valueric", 
                     "valueric can not be null!");
            return IsLogicNumeric(valueric.Value);
        }
        public static bool IsLogicNumeric(ValueType value)
        {
            if (value != null)
            {
                Type valueType = value.GetType();
                if (valueType == typeof(bool))
                    return true;
            }
            return false;
        }
        public static LogicNumeric of(bool value)
        {
            return new LogicNumeric(value);
        }
        #endregion
        #region Constructors
        public LogicNumeric():base(false) { }
        public LogicNumeric(bool value) :base(value) { }
        public LogicNumeric(Numeric numeric):this()
        {
            numeric.AssignValueTo(this);
        }
        #endregion
    }
    /// <summary>
    /// </summary>
    /// <remarks>
    /// byte,sbyte,char
    /// decimal,int,uint,
    /// long,ulong,
    /// short,ushort&#31561;&#31867;&#22411;&#12290;
    /// </remarks>
    public class IntegerNumeric : Numeric
    {
        #region Shortcuts
        public static IntegerNumeric of(byte value)
        {
            return new IntegerNumeric(value);
        }
        public static IntegerNumeric of(sbyte value)
        {
            return new IntegerNumeric(value);
        }
        public static IntegerNumeric of(char value)
        {
            return new IntegerNumeric(value);
        }
        public static IntegerNumeric of(int value)
        {
            return new IntegerNumeric(value);
        }
        public static IntegerNumeric of(uint value)
        {
            return new IntegerNumeric(value);
        }
        public static IntegerNumeric of(long value)
        {
            return new IntegerNumeric(value);
        }
        public static IntegerNumeric of(ulong value)
        {
            return new IntegerNumeric(value);
        }
        public static IntegerNumeric of(short value)
        {
            return new IntegerNumeric(value);
        }
        public static IntegerNumeric of(ushort value)
        {
            return new IntegerNumeric(value);
        }
        public static implicit operator byte(IntegerNumeric nInteger)
        {
            return (byte)nInteger.Value;
        }
        public static implicit operator sbyte(IntegerNumeric nInteger)
        {
            return (sbyte)nInteger.Value;
        }
        public static implicit operator char(IntegerNumeric nInteger)
        {
            return (char)nInteger.Value;
        }
        public static implicit operator int(IntegerNumeric nInteger)
        {
            return (int)nInteger.Value;
        }
        public static implicit operator uint(IntegerNumeric nInteger)
        {
            return (uint)nInteger.Value;
        }
        public static implicit operator long(IntegerNumeric nInteger)
        {
            return (long)nInteger.Value;
        }
        public static implicit operator ulong(IntegerNumeric nInteger)
        {
            return (ulong)nInteger.Value;
        }
        public static implicit operator short(IntegerNumeric nInteger)
        {
            return (short)nInteger.Value;
        }
        public static implicit operator ushort(IntegerNumeric nInteger)
        {
            return (ushort)nInteger.Value;
        }
        public static bool IsIntegerNumeric(Valueric valueric)
        {
            if (valueric == null)
                throw new ArgumentNullException("valueric", 
                       "valueric can not be null!");
            return IsIntegerNumeric(valueric.Value);
        }
        public static bool IsIntegerNumeric(ValueType value)
        {
            if (value != null)
            {
                Type valueType = value.GetType();
                if (valueType == typeof(byte)
                    || valueType == typeof(sbyte)
                    || valueType == typeof(char)
                    || valueType == typeof(int)
                    || valueType == typeof(uint)
                    || valueType == typeof(long)
                    || valueType == typeof(ulong)
                    || valueType == typeof(short)
                    || valueType == typeof(ushort)
                    )
                    return true;
            }
            return false;
        }
        #endregion
        #region Constructors
        public IntegerNumeric()     :base(0) { }
        public IntegerNumeric(byte value)  :base(value) { }
        public IntegerNumeric(sbyte value)  :base(value) { }
        public IntegerNumeric(char value)  :base(value) { }
        public IntegerNumeric(decimal value) :base(value) { }
        public IntegerNumeric(int value)  :base(value) { }
        public IntegerNumeric(uint value)  :base(value) { }
        public IntegerNumeric(long value)  :base(value) { }
        public IntegerNumeric(ulong value)  :base(value) { }
        public IntegerNumeric(short value)  :base(value) { }
        public IntegerNumeric(ushort value)  :base(value) { }
        public IntegerNumeric(Numeric numeric):this()
        {
            numeric.AssignValueTo(this);
        }
        #endregion
    }
    /// <summary>
    /// </summary>
    /// <remarks>
    /// </remarks>
    public class FloatingNumeric : Numeric
    {
        #region Shortcuts
        public static FloatingNumeric of(float value)
        {
            return new FloatingNumeric(value);
        }
        public static FloatingNumeric of(double value)
        {
            return new FloatingNumeric(value);
        }
        public static FloatingNumeric of(decimal value)
        {
            return new FloatingNumeric(value);
        }
        public static implicit operator float(FloatingNumeric nFloating)
        {
            return (float)nFloating.Value;
        }
        public static implicit operator double(FloatingNumeric nFloating)
        {
            return (double)nFloating.Value;
        }
        public static implicit operator decimal(FloatingNumeric nFloating)
        {
            return (decimal)nFloating.Value;
        }
        public static bool IsFloatingNumeric(Valueric valueric)
        {
            if (valueric == null)
                throw new ArgumentNullException("valueric", 
                        "valueric can not be null!");
            return IsFloatingNumeric(valueric.Value);
        }
        public static bool IsFloatingNumeric(ValueType value)
        {
            if (value != null)
            {
                Type valueType = value.GetType();
                if (valueType == typeof(decimal) ||
                    valueType == typeof(double) ||
                    valueType == typeof(float)
                    )
                    return true;
            }
            return false;
        }
        #endregion
        #region Constructors
        public FloatingNumeric()     :base(0.0f ) { }
        public FloatingNumeric(float value)   :base(value) { }
        public FloatingNumeric(double value)  :base(value) { }
        public FloatingNumeric(decimal value)  :base(value) { }
        public FloatingNumeric(Numeric numeric):this()
        {
            numeric.AssignValueTo(this);
        }
        #endregion
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

yilin_yang
Software Developer (Senior) NOC
China China
Please view my resume at
 
http://www.rentacoder.com/RentACoder/SoftwareCoders/showBioInfo.asp?lngAuthorId=1309793

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 27 Sep 2004
Article Copyright 2004 by yilin_yang
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid