Пример #1
0
        // For the field identified by its name.
        protected override FieldDefinition DeclarePrivateFieldCore(string name, TypeDefinition type)
        {
            var field = this._buildingType.Members.OfType <CodeMemberField>().SingleOrDefault(f => f.Name == name);

            if (field == null)
            {
                this._buildingType.Members.Add(
                    new CodeMemberField(CodeDomSerializerBuilder.ToCodeTypeReference(type), name)
                {
                    Attributes = MemberAttributes.Private
                }
                    );
            }

            return(new FieldDefinition(null, name, type));
        }
Пример #2
0
        protected override void DefineUnpackingContextCore(
            IList <KeyValuePair <string, TypeDefinition> > fields,
            out TypeDefinition type,
            out ConstructorDefinition constructor,
            out CodeDomConstruct parameterInUnpackValueMethods,
            out CodeDomConstruct parameterInSetValueMethods,
            out CodeDomConstruct parameterInCreateObjectFromContext
            )
        {
            var codeType = new CodeTypeDeclaration(SerializerBuilderHelper.UnpackingContextTypeName);
            var ctor     =
                new CodeConstructor
            {
                Attributes = MemberAttributes.Public
            };

            foreach (var kv in fields)
            {
                var field =
                    new CodeMemberField(CodeDomSerializerBuilder.ToCodeTypeReference(kv.Value), kv.Key)
                {
                    Attributes = MemberAttributes.Public
                };
                codeType.Members.Add(field);

                var param = new CodeParameterDeclarationExpression(CodeDomSerializerBuilder.ToCodeTypeReference(kv.Value), kv.Key);

                ctor.Parameters.Add(param);
                ctor.Statements.Add(
                    new CodeAssignStatement(
                        new CodeFieldReferenceExpression(
                            new CodeThisReferenceExpression(),
                            kv.Key
                            ),
                        new CodeArgumentReferenceExpression(kv.Key)
                        )
                    );
            }

            codeType.Members.Add(ctor);
            this._buildingType.Members.Add(codeType);
            type        = TypeDefinition.Object(codeType.Name);
            constructor = new ConstructorDefinition(type, fields.Select(kv => kv.Value).ToArray());
            DefineUnpackValueMethodArguments(type, out parameterInUnpackValueMethods, out parameterInSetValueMethods, out parameterInCreateObjectFromContext);
        }
Пример #3
0
        protected override MethodDefinition EndPrivateMethodCore(string name, CodeDomConstruct body)
        {
            var context = this._methodContextStack.Pop();

#if DEBUG
            Contract.Assert(context.Name == name, "context.Name == name");
#endif // DEBUG
            if (body == null)
            {
                return(null);
            }

            // ReSharper disable once BitwiseOperatorOnEnumWithoutFlags
            var codeMethod = new CodeMemberMethod {
                Name = context.Name, Attributes = MemberAttributes.Private | (context.IsStatic ? MemberAttributes.Static : 0)
            };
            // ReSharper disable once ImpureMethodCallOnReadonlyValueField
            if (context.ReturnType.TryGetRuntimeType() != typeof(void))
            {
                codeMethod.ReturnType = CodeDomSerializerBuilder.ToCodeTypeReference(context.ReturnType);
            }

            codeMethod.Parameters.AddRange(
                context.Parameters.Select(kv =>
                                          new CodeParameterDeclarationExpression(CodeDomSerializerBuilder.ToCodeTypeReference(kv.Value), kv.Key)
                                          ).ToArray()
                );

            codeMethod.Statements.AddRange(body.AsStatements().ToArray());

            this.DeclaringType.Members.Add(codeMethod);
            return
                (new MethodDefinition(
                     context.Name,
                     null,
                     null,
                     context.IsStatic,
                     context.ReturnType,
                     context.Parameters.Select(kv => kv.Value).ToArray()
                     ));
        }
Пример #4
0
 public VariableCodeDomConstruct(TypeDefinition type, string name)
     : base(type)
 {
     this._type = CodeDomSerializerBuilder.ToCodeTypeReference(type);
     this._name = name;
 }
Пример #5
0
        protected override MethodDefinition EndMethodOverrideCore(string name, CodeDomConstruct body)
        {
            var context = this._methodContextStack.Pop();

#if DEBUG
            Contract.Assert(context.Name == name, "context.Name == name");
#endif // DEBUG
            if (body == null)
            {
                return(null);
            }

            CodeMemberMethod codeMethod = new CodeMemberMethod {
                Name = name
            };
            switch (name)
            {
            case MethodName.PackToCore:
            {
                codeMethod.Parameters.Add(this.Packer.AsParameter());
                codeMethod.Parameters.Add(this.PackToTarget.AsParameter());
                // ReSharper disable BitwiseOperatorOnEnumWithoutFlags
                codeMethod.Attributes = (this.IsInternalToMsgPackLibrary ? MemberAttributes.FamilyOrAssembly : MemberAttributes.Family) | MemberAttributes.Override;
                // ReSharper restore BitwiseOperatorOnEnumWithoutFlags

                break;
            }

            case MethodName.UnpackFromCore:
            {
                codeMethod.ReturnType = new CodeTypeReference(this._targetType);
                codeMethod.Parameters.Add(this.Unpacker.AsParameter());
                // ReSharper disable BitwiseOperatorOnEnumWithoutFlags
                codeMethod.Attributes = (this.IsInternalToMsgPackLibrary ? MemberAttributes.FamilyOrAssembly : MemberAttributes.Family) | MemberAttributes.Override;
                // ReSharper restore BitwiseOperatorOnEnumWithoutFlags

                break;
            }

            case MethodName.UnpackToCore:
            {
                codeMethod.Parameters.Add(this.Unpacker.AsParameter());
                codeMethod.Parameters.Add(this.UnpackToTarget.AsParameter());
                // ReSharper disable BitwiseOperatorOnEnumWithoutFlags
                codeMethod.Attributes = (this.IsInternalToMsgPackLibrary? MemberAttributes.FamilyOrAssembly : MemberAttributes.Family) | MemberAttributes.Override;
                // ReSharper restore BitwiseOperatorOnEnumWithoutFlags

                break;
            }

            case MethodName.PackUnderlyingValueTo:
            {
                codeMethod.Parameters.Add(this.Packer.AsParameter());
                codeMethod.Parameters.Add(new CodeParameterDeclarationExpression(this._targetType, "enumValue"));
                // ReSharper disable BitwiseOperatorOnEnumWithoutFlags
                codeMethod.Attributes = (this.IsInternalToMsgPackLibrary ? MemberAttributes.FamilyOrAssembly : MemberAttributes.Family) | MemberAttributes.Override;
                // ReSharper restore BitwiseOperatorOnEnumWithoutFlags

                break;
            }

            case MethodName.UnpackFromUnderlyingValue:
            {
                codeMethod.ReturnType = new CodeTypeReference(this._targetType);
                codeMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(MessagePackObject), "messagePackObject"));
                // ReSharper disable BitwiseOperatorOnEnumWithoutFlags
                codeMethod.Attributes = (this.IsInternalToMsgPackLibrary ? MemberAttributes.FamilyOrAssembly : MemberAttributes.Family) | MemberAttributes.Override;
                // ReSharper restore BitwiseOperatorOnEnumWithoutFlags

                break;
            }

            case MethodName.AddItem:
            {
                codeMethod.Parameters.Add(new CodeParameterDeclarationExpression(this._targetType, "collection"));
                if (this.IsDictionary)
                {
                    codeMethod.Parameters.Add(
                        new CodeParameterDeclarationExpression(
                            CodeDomSerializerBuilder.ToCodeTypeReference(this.KeyToAdd.ContextType),
                            "key"
                            )
                        );
                    codeMethod.Parameters.Add(
                        new CodeParameterDeclarationExpression(
                            CodeDomSerializerBuilder.ToCodeTypeReference(this.ValueToAdd.ContextType),
                            "value"
                            )
                        );
                }
                else
                {
                    codeMethod.Parameters.Add(
                        new CodeParameterDeclarationExpression(
                            CodeDomSerializerBuilder.ToCodeTypeReference(this.ItemToAdd.ContextType),
                            "item"
                            )
                        );
                }
                // ReSharper disable BitwiseOperatorOnEnumWithoutFlags
                codeMethod.Attributes = MemberAttributes.Family | MemberAttributes.Override;
                // ReSharper restore BitwiseOperatorOnEnumWithoutFlags
                break;
            }

            case MethodName.CreateInstance:
            {
                codeMethod.ReturnType = new CodeTypeReference(this._targetType);
                codeMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "initialCapacity"));

                // ReSharper disable BitwiseOperatorOnEnumWithoutFlags
                codeMethod.Attributes = MemberAttributes.Family | MemberAttributes.Override;
                // ReSharper restore BitwiseOperatorOnEnumWithoutFlags
                break;
            }

#if FEATURE_TAP
            case MethodName.PackToAsyncCore:
            {
                codeMethod.ReturnType = new CodeTypeReference(typeof(Task));
                codeMethod.Parameters.Add(this.Packer.AsParameter());
                codeMethod.Parameters.Add(this.PackToTarget.AsParameter());
                codeMethod.Parameters.Add(CreateCancellationTokenParameter());
                // ReSharper disable BitwiseOperatorOnEnumWithoutFlags
                codeMethod.Attributes = (this.IsInternalToMsgPackLibrary ? MemberAttributes.FamilyOrAssembly : MemberAttributes.Family) | MemberAttributes.Override;
                // ReSharper restore BitwiseOperatorOnEnumWithoutFlags

                break;
            }

            case MethodName.UnpackFromAsyncCore:
            {
                codeMethod.ReturnType = new CodeTypeReference(typeof(Task <>).MakeGenericType(this._targetType));
                codeMethod.Parameters.Add(this.Unpacker.AsParameter());
                codeMethod.Parameters.Add(CreateCancellationTokenParameter());
                // ReSharper disable BitwiseOperatorOnEnumWithoutFlags
                codeMethod.Attributes = (this.IsInternalToMsgPackLibrary ? MemberAttributes.FamilyOrAssembly : MemberAttributes.Family) | MemberAttributes.Override;
                // ReSharper restore BitwiseOperatorOnEnumWithoutFlags

                break;
            }

            case MethodName.UnpackToAsyncCore:
            {
                codeMethod.ReturnType = new CodeTypeReference(typeof(Task));
                codeMethod.Parameters.Add(this.Unpacker.AsParameter());
                codeMethod.Parameters.Add(this.UnpackToTarget.AsParameter());
                codeMethod.Parameters.Add(CreateCancellationTokenParameter());
                // ReSharper disable BitwiseOperatorOnEnumWithoutFlags
                codeMethod.Attributes = (this.IsInternalToMsgPackLibrary ? MemberAttributes.FamilyOrAssembly : MemberAttributes.Family) | MemberAttributes.Override;
                // ReSharper restore BitwiseOperatorOnEnumWithoutFlags

                break;
            }

            case MethodName.PackUnderlyingValueToAsync:
            {
                codeMethod.ReturnType = new CodeTypeReference(typeof(Task));
                codeMethod.Parameters.Add(this.Packer.AsParameter());
                codeMethod.Parameters.Add(new CodeParameterDeclarationExpression(this._targetType, "enumValue"));
                codeMethod.Parameters.Add(CreateCancellationTokenParameter());
                // ReSharper disable BitwiseOperatorOnEnumWithoutFlags
                codeMethod.Attributes = (this.IsInternalToMsgPackLibrary ? MemberAttributes.FamilyOrAssembly : MemberAttributes.Family) | MemberAttributes.Override;
                // ReSharper restore BitwiseOperatorOnEnumWithoutFlags

                break;
            }
#endif // FEATURE_TAP
            default:
            {
                throw new ArgumentOutOfRangeException("name", name);
            }
            }

            codeMethod.Statements.AddRange(body.AsStatements().ToArray());

            this.DeclaringType.Members.Add(codeMethod);
            return
                (new MethodDefinition(
                     context.Name,
                     null,
                     null,
                     context.IsStatic,
                     context.ReturnType,
                     context.Parameters.Select(kv => kv.Value).ToArray()
                     ));
        }