Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BoolSchema"/> class.
 /// </summary>
 public BoolSchema()
 {
     Serializer = new BoolSerializer();
 }
        private ClassInfo <T> RegisterInternal <T>()
        {
            if (ClassInfo <T> .Instance != null)
            {
                return(ClassInfo <T> .Instance);
            }

            var t     = typeof(T);
            var props = t.GetProperties(
                BindingFlags.Instance |
                BindingFlags.Public |
                BindingFlags.GetProperty |
                BindingFlags.SetProperty);
            var serializers = new List <FastCall <T> >();

            for (var i = 0; i < props.Length; i++)
            {
                var property     = props[i];
                var propertyType = property.PropertyType;

                var elementType = propertyType.IsArray ? propertyType.GetElementType() : propertyType;
                var callType    = propertyType.IsArray ? CallType.Array : CallType.Basic;

                if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List <>))
                {
                    elementType = propertyType.GetGenericArguments()[0];
                    callType    = CallType.List;
                }

                var getMethod = property.GetGetMethod();
                var setMethod = property.GetSetMethod();
                if (getMethod == null || setMethod == null)
                {
                    continue;
                }

                FastCall <T> serialzer = null;
                if (propertyType.IsEnum)
                {
                    var underlyingType = Enum.GetUnderlyingType(propertyType);
                    if (underlyingType == typeof(byte))
                    {
                        serialzer = new EnumByteSerializer <T>(property, propertyType);
                    }
                    else if (underlyingType == typeof(int))
                    {
                        serialzer = new EnumIntSerializer <T>(property, propertyType);
                    }
                    else
                    {
                        throw new InvalidTypeException("Not supported enum underlying type: " + underlyingType.Name);
                    }
                }
                else if (elementType == typeof(string))
                {
                    serialzer = new StringSerializer <T>(_maxStringLength);
                }
                else if (elementType == typeof(bool))
                {
                    serialzer = new BoolSerializer <T>();
                }
                else if (elementType == typeof(byte))
                {
                    serialzer = new ByteSerializer <T>();
                }
                else if (elementType == typeof(sbyte))
                {
                    serialzer = new SByteSerializer <T>();
                }
                else if (elementType == typeof(short))
                {
                    serialzer = new ShortSerializer <T>();
                }
                else if (elementType == typeof(ushort))
                {
                    serialzer = new UShortSerializer <T>();
                }
                else if (elementType == typeof(int))
                {
                    serialzer = new IntSerializer <T>();
                }
                else if (elementType == typeof(uint))
                {
                    serialzer = new UIntSerializer <T>();
                }
                else if (elementType == typeof(long))
                {
                    serialzer = new LongSerializer <T>();
                }
                else if (elementType == typeof(ulong))
                {
                    serialzer = new ULongSerializer <T>();
                }
                else if (elementType == typeof(float))
                {
                    serialzer = new FloatSerializer <T>();
                }
                else if (elementType == typeof(double))
                {
                    serialzer = new DoubleSerializer <T>();
                }
                else if (elementType == typeof(char))
                {
                    serialzer = new CharSerializer <T>();
                }
                else if (elementType == typeof(IPEndPoint))
                {
                    serialzer = new IPEndPointSerializer <T>();
                }
                else
                {
                    CustomType customType;
                    _registeredTypes.TryGetValue(elementType, out customType);
                    if (customType != null)
                    {
                        serialzer = customType.Get <T>();
                    }
                }

                if (serialzer != null)
                {
                    serialzer.Init(getMethod, setMethod, callType);
                    serializers.Add(serialzer);
                }
                else
                {
                    throw new InvalidTypeException("Unknown property type: " + propertyType.FullName);
                }
            }

            ClassInfo <T> .Instance = new ClassInfo <T>(serializers);
            return(ClassInfo <T> .Instance);
        }
Exemple #3
0
        private ClassInfo <T> RegisterInternal <T>()
        {
            if (ClassInfo <T> .Instance != null)
            {
                return(ClassInfo <T> .Instance);
            }

            Type t = typeof(T);

            PropertyInfo[] props = t.GetProperties(
                BindingFlags.Instance |
                BindingFlags.Public |
                BindingFlags.GetProperty |
                BindingFlags.SetProperty);
            List <FastCall <T> > serializers = new List <FastCall <T> >();

            for (int i = 0; i < props.Length; i++)
            {
                PropertyInfo property     = props[i];
                Type         propertyType = property.PropertyType;

                Type     elementType = propertyType.IsArray ? propertyType.GetElementType() : propertyType;
                CallType callType    = propertyType.IsArray ? CallType.Array : CallType.Basic;

                if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List <>))
                {
                    elementType = propertyType.GetGenericArguments()[0];
                    callType    = CallType.List;
                }

                // Note from Cod: Required to get it to build
                // TODO: Fix this

                /*if (Attribute.IsDefined(property, typeof(IgnoreDataMemberAttribute)))
                 *  continue;*/

                MethodInfo getMethod = property.GetGetMethod();
                MethodInfo setMethod = property.GetSetMethod();
                if (getMethod == null || setMethod == null)
                {
                    continue;
                }

                FastCall <T> serialzer = null;
                if (propertyType.IsEnum)
                {
                    Type underlyingType = Enum.GetUnderlyingType(propertyType);
                    if (underlyingType == typeof(byte))
                    {
                        serialzer = new EnumByteSerializer <T>(property, propertyType);
                    }
                    else if (underlyingType == typeof(int))
                    {
                        serialzer = new EnumIntSerializer <T>(property, propertyType);
                    }
                    else
                    {
                        throw new InvalidTypeException("Not supported enum underlying type: " + underlyingType.Name);
                    }
                }
                else if (elementType == typeof(string))
                {
                    serialzer = new StringSerializer <T>(_maxStringLength);
                }
                else if (elementType == typeof(bool))
                {
                    serialzer = new BoolSerializer <T>();
                }
                else if (elementType == typeof(byte))
                {
                    serialzer = new ByteSerializer <T>();
                }
                else if (elementType == typeof(sbyte))
                {
                    serialzer = new SByteSerializer <T>();
                }
                else if (elementType == typeof(short))
                {
                    serialzer = new ShortSerializer <T>();
                }
                else if (elementType == typeof(ushort))
                {
                    serialzer = new UShortSerializer <T>();
                }
                else if (elementType == typeof(int))
                {
                    serialzer = new IntSerializer <T>();
                }
                else if (elementType == typeof(uint))
                {
                    serialzer = new UIntSerializer <T>();
                }
                else if (elementType == typeof(long))
                {
                    serialzer = new LongSerializer <T>();
                }
                else if (elementType == typeof(ulong))
                {
                    serialzer = new ULongSerializer <T>();
                }
                else if (elementType == typeof(float))
                {
                    serialzer = new FloatSerializer <T>();
                }
                else if (elementType == typeof(double))
                {
                    serialzer = new DoubleSerializer <T>();
                }
                else if (elementType == typeof(char))
                {
                    serialzer = new CharSerializer <T>();
                }
                else if (elementType == typeof(IPEndPoint))
                {
                    serialzer = new IPEndPointSerializer <T>();
                }
                else
                {
                    _registeredTypes.TryGetValue(elementType, out CustomType customType);
                    if (customType != null)
                    {
                        serialzer = customType.Get <T>();
                    }
                }

                if (serialzer != null)
                {
                    serialzer.Init(getMethod, setMethod, callType);
                    serializers.Add(serialzer);
                }
                else
                {
                    throw new InvalidTypeException("Unknown property type: " + propertyType.FullName);
                }
            }
            ClassInfo <T> .Instance = new ClassInfo <T>(serializers);
            return(ClassInfo <T> .Instance);
        }