Ejemplo n.º 1
0
        /// <summary>
        /// Create a new enumeration based on a string
        /// </summary>
        /// <param name="value">The enumerate string, flags are separated with | characters</param>
        public PortableEnum FromString(string value)
        {
            PortableEnum ret = this;

            if (_isFlags)
            {
                string[] vals    = value.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                long     currVal = 0;

                if (vals.Length > 0)
                {
                    foreach (string s in vals)
                    {
                        currVal |= FindValueByName(s);
                    }

                    ret._value = currVal;
                }
                else
                {
                    throw new ArgumentException(CANAPE.Properties.Resources.EnumDataValue_FromString, "value");
                }
            }
            else
            {
                ret._value = FindValueByName(value);
            }

            return(ret);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="value">Enumerated value</param>
        /// <param name="name">Name of the value</param>
        /// <param name="underlyingType">The underlying type which this serialized to</param>
        /// <param name="littleEndian">The endian of the underlying type</param>
        public EnumDataValue(string name, Enum value, Type underlyingType, bool littleEndian)
            : base(name, littleEndian)
        {
            _value       = new PortableEnum(value.GetType());
            _value.Value = (long)Convert.ChangeType(value, typeof(long));

            _underlyingType = underlyingType;

            if (_underlyingType == typeof(byte))
            {
                _toMethodInfo   = typeof(EnumDataValue).GetMethod("FromByte", BindingFlags.Static | BindingFlags.NonPublic);
                _fromMethodInfo = typeof(EnumDataValue).GetMethod("ToByte", BindingFlags.Static | BindingFlags.NonPublic);
            }
            else if (_underlyingType == typeof(sbyte))
            {
                _toMethodInfo   = typeof(EnumDataValue).GetMethod("FromSByte", BindingFlags.Static | BindingFlags.NonPublic);
                _fromMethodInfo = typeof(EnumDataValue).GetMethod("ToSByte", BindingFlags.Static | BindingFlags.NonPublic);
            }
            else
            {
                _toMethodInfo = typeof(BitConverter).GetMethod("GetBytes", BindingFlags.Public | BindingFlags.Static, null, new Type[] { _underlyingType }, null);

                foreach (MethodInfo mi in typeof(BitConverter).GetMethods(BindingFlags.Public | BindingFlags.Static))
                {
                    if (mi.Name.StartsWith("To") && (mi.ReturnType == _underlyingType))
                    {
                        ParameterInfo[] pis = mi.GetParameters();
                        if ((pis.Length == 2) && (pis[0].ParameterType == typeof(byte[])) && (pis[1].ParameterType == typeof(int)))
                        {
                            _fromMethodInfo = mi;
                            break;
                        }
                    }
                }
            }

            if ((_toMethodInfo == null) || (_fromMethodInfo == null))
            {
                throw new ArgumentException(CANAPE.Properties.Resources.EnumDataValue_InvalidType);
            }

            if (_value.IsFlags)
            {
                Class = new Guid(DataNodeClasses.FLAGS_ENUM_NODE_CLASS);
            }
            else
            {
                Class = new Guid(DataNodeClasses.ENUM_NODE_CLASS);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create a new enumerated value based this value
        /// </summary>
        /// <param name="value">The value to use, must be convertable to a long</param>
        public PortableEnum FromValue(object value)
        {
            PortableEnum ret = this;

            if (value is string)
            {
                ret = FromString((string)value);
            }
            else if (value is PortableEnum)
            {
                ret._value = ((PortableEnum)value)._value;
            }
            else
            {
                ret._value = (long)Convert.ChangeType(value, typeof(long));
            }

            return(ret);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="value">Enumerated value</param>
        /// <param name="name">Name of the value</param>
        /// <param name="underlyingType">The underlying type which this serialized to</param>        
        /// <param name="littleEndian">The endian of the underlying type</param>
        public EnumDataValue(string name, Enum value, Type underlyingType, bool littleEndian)
            : base(name, littleEndian)
        {
            _value = new PortableEnum(value.GetType());
            _value.Value = (long)Convert.ChangeType(value, typeof(long));

            _underlyingType = underlyingType;

            if (_underlyingType == typeof(byte))
            {
                _toMethodInfo = typeof(EnumDataValue).GetMethod("FromByte", BindingFlags.Static | BindingFlags.NonPublic);
                _fromMethodInfo = typeof(EnumDataValue).GetMethod("ToByte", BindingFlags.Static | BindingFlags.NonPublic);
            }
            else if (_underlyingType == typeof(sbyte))
            {
                _toMethodInfo = typeof(EnumDataValue).GetMethod("FromSByte", BindingFlags.Static | BindingFlags.NonPublic);
                _fromMethodInfo = typeof(EnumDataValue).GetMethod("ToSByte", BindingFlags.Static | BindingFlags.NonPublic);
            }
            else
            {
                _toMethodInfo = typeof(BitConverter).GetMethod("GetBytes", BindingFlags.Public | BindingFlags.Static, null, new Type[] { _underlyingType }, null);

                foreach (MethodInfo mi in typeof(BitConverter).GetMethods(BindingFlags.Public | BindingFlags.Static))
                {
                    if (mi.Name.StartsWith("To") && (mi.ReturnType == _underlyingType))
                    {
                        ParameterInfo[] pis = mi.GetParameters();
                        if ((pis.Length == 2) && (pis[0].ParameterType == typeof(byte[])) && (pis[1].ParameterType == typeof(int)))
                        {
                            _fromMethodInfo = mi;
                            break;
                        }
                    }
                }
            }

            if ((_toMethodInfo == null) || (_fromMethodInfo == null))
            {
                throw new ArgumentException(CANAPE.Properties.Resources.EnumDataValue_InvalidType);
            }

            if (_value.IsFlags)
            {
                Class = new Guid(DataNodeClasses.FLAGS_ENUM_NODE_CLASS);
            }
            else
            {
                Class = new Guid(DataNodeClasses.ENUM_NODE_CLASS);
            }
        }