internal FieldDescriptor(FieldDescriptorProto proto, FileDescriptor file,
                                 MessageDescriptor parent, int index, string propertyName)
            : base(file, file.ComputeFullName(parent, proto.Name), index)
        {
            this.proto = proto;
            if (proto.Type != 0)
            {
                fieldType = GetFieldTypeFromProtoType(proto.Type);
            }

            if (FieldNumber <= 0)
            {
                throw new DescriptorValidationException(this, "Field numbers must be positive integers.");
            }
            containingType = parent;
            // OneofIndex "defaults" to -1 due to a hack in FieldDescriptor.OnConstruction.
            if (proto.OneofIndex != -1)
            {
                if (proto.OneofIndex < 0 || proto.OneofIndex >= parent.Proto.OneofDecl.Count)
                {
                    throw new DescriptorValidationException(this,
                                                            $"FieldDescriptorProto.oneof_index is out of range for type {parent.Name}");
                }
                containingOneof = parent.Oneofs[proto.OneofIndex];
            }

            file.DescriptorPool.AddSymbol(this);
            // We can't create the accessor until we've cross-linked, unfortunately, as we
            // may not know whether the type of the field is a map or not. Remember the property name
            // for later.
            // We could trust the generated code and check whether the type of the property is
            // a MapField, but that feels a tad nasty.
            this.propertyName = propertyName;
        }
Exemple #2
0
        public void OneofDescriptor()
        {
            OneofDescriptor descriptor = TestAllTypes.Descriptor.FindDescriptor <OneofDescriptor>("oneof_field");

            Assert.IsFalse(descriptor.IsSynthetic);
            Assert.AreEqual("oneof_field", descriptor.Name);
            Assert.AreEqual("protobuf_unittest3.TestAllTypes.oneof_field", descriptor.FullName);

            var expectedFields = new[] {
                TestAllTypes.OneofBytesFieldNumber,
                TestAllTypes.OneofNestedMessageFieldNumber,
                TestAllTypes.OneofStringFieldNumber,
                TestAllTypes.OneofUint32FieldNumber
            }
            .Select(fieldNumber => TestAllTypes.Descriptor.FindFieldByNumber(fieldNumber))
            .ToList();

            foreach (var field in expectedFields)
            {
                Assert.AreSame(descriptor, field.ContainingOneof);
            }

            CollectionAssert.AreEquivalent(expectedFields, descriptor.Fields);
            TestDescriptorToProto(descriptor.ToProto, descriptor.Proto);
        }
 internal static OneofAccessor ForRegularOneof(
     OneofDescriptor descriptor,
     PropertyInfo caseProperty,
     MethodInfo clearMethod) =>
 new OneofAccessor(
     descriptor,
     ReflectionUtil.CreateFuncIMessageInt32(caseProperty.GetGetMethod()),
     ReflectionUtil.CreateActionIMessage(clearMethod));
 internal static OneofAccessor ForSyntheticOneof(OneofDescriptor descriptor)
 {
     // Note: descriptor.Fields will be null when this method is called, because we haven't
     // cross-linked yet. But by the time the delegates are called by user code, all will be
     // well. (That's why we capture the descriptor itself rather than a field.)
     return(new OneofAccessor(descriptor,
                              message => descriptor.Fields[0].Accessor.HasValue(message) ? descriptor.Fields[0].FieldNumber : 0,
                              message => descriptor.Fields[0].Accessor.Clear(message)));
 }
Exemple #5
0
 internal OneofAccessor(PropertyInfo caseProperty, MethodInfo clearMethod, OneofDescriptor descriptor)
 {
     if (!OneofAccessor.smethod_0(caseProperty))
     {
         throw OneofAccessor.smethod_1(Module.smethod_33 <string>(966859498u));
     }
     this.descriptor    = descriptor;
     this.caseDelegate  = ReflectionUtil.CreateFuncIMessageT <int>(caseProperty.GetGetMethod());
     this.descriptor    = descriptor;
     this.clearDelegate = ReflectionUtil.CreateActionIMessage(clearMethod);
 }
        public OneofAccessor(PropertyInfo caseProperty, MethodInfo clearMethod, OneofDescriptor descriptor)
        {
            if (!caseProperty.CanRead)
            {
                throw new ArgumentException("Cannot read from property");
            }
            this.descriptor = descriptor;
            caseDelegate    = ReflectionUtil.CreateFuncIMessageT <int>(caseProperty.GetGetMethod());

            this.descriptor = descriptor;
            clearDelegate   = ReflectionUtil.CreateActionIMessage(clearMethod);
        }
Exemple #7
0
        internal OneofAccessor(PropertyInfo caseProperty, MethodInfo clearMethod, OneofDescriptor descriptor) 
        {
            if (!caseProperty.CanRead)
            {
                throw new ArgumentException("Cannot read from property");
            }
            this.descriptor = descriptor;
            caseDelegate = ReflectionUtil.CreateFuncObjectT<int>(caseProperty.GetGetMethod());

            this.descriptor = descriptor;
            clearDelegate = ReflectionUtil.CreateActionObject(clearMethod);
        }
 private OneofAccessor(OneofDescriptor descriptor, Func <IMessage, int> caseDelegate, Action <IMessage> clearDelegate)
 {
     Descriptor         = descriptor;
     this.caseDelegate  = caseDelegate;
     this.clearDelegate = clearDelegate;
 }
        internal void CrossLink()
        {
            List <FieldDescriptor>        list       = new List <FieldDescriptor>();
            IEnumerator <FieldDescriptor> enumerator = this.ContainingType.Fields.InDeclarationOrder().GetEnumerator();

            try
            {
                while (true)
                {
IL_A4:
                    uint arg_78_0 = OneofDescriptor.smethod_0(enumerator) ? 645945303u : 1779362706u;
                    while (true)
                    {
                        uint num;
                        switch ((num = (arg_78_0 ^ 1929289758u)) % 5u)
                        {
                        case 0u:
                        {
                            FieldDescriptor current;
                            list.Add(current);
                            arg_78_0 = (num * 2160487366u ^ 3013554972u);
                            continue;
                        }

                        case 1u:
                            goto IL_A4;

                        case 2u:
                            arg_78_0 = 645945303u;
                            continue;

                        case 3u:
                        {
                            FieldDescriptor current = enumerator.Current;
                            arg_78_0 = ((current.ContainingOneof != this) ? 607704728u : 324701640u);
                            continue;
                        }
                        }
                        goto Block_4;
                    }
                }
                Block_4 :;
            }
            finally
            {
                if (enumerator != null)
                {
                    while (true)
                    {
                        IL_E8 :
                        uint arg_D0_0 = 327623526u;
                        while (true)
                        {
                            uint num;
                            switch ((num = (arg_D0_0 ^ 1929289758u)) % 3u)
                            {
                            case 1u:
                                OneofDescriptor.smethod_1(enumerator);
                                arg_D0_0 = (num * 1913406752u ^ 3932505543u);
                                continue;

                            case 2u:
                                goto IL_E8;
                            }
                            goto Block_8;
                        }
                    }
                    Block_8 :;
                }
            }
            this.fields = new ReadOnlyCollection <FieldDescriptor>(list);
        }
Exemple #10
0
        internal FieldDescriptor(FieldDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, string propertyName) : base(file, file.ComputeFullName(parent, proto.Name), index)
        {
            while (true)
            {
IL_192:
                uint arg_155_0 = 668326049u;
                while (true)
                {
                    uint num;
                    switch ((num = (arg_155_0 ^ 1907900556u)) % 12u)
                    {
                    case 0u:
                        this.containingOneof = parent.Oneofs[proto.OneofIndex];
                        arg_155_0            = 951252349u;
                        continue;

                    case 2u:
                        goto IL_192;

                    case 3u:
                        arg_155_0 = (((proto.OneofIndex != -1) ? 3178787542u : 4269045895u) ^ num * 287148678u);
                        continue;

                    case 4u:
                        arg_155_0 = (((proto.OneofIndex < 0) ? 830153714u : 1120517676u) ^ num * 2667568972u);
                        continue;

                    case 5u:
                        this.proto = proto;
                        arg_155_0  = (((proto.Type == (FieldDescriptorProto.Types.Type) 0) ? 1249293554u : 878441721u) ^ num * 64997452u);
                        continue;

                    case 6u:
                        arg_155_0 = ((this.FieldNumber > 0) ? 416220771u : 1243880467u);
                        continue;

                    case 7u:
                        goto IL_199;

                    case 8u:
                        arg_155_0 = (((proto.OneofIndex < parent.Proto.OneofDecl.Count) ? 3890232560u : 2326943442u) ^ num * 1872585197u);
                        continue;

                    case 9u:
                        this.fieldType = FieldDescriptor.GetFieldTypeFromProtoType(proto.Type);
                        arg_155_0      = (num * 2182631442u ^ 3138733388u);
                        continue;

                    case 10u:
                        goto IL_1AA;

                    case 11u:
                        this.containingType = parent;
                        arg_155_0           = 1386389043u;
                        continue;
                    }
                    goto Block_6;
                }
            }
Block_6:
            goto IL_1CF;
IL_199:
            throw new DescriptorValidationException(this, Module.smethod_33 <string>(1349002182u));
IL_1AA:
            throw new DescriptorValidationException(this, FieldDescriptor.smethod_0(Module.smethod_37 <string>(3181016348u), new object[]
            {
                parent.Name
            }));
IL_1CF:
            file.DescriptorPool.AddSymbol(this);
            this.propertyName = propertyName;
        }