Esempio n. 1
0
 public static T FromXDoc(XElement xe)
 {
     // XElement xe = XElement.Load(new StringReader(xml));
     return(Proto <T> .Read(xe));
 }
Esempio n. 2
0
        private static IProtoValue CreateProtoValue(Type type)
        {
            if (type == typeof(string))
            {
                return(new StringValue());
            }
            else if (type == typeof(bool))
            {
                return(new BooleanValue());
            }
            else if (type == typeof(byte))
            {
                return(new ByteValue());
            }
            else if (type == typeof(short))
            {
                return(new Int16Value());
            }
            else if (type == typeof(ushort))
            {
                return(new UInt16Value());
            }
            else if (type == typeof(int))
            {
                return(new Int32Value());
            }
            else if (type == typeof(uint))
            {
                return(new UInt32Value());
            }
            else if (type == typeof(long))
            {
                return(new Int64Value());
            }
            else if (type == typeof(ulong))
            {
                return(new UInt64Value());
            }
            else if (type == typeof(double))
            {
                return(new DoubleValue());
            }
            else if (type == typeof(DateTime))
            {
                return(new DateTimeValue());
            }
            else if (type == typeof(TimeSpan))
            {
                return(new TimeSpanValue());
            }
            else if (type == typeof(Guid))
            {
                return(new GuidValue());
            }
            else if (type == typeof(byte[]))
            {
                return(new BinaryValue());
            }
            else if (type.IsArray)
            {
                Type        elementType     = type.GetElementType();
                IProtoValue elementMetadata = Proto <T> .CreateProtoValue(elementType);

                Type genericDef = typeof(ArrayValue <int>).GetGenericTypeDefinition();
                Type target     = genericDef.MakeGenericType(elementType);
                return((IProtoValue)Activator.CreateInstance(target, elementMetadata));
            }
            else if (type.GetTypeInfo().IsGenericType &&
                     type.GetGenericTypeDefinition() == typeof(List <int>).GetGenericTypeDefinition())
            {
                Type        elementType     = type.GetGenericArguments()[0];
                IProtoValue elementMetadata = Proto <T> .CreateProtoValue(elementType);

                Type genericDef = typeof(ListValue <int>).GetGenericTypeDefinition();
                Type target     = genericDef.MakeGenericType(elementType);
                return((IProtoValue)Activator.CreateInstance(target, elementMetadata));
            }
            else if (type.GetTypeInfo().IsGenericType &&
                     type.GetGenericTypeDefinition() == typeof(HashSet <int>).GetGenericTypeDefinition())
            {
                Type        elementType     = type.GetGenericArguments()[0];
                IProtoValue elementMetadata = Proto <T> .CreateProtoValue(elementType);

                Type genericDef = typeof(SetValue <int>).GetGenericTypeDefinition();
                Type target     = genericDef.MakeGenericType(elementType);
                return((IProtoValue)Activator.CreateInstance(target, elementMetadata));
            }
            else if (type.GetTypeInfo().IsGenericType &&
                     type.GetGenericTypeDefinition() == typeof(Dictionary <int, int>).GetGenericTypeDefinition())
            {
                Type        keyType     = type.GetGenericArguments()[0];
                IProtoValue keyMetadata = Proto <T> .CreateProtoValue(keyType);

                Type        elementType     = type.GetGenericArguments()[1];
                IProtoValue elementMetadata = Proto <T> .CreateProtoValue(elementType);

                Type genericDef = typeof(MapValue <int, int>).GetGenericTypeDefinition();
                Type target     = genericDef.MakeGenericType(keyType, elementType);
                return((IProtoValue)Activator.CreateInstance(target, keyMetadata, elementMetadata));
            }
            else if (type.HasCustomAttribute <ProtoAttribute>())
            {
                Type genericDef = typeof(StructValue <T>).GetGenericTypeDefinition();
                Type target     = genericDef.MakeGenericType(type);
                return((IProtoValue)Activator.CreateInstance(target));
            }
            else
            {
                throw new ProtoException(ProtoErrorCode.InvalidArgument, "failed to create proto column for type {0}", type);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Initialize proto metadata
        /// </summary>
        /// <returns></returns>
        private static Proto <T> Initialize()
        {
            Type type = typeof(T);

            if (!type.HasCustomAttribute <ProtoAttribute>())
            {
                throw new ProtoException(ProtoErrorCode.InvalidArgument, "ProtoAttribute is not defined for type {0}", type.FullName);
            }

            BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly;

            List <IProtoColumn <T> > propColumns = new List <IProtoColumn <T> >();

            foreach (PropertyInfo propInfo in type.GetProperties(bindingFlags))
            {
                if (propInfo.HasCustomAttribute <ProtoColumnAttribute>())
                {
                    IProtoColumn <T> protoColumn = Proto <T> .CreateProtoColumn(propInfo);

                    propColumns.Add(protoColumn);
                }
            }

            HashSet <string> nameChecks = new HashSet <string>();
            HashSet <short>  idChecks   = new HashSet <short>();

            int maxID = 0;
            int minID = 1;
            int count = 0;

            foreach (IProtoColumn <T> col in propColumns)
            {
                if (!string.IsNullOrEmpty(col.Name))
                {
                    if (nameChecks.Contains(col.Name))
                    {
                        throw new ProtoException(ProtoErrorCode.InvalidArgument, "Duplicated column name {0}", col.Name);
                    }
                    else
                    {
                        nameChecks.Add(col.Name);
                    }
                }

                if (idChecks.Contains(col.ID))
                {
                    throw new ProtoException(ProtoErrorCode.InvalidArgument, "Duplicated column ID {0}", col.ID);
                }
                else
                {
                    idChecks.Add(col.ID);
                }

                count++;
                minID = Math.Min(col.ID, minID);
                maxID = Math.Max(col.ID, maxID);
            }

            // if (minID != 1 || maxID != count)
            // {
            //   throw new ProtoException(ProtoErrorCode.InvalidArgument, "Column IDs are not consecutive starting from 1");
            // }

            if (minID <= 0 && maxID > short.MaxValue)
            {
                throw new ProtoException(ProtoErrorCode.InvalidArgument, "Column IDs must be unique and between 1 to 65535");
            }

            IProtoColumn <T>[] columns = new IProtoColumn <T> [maxID + 1];
            foreach (IProtoColumn <T> column in propColumns)
            {
                columns[column.ID] = column;
            }

            Proto <T> proto = new Proto <T>(columns);

            ParameterExpression iprot    = Expression.Parameter(protType);
            ParameterExpression ivalue   = Expression.Parameter(typeof(T));
            Expression          readExpr = Read(iprot, ivalue, columns);

            proto.reader = Expression.Lambda <Action <TProtocol, T> >(readExpr, iprot, ivalue).Compile();

            ParameterExpression oprot     = Expression.Parameter(protType);
            ParameterExpression ovalue    = Expression.Parameter(typeof(T));
            Expression          writeExpr = Write(oprot, ovalue, columns);

            proto.writer = Expression.Lambda <Action <TProtocol, T> >(writeExpr, oprot, ovalue).Compile();

            return(proto);
        }
Esempio n. 4
0
        private static IProtoColumn <T> CreateProtoColumn(PropertyInfo propInfo)
        {
            Type type         = typeof(T);
            Type propertyType = propInfo.PropertyType;


            LambdaExpression getter = null;
            LambdaExpression setter = null;

            Expression defaultExpr = Expression.Default(propertyType);

            Type getterType = typeof(Func <T, int>)
                              .GetGenericTypeDefinition()
                              .MakeGenericType(typeof(T), propertyType);

            Type setterType = typeof(Action <T, int>)
                              .GetGenericTypeDefinition()
                              .MakeGenericType(typeof(T), propertyType);

            if (propInfo.CanRead)
            {
                ParameterExpression objectParameter = Expression.Parameter(type);
                Expression          propertyExpr    = Expression.Property(objectParameter, propInfo);
                getter = Expression.Lambda(getterType, propertyExpr, objectParameter);
            }
            else
            {
                // return default value if the property has no getter method.
                ParameterExpression objectParameter = Expression.Parameter(type);
                getter = Expression.Lambda(getterType, defaultExpr, objectParameter);
            }

            if (propInfo.CanWrite)
            {
                ParameterExpression objectParameter = Expression.Parameter(type);
                ParameterExpression valueParameter  = Expression.Parameter(propertyType);
                Expression          propertyExpr    = Expression.Property(objectParameter, propInfo);
                Expression          assignExpr      = Expression.Assign(propertyExpr, valueParameter);
                setter = Expression.Lambda(setterType, assignExpr, objectParameter, valueParameter);
            }
            else
            {
                ParameterExpression objectParameter = Expression.Parameter(type);
                ParameterExpression valueParameter  = Expression.Parameter(propertyType);
                Expression          emptyExpr       = Expression.Empty();
                setter = Expression.Lambda(setterType, emptyExpr, objectParameter, valueParameter);
            }

            ProtoColumnAttribute protoColumnAttribute = propInfo.GetCustomAttribute <ProtoColumnAttribute>();
            IProtoValue          valueMetadata        = Proto <T> .CreateProtoValue(propertyType);

            Type genericDef = typeof(ProtoColumn <T, int>).GetGenericTypeDefinition();
            Type target     = genericDef.MakeGenericType(typeof(T), propertyType);

            ConstructorInfo constructorInfo = target.GetConstructors()[0];

            return((IProtoColumn <T>)constructorInfo.Invoke(
                       new object[]
            {
                propInfo,
                propInfo.Name,
                protoColumnAttribute.ID,
                valueMetadata,
                Expression.Lambda(defaultExpr).Compile().DynamicInvoke(),
                getter.Compile(),
                setter.Compile()
            }
                       ));
        }
Esempio n. 5
0
        public static T Read(TProtocol iprot)
        {
            T value = new T();

            return(Proto <T> .Read(iprot, value));
        }