Exemplo n.º 1
0
 public void TypeModel_Struct_NonVirtualProperty()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(NonVirtualPropertyStruct <byte>)));
 }
 /// <summary>
 /// Apply include mappings via assembly reflection (<see cref="ProtoBaseAttribute"/>).<br />
 /// Important: This must be called before any serialization happen.
 /// </summary>
 /// <param name="model">The RuntimeTypeModel</param>
 /// <param name="assemblyRegexFilter">The regex assembly filer</param>
 /// <param name="implicitFields">The default import of base type fields, if nothing is specified.</param>
 /// <exception cref="ArgumentException"></exception>
 /// <exception cref="ArgumentNullException"></exception>
 /// <exception cref="InvalidDataException"></exception>
 public static void ApplyProtoBase(this RuntimeTypeModel model, Regex assemblyRegexFilter, ImplicitFields implicitFields = ImplicitFields.AllPublic)
 {
     model.ApplyProtoBase(assembly =>
                          assemblyRegexFilter == null || assemblyRegexFilter.IsMatch(assembly.FullName), implicitFields);
 }
Exemplo n.º 3
0
        public static void Configure(RuntimeTypeModel model)
        {
            var type = model.Add(typeof(AgGateway.ADAPT.ApplicationDataModel.Guidance.Spiral), Constants.UseDefaults);

            type.AddField(305, nameof(AgGateway.ADAPT.ApplicationDataModel.Guidance.Spiral.Shape));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Applies a protocol-buffer stream to an existing instance, using length-prefixed
        /// data - useful with network IO.
        /// </summary>
        /// <typeparam name="T">The type being merged.</typeparam>
        /// <param name="instance">The existing instance to be modified (can be null).</param>
        /// <param name="source">The binary stream to apply to the instance (cannot be null).</param>
        /// <param name="style">How to encode the length prefix.</param>
        /// <returns>The updated instance; this may be different to the instance argument if
        /// either the original instance was null, or the stream defines a known sub-type of the
        /// original instance.</returns>
        public static T MergeWithLengthPrefix <T>(Stream source, T instance, PrefixStyle style)
        {
            RuntimeTypeModel model = RuntimeTypeModel.Default;

            return((T)model.DeserializeWithLengthPrefix(source, instance, model.MapType(typeof(T)), style, 0));
        }
Exemplo n.º 5
0
 public override void Work(RuntimeTypeModel model)
 {
     SetSurrogate <Color, ColorSurrogate>(model);
 }
Exemplo n.º 6
0
 public void TypeModel_Struct_VectorNotAllowed()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(GenericStruct <Memory <byte> >)));
 }
Exemplo n.º 7
0
 public void TypeModel_Struct_Misnumbered()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(MisnumberedStruct <byte>)));
 }
Exemplo n.º 8
0
 public void TypeModel_Vector_VectorNotAllowed()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(GenericTable <IList <IList <int> > >)));
 }
Exemplo n.º 9
0
 public void TypeModel_Enum_UntaggedEnumNotAllowed()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(UntaggedEnum)));
 }
Exemplo n.º 10
0
 public void TypeModel_Struct_InterfaceImplementationVirtual()
 {
     RuntimeTypeModel.CreateFrom(typeof(InterfaceStructSuccess));
 }
Exemplo n.º 11
0
 public void TypeModel_TypeCantBeTableAndStruct()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(InvalidTableAndStruct)));
 }
Exemplo n.º 12
0
 public void TypeModel_Struct_DefaultValue()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(StructWithDefaultValue)));
 }
Exemplo n.º 13
0
 public void TypeModel_Struct_Abstract()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(AbstractStruct <byte>)));
 }
Exemplo n.º 14
0
 public void TypeModel_Struct_DoesNotInheritFromObject()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(DoesNotInheritFromObjectStruct <byte>)));
 }
Exemplo n.º 15
0
 public void TypeModel_Table_InterfaceImplementationNonVirtual()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(InterfaceTableFailure)));
 }
Exemplo n.º 16
0
 public void TypeModel_SortedVector_OfScalar_NotAllowed()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(SortedVector <int[]>)));
 }
Exemplo n.º 17
0
 public void TypeModel_Table_InterfaceImplementationVirtual()
 {
     RuntimeTypeModel.CreateFrom(typeof(InterfaceTableSuccess));
 }
Exemplo n.º 18
0
 public void TypeModel_Table_UnrecognizedPropertyType()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(GenericTable <Dictionary <string, string> >)));
 }
Exemplo n.º 19
0
 public void TypeModel_Struct_UnionNotAllowed()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(GenericStruct <FlatBufferUnion <string, GenericTable <string> > >)));
 }
Exemplo n.º 20
0
 public void TypeModel_SortedVector_OfTableWithEnumKey_NotAllowed()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(SortedVector <IList <SortedVectorKeyTable <TaggedEnum> > >)));
 }
Exemplo n.º 21
0
        public static void Configure(RuntimeTypeModel model)
        {
            var type = model.Add(typeof(AgGateway.ADAPT.ApplicationDataModel.ReferenceLayers.ShapeReferenceLayer), Constants.UseDefaults);

            type.AddField(1, nameof(AgGateway.ADAPT.ApplicationDataModel.ReferenceLayers.ShapeReferenceLayer.ShapeLookups));
        }
Exemplo n.º 22
0
 public void TypeModel_SortedVector_OfTableWithMultipleKeys_NotAllowed()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(SortedVector <IList <SortedVectorMultiKeyTable <string> > >)));
 }
Exemplo n.º 23
0
        /// <summary>
        /// Writes a protocol-buffer representation of the given instance to the supplied stream,
        /// with a length-prefix. This is useful for socket programming,
        /// as DeserializeWithLengthPrefix/MergeWithLengthPrefix can be used to read the single object back
        /// from an ongoing stream.
        /// </summary>
        /// <typeparam name="T">The type being serialized.</typeparam>
        /// <param name="instance">The existing instance to be serialized (cannot be null).</param>
        /// <param name="style">How to encode the length prefix.</param>
        /// <param name="destination">The destination stream to write to.</param>
        /// <param name="fieldNumber">The tag used as a prefix to each record (only used with base-128 style prefixes).</param>
        public static void SerializeWithLengthPrefix <T>(Stream destination, T instance, PrefixStyle style, int fieldNumber)
        {
            RuntimeTypeModel model = RuntimeTypeModel.Default;

            model.SerializeWithLengthPrefix(destination, instance, model.MapType(typeof(T)), style, fieldNumber);
        }
Exemplo n.º 24
0
 public void TypeModel_Table_DuplicateIndex()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(DuplicateNumberTable)));
 }
 /// <summary>
 /// Apply include mappings via assembly reflection (<see cref="ProtoBaseAttribute"/>).<br />
 /// Important: This must be called before any serialization happen.
 /// </summary>
 /// <param name="model">The RuntimeTypeModel</param>
 /// <param name="implicitFields">The default import of base type fields, if nothing is specified.</param>
 /// <exception cref="ArgumentException"></exception>
 /// <exception cref="InvalidDataException"></exception>
 public static void ApplyProtoBase(this RuntimeTypeModel model, ImplicitFields implicitFields = ImplicitFields.AllPublic)
 {
     model.ApplyProtoBase(assembly => true, implicitFields);
 }
Exemplo n.º 26
0
 public void TypeModel_Union_EnumsNotAllowed()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(GenericTable <FlatBufferUnion <string, TaggedEnum> >)));
 }
        /// <summary>
        /// Apply include mappings via assembly reflection (<see cref="ProtoBaseAttribute"/>).<br />
        /// Important: This must be called before any serialization happen.
        /// </summary>
        /// <param name="model">The RuntimeTypeModel</param>
        /// <param name="assemblyFilter">The custom assembly filter</param>
        /// <param name="implicitFields">The default import of base type fields, if nothing is specified.</param>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="InvalidDataException"></exception>
        public static void ApplyProtoBase(this RuntimeTypeModel model, Func <Assembly, bool> assemblyFilter, ImplicitFields implicitFields = ImplicitFields.AllPublic)
        {
            AppDomain.CurrentDomain.GetAssemblies().Where(a => assemblyFilter?.Invoke(a) != false)
            .AsParallel().SelectMany(a =>
                                     a.GetTypes().Where(t => t.IsClass && t.GetCustomAttributes <ProtoBaseAttribute>(false).Any()))
            .ForAll(t =>
            {
                foreach (var attr in t.GetCustomAttributes <ProtoBaseAttribute>(false))
                {
                    if (attr.ConcreteType != null)
                    {
                        if (t.IsGenericType)
                        {
                            if (!attr.ConcreteType.IsGenericType ||
                                attr.ConcreteType.GetGenericTypeDefinition() != t)
                            {
                                throw new ArgumentException("Concrete type must be assignable from this type");
                            }
                        }
                        else if (!t.IsAssignableFrom(attr.ConcreteType))
                        {
                            throw new ArgumentException("Concrete type must be assignable from this type");
                        }
                    }

                    var type = attr.ConcreteType ?? t;

                    if (type.BaseType == null)
                    {
                        throw new ArgumentException("BaseType cannot be null");
                    }

                    if (type.IsGenericType && type.GenericTypeArguments.Length == 0)
                    {
                        throw new InvalidDataException($"Cannot add generic type without concrete definition. '{type.FullName}'");
                    }

                    Type baseType;
                    if (attr.InterfaceType != null)
                    {
                        if (!attr.InterfaceType.IsInterface)
                        {
                            throw new ArgumentException($"The attributed InterfaceType must be an interface. '{attr.InterfaceType.FullName}'");
                        }

                        if (!attr.InterfaceType.IsAssignableFrom(type))
                        {
                            throw new ArgumentException(
                                $"BaseType '{attr.InterfaceType.FullName}' must be assignable from type {type.FullName}.");
                        }

                        baseType = attr.InterfaceType;
                    }
                    else
                    {
                        if (type.BaseType == typeof(object))
                        {
                            baseType = type.GetInterfaces().FirstOrDefault();
                            if (baseType == null)
                            {
                                throw new ArgumentException($"BaseType cannot be of type '{typeof(object).FullName}' and no interfaces are defined.");
                            }
                        }
                        else
                        {
                            baseType = type.BaseType;
                        }
                    }

                    var metaType = model.Add(type, true);
                    if (metaType.BaseType != null && metaType.BaseType.Type != baseType)
                    {
                        throw new InvalidDataException(
                            $"Type '{type.FullName}' has already an assigned base type '{metaType.BaseType.Type.FullName}'");
                    }

                    MetaType baseMetaType;
                    if (baseType.IsClass &&
                        baseType.GetCustomAttribute <DataContractAttribute>() == null &&
                        baseType.GetCustomAttribute <ProtoContractAttribute>() == null &&
                        model.GetTypes().OfType <MetaType>().All(a => a.Type != baseType))
                    {
                        baseMetaType = model.Add(baseType, true);

                        void ApplyImplicitFields(BindingFlags bindingFlags)
                        {
                            var fields = baseType
                                         .GetProperties(bindingFlags | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                                         .Cast <MemberInfo>()
                                         .Concat(baseType.GetFields(
                                                     bindingFlags | BindingFlags.Instance | BindingFlags.DeclaredOnly)).ToList();

                            if (fields.Any(a =>
                                           a.GetCustomAttribute <DataMemberAttribute>() != null ||
                                           a.GetCustomAttribute <ProtoMemberAttribute>() != null))
                            {
                                return;
                            }

                            var fieldNumber = 1;
                            foreach (var name in fields.Where(x =>
                                                              x.GetCustomAttribute <IgnoreDataMemberAttribute>() == null &&
                                                              x.GetCustomAttribute <ProtoIgnoreAttribute>() == null &&
                                                              x.GetCustomAttribute <CompilerGeneratedAttribute>() == null)
                                     .Select(a => a.Name))
                            {
                                baseMetaType.Add(fieldNumber++, name);
                            }
                        }

                        switch (implicitFields)
                        {
                        case ImplicitFields.AllPublic:
                            ApplyImplicitFields(BindingFlags.Public);
                            break;

                        case ImplicitFields.AllFields:
                            ApplyImplicitFields(BindingFlags.Public | BindingFlags.NonPublic);
                            break;

                        case ImplicitFields.None:
                            // Do noting
                            break;
                        }
                    }
                    else
                    {
                        baseMetaType = model.Add(baseType, true);
                    }

                    var rootMetaType = baseMetaType;
                    while (rootMetaType.BaseType != null)
                    {
                        rootMetaType = rootMetaType.BaseType;
                    }

                    void ValidateMetaTypes(MetaType mt)
                    {
                        if (mt.HasSubtypes)
                        {
                            if (mt.GetSubtypes().Any(a => a.FieldNumber == attr.FieldNumber))
                            {
                                throw new InvalidDataException(
                                    $"Cannot assign field number '{attr.FieldNumber}' for '{type.FullName}' on '{baseType.FullName}'. It's already being assigned to '{mt.Type.FullName}' in the hierarchy.");
                            }

                            foreach (var en in mt.GetSubtypes())
                            {
                                ValidateMetaTypes(en.DerivedType);
                            }
                        }
                    }

                    ValidateMetaTypes(rootMetaType);
                    baseMetaType.AddSubType(attr.FieldNumber, type);
                }
            });
        }
Exemplo n.º 28
0
 public void TypeModel_Table_InternalSetter()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(InternalSetter)));
 }
Exemplo n.º 29
0
        /// <summary>
        /// Creates a new instance from a protocol-buffer stream that has a length-prefix
        /// on data (to assist with network IO).
        /// </summary>
        /// <typeparam name="T">The type to be created.</typeparam>
        /// <param name="source">The binary stream to apply to the new instance (cannot be null).</param>
        /// <param name="style">How to encode the length prefix.</param>
        /// <param name="fieldNumber">The expected tag of the item (only used with base-128 prefix style).</param>
        /// <returns>A new, initialized instance.</returns>
        public static T DeserializeWithLengthPrefix <T>(Stream source, PrefixStyle style, int fieldNumber)
        {
            RuntimeTypeModel model = RuntimeTypeModel.Default;

            return((T)model.DeserializeWithLengthPrefix(source, null, model.MapType(typeof(T)), style, fieldNumber));
        }
Exemplo n.º 30
0
 public void TypeModel_Struct_InternalAccess()
 {
     Assert.ThrowsException <InvalidFlatBufferDefinitionException>(() =>
                                                                   RuntimeTypeModel.CreateFrom(typeof(InternalAccessStruct <byte>)));
 }