Exemplo n.º 1
0
 public static MemberDeclarationSyntax IConvertibleToTypeSyntax(this PrimitiveDescriptor descriptor)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.ObjectKeyword)), Identifier("ToType"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
 .WithParameterList(
     ParameterList(
         SeparatedList <ParameterSyntax>(
             new SyntaxNodeOrToken[]
 {
     Parameter(Identifier("conversionType"))
     .WithType(IdentifierName("Type")),
     Token(SyntaxKind.CommaToken), Parameter(Identifier("provider"))
     .WithType(descriptor.TryWrapInNullableTypeSyntax(IdentifierName("IFormatProvider")))
 })))
 .WithExpressionBody(
     ArrowExpressionClause(
         InvocationExpression(
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 IdentifierName("Convert"),
                 IdentifierName("ChangeType")))
         .WithArgumentList(
             ArgumentList(
                 SeparatedList <ArgumentSyntax>(
                     new SyntaxNodeOrToken[]
 {
     Argument(
         MemberAccessExpression(
             SyntaxKind.SimpleMemberAccessExpression,
             ThisExpression(),
             IdentifierName(descriptor.InnerName))),
     Token(SyntaxKind.CommaToken), Argument(IdentifierName("conversionType")), Token(SyntaxKind.CommaToken),
     Argument(IdentifierName("provider"))
 })))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemplo n.º 2
0
        public object Create(Type type)
        {
            type = GetDefaultImplementation(type);

            // We can't instantiate primitive or arrays
            if (PrimitiveDescriptor.IsPrimitive(type) || type.IsArray)
            {
                return(null);
            }

            if (type.GetConstructor(EmptyTypes) != null || type.IsValueType)
            {
                try
                {
                    return(Activator.CreateInstance(type));
                }
                catch (Exception e)
                {
                    //return System.Runtime.Serialization.FormatterServices.GetUninitializedObject(type);
                    throw new InstanceCreationException($"'{typeof(Activator)}' failed to create instance of type '{type}', see inner exception.", e);
                }
            }

            return(null);
        }
Exemplo n.º 3
0
 static MemberDeclarationSyntax IConvertibleConvertToSyntax(this PrimitiveDescriptor descriptor, TypeSyntax returnType, string name)
 => MethodDeclaration(returnType, Identifier(name))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
 .WithParameterList(
     ParameterList(
         SingletonSeparatedList(
             Parameter(Identifier("provider"))
             .WithType(descriptor.TryWrapInNullableTypeSyntax(IdentifierName("IFormatProvider"))))))
 .WithExpressionBody(
     ArrowExpressionClause(
         InvocationExpression(
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 IdentifierName("Convert"),
                 IdentifierName(name)))
         .WithArgumentList(
             ArgumentList(
                 SeparatedList <ArgumentSyntax>(
                     new SyntaxNodeOrToken[]
 {
     Argument(
         MemberAccessExpression(
             SyntaxKind.SimpleMemberAccessExpression,
             ThisExpression(),
             IdentifierName(descriptor.InnerName))),
     Token(SyntaxKind.CommaToken), Argument(IdentifierName("provider"))
 })))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemplo n.º 4
0
 public static MemberDeclarationSyntax StringIComparableTCompareToSyntax(this PrimitiveDescriptor descriptor, StringComparison stringComparison)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword)), Identifier("CompareTo"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
 .WithParameterList(ParameterList(SingletonSeparatedList(Parameter(Identifier("other")).WithType(IdentifierName(descriptor.Name)))))
 .WithExpressionBody(
     ArrowExpressionClause(
         InvocationExpression(
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 PredefinedType(Token(SyntaxKind.StringKeyword)),
                 IdentifierName("Compare")))
         .WithArgumentList(
             ArgumentList(
                 SeparatedList <ArgumentSyntax>(
                     new SyntaxNodeOrToken[]
 {
     Argument(
         MemberAccessExpression(
             SyntaxKind.SimpleMemberAccessExpression,
             ThisExpression(),
             IdentifierName(descriptor.InnerName))),
     Token(SyntaxKind.CommaToken), Argument(
         MemberAccessExpression(
             SyntaxKind.SimpleMemberAccessExpression,
             IdentifierName("other"),
             IdentifierName(descriptor.InnerName))),
     Token(SyntaxKind.CommaToken), Argument(
         MemberAccessExpression(
             SyntaxKind.SimpleMemberAccessExpression,
             IdentifierName("StringComparison"),
             IdentifierName(stringComparison.ToString())))
 })))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemplo n.º 5
0
    static IEnumerable <BaseTypeSyntax> GetBaseTypes(PrimitiveDescriptor descriptor)
    {
        if (descriptor.Features.HasFlag(Features.Equatable))
        {
            yield return(SimpleBaseType(
                             GenericName(Identifier("IEquatable"))
                             .WithTypeArgumentList(
                                 TypeArgumentList(
                                     SingletonSeparatedList <TypeSyntax>(
                                         IdentifierName(descriptor.Name))))));
        }

        if (descriptor.Features.HasFlag(Features.Comparable))
        {
            yield return(SimpleBaseType(
                             GenericName(Identifier("IComparable"))
                             .WithTypeArgumentList(
                                 TypeArgumentList(
                                     SingletonSeparatedList <TypeSyntax>(
                                         IdentifierName(descriptor.Name))))));

            yield return(SimpleBaseType(IdentifierName("IComparable")));
        }

        if (descriptor.Features.HasFlag(Features.Formattable))
        {
            yield return(SimpleBaseType(IdentifierName("IFormattable")));
        }

        if (descriptor.Features.HasFlag(Features.Convertible))
        {
            yield return(SimpleBaseType(IdentifierName("IConvertible")));
        }
    }
        public virtual DataStyle GetStyle(ref ObjectContext objectContext)
        {
            var context = objectContext.SerializerContext;

            // Resolve the style, use default style if not defined.
            // First pop style of current member being serialized.
            var style = objectContext.Style;

            // If no style yet defined
            if (style != DataStyle.Any)
            {
                return(style);
            }

            // Try to get the style from this serializer
            style = objectContext.Descriptor.Style;

            // In case of any style, allow to emit a flow sequence depending on Settings LimitPrimitiveFlowSequence.
            // Apply this only for primitives
            if (style == DataStyle.Any)
            {
                bool isPrimitiveElementType = false;
                var  collectionDescriptor   = objectContext.Descriptor as CollectionDescriptor;
                int  count = 0;
                if (collectionDescriptor != null)
                {
                    isPrimitiveElementType = PrimitiveDescriptor.IsPrimitive(collectionDescriptor.ElementType);
                    count = collectionDescriptor.GetCollectionCount(objectContext.Instance);
                }
                else
                {
                    var arrayDescriptor = objectContext.Descriptor as ArrayDescriptor;
                    if (arrayDescriptor != null)
                    {
                        isPrimitiveElementType = PrimitiveDescriptor.IsPrimitive(arrayDescriptor.ElementType);
                        count = ((Array)objectContext.Instance)?.Length ?? -1;
                    }
                }

                style = objectContext.Instance == null || count >= objectContext.SerializerContext.Settings.LimitPrimitiveFlowSequence || !isPrimitiveElementType
                    ? DataStyle.Normal
                    : DataStyle.Compact;
            }

            // If not defined, get the default style
            if (style == DataStyle.Any)
            {
                style = context.Settings.DefaultStyle;

                // If default style is set to Any, set it to Block by default.
                if (style == DataStyle.Any)
                {
                    style = DataStyle.Normal;
                }
            }

            return(style);
        }
Exemplo n.º 7
0
 public static MemberDeclarationSyntax ImplicitCastFromPrimitiveSyntax(this PrimitiveDescriptor descriptor)
 => ConversionOperatorDeclaration(Token(SyntaxKind.ImplicitKeyword), IdentifierName(descriptor.Name))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
 .WithParameterList(ParameterList(SingletonSeparatedList(Parameter(descriptor.InnerName).WithType(descriptor.InnerType))))
 .WithExpressionBody(
     ArrowExpressionClause(
         InvocationExpression(IdentifierName("From" + descriptor.InnerTypeName))
         .WithArgumentList(ArgumentList(SingletonSeparatedList(Argument(IdentifierName(descriptor.InnerName)))))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemplo n.º 8
0
        /// <summary>
        /// Visits the specified <see cref="PrimitiveDescriptor" /> descriptor.
        /// </summary>
        /// <param name="descriptor">The descriptor.</param>
        /// <returns></returns>
        public object Visit(PrimitiveDescriptor descriptor)
        {
            XElement primitiveElement = new XElement(descriptor.Description,
                                                     new XAttribute("name", descriptor.SourceName),
                                                     new XAttribute("type", descriptor.SourceType),
                                                     descriptor.Value);

            return(primitiveElement);
        }
Exemplo n.º 9
0
            /// <summary>
            /// Visits the specified <see cref="PrimitiveDescriptor" /> descriptor.
            /// </summary>
            /// <param name="descriptor">The descriptor.</param>
            /// <returns></returns>
            public object Visit(PrimitiveDescriptor descriptor)
            {
                Type creationType = Type.GetType(descriptor.SourceType);

                ValidateType(creationType);

                object value = Convert.ChangeType(descriptor.Value, creationType);

                return(value);
            }
Exemplo n.º 10
0
 static MemberDeclarationSyntax NumberTryParseSyntax(this PrimitiveDescriptor descriptor, TypeSyntax inputType)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.BoolKeyword)), Identifier("TryParse"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
 .WithParameterList(
     ParameterList(
         SeparatedList <ParameterSyntax>(
             new SyntaxNodeOrToken[]
 {
     Parameter(Identifier("s"))
     .WithType(inputType),
     Token(SyntaxKind.CommaToken), Parameter(Identifier("numberStyles"))
     .WithType(IdentifierName("NumberStyles")),
     Token(SyntaxKind.CommaToken), Parameter(Identifier("formatProvider"))
     .WithType(IdentifierName("IFormatProvider")),
     Token(SyntaxKind.CommaToken), Parameter(Identifier("value"))
     .WithModifiers(TokenList(Token(SyntaxKind.OutKeyword)))
     .WithType(IdentifierName(descriptor.Name))
 })))
 .WithBody(
     Block(
         IfStatement(
             PrefixUnaryExpression(
                 SyntaxKind.LogicalNotExpression,
                 InvocationExpression(
                     MemberAccessExpression(
                         SyntaxKind.SimpleMemberAccessExpression,
                         PredefinedType(Token(descriptor.InnerKnownType.ToTypeKeyword())),
                         IdentifierName("TryParse")))
                 .WithArgumentList(
                     ArgumentList(
                         SeparatedList <ArgumentSyntax>(
                             new SyntaxNodeOrToken[]
 {
     Argument(IdentifierName("s")), Token(SyntaxKind.CommaToken), Argument(IdentifierName("numberStyles")),
     Token(SyntaxKind.CommaToken), Argument(IdentifierName("formatProvider")), Token(SyntaxKind.CommaToken),
     Argument(
         DeclarationExpression(
             PredefinedType(Token(descriptor.InnerKnownType.ToTypeKeyword())),
             SingleVariableDesignation(Identifier("result"))))
     .WithRefKindKeyword(Token(SyntaxKind.OutKeyword))
 })))),
             Block(
                 ExpressionStatement(
                     AssignmentExpression(
                         SyntaxKind.SimpleAssignmentExpression,
                         IdentifierName("value"),
                         LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)))),
                 ReturnStatement(LiteralExpression(SyntaxKind.FalseLiteralExpression)))),
         ExpressionStatement(
             AssignmentExpression(
                 SyntaxKind.SimpleAssignmentExpression,
                 IdentifierName("value"),
                 ObjectCreationExpression(IdentifierName(descriptor.Name))
                 .WithArgumentList(ArgumentList(SingletonSeparatedList(Argument(IdentifierName("result"))))))),
         ReturnStatement(LiteralExpression(SyntaxKind.TrueLiteralExpression))));
Exemplo n.º 11
0
 public static MemberDeclarationSyntax ToStringSyntax(this PrimitiveDescriptor descriptor)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.StringKeyword)), Identifier("ToString"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword)))
 .WithExpressionBody(
     ArrowExpressionClause(
         InvocationExpression(
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 IdentifierName(descriptor.InnerName),
                 IdentifierName("ToString")))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemplo n.º 12
0
 public static MemberDeclarationSyntax StringExplicitCastToPrimitiveSyntax(this PrimitiveDescriptor descriptor)
 => ConversionOperatorDeclaration(Token(SyntaxKind.ExplicitKeyword), descriptor.InnerType)
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
 .WithParameterList(ParameterList(SingletonSeparatedList(Parameter(Identifier("value")).WithType(IdentifierName(descriptor.Name)))))
 .WithExpressionBody(
     ArrowExpressionClause(
         MemberAccessExpression(
             SyntaxKind.SimpleMemberAccessExpression,
             IdentifierName("value"),
             IdentifierName(descriptor.InnerName))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemplo n.º 13
0
 /// <inheritdoc/>
 public override void VisitPrimitive(object primitive, PrimitiveDescriptor descriptor)
 {
     if (CurrentPath.Match(MemberPath))
     {
         VisitAssetMember(primitive, descriptor);
     }
     else
     {
         base.VisitPrimitive(primitive, descriptor);
     }
 }
Exemplo n.º 14
0
        public object Create(Type type)
        {
            type = GetDefaultImplementation(type);

            // We can't instantiate primitive or arrays
            if (PrimitiveDescriptor.IsPrimitive(type) || type.IsArray)
            {
                return(null);
            }

            return(type.GetConstructor(EmptyTypes) != null || type.GetTypeInfo().IsValueType ? Activator.CreateInstance(type) : null);
        }
Exemplo n.º 15
0
        public void TestPrimitiveDescriptor()
        {
            var attributeRegistry = new AttributeRegistry();
            var descriptor        = new PrimitiveDescriptor(attributeRegistry, typeof(int), new DefaultNamingConvention());

            Assert.AreEqual(0, descriptor.Count);

            Assert.True(PrimitiveDescriptor.IsPrimitive(typeof(MyEnum)));
            Assert.True(PrimitiveDescriptor.IsPrimitive(typeof(object)));
            Assert.True(PrimitiveDescriptor.IsPrimitive(typeof(DateTime)));
            Assert.True(PrimitiveDescriptor.IsPrimitive(typeof(TimeSpan)));
            Assert.False(PrimitiveDescriptor.IsPrimitive(typeof(IList)));
        }
Exemplo n.º 16
0
 public static MemberDeclarationSyntax ConstructorSyntax(this PrimitiveDescriptor descriptor)
 => ConstructorDeclaration(descriptor.Name)
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
 .WithParameterList(ParameterList(SingletonSeparatedList(Parameter(descriptor.InnerName).WithType(descriptor.InnerType))))
 .WithExpressionBody(
     ArrowExpressionClause(
         AssignmentExpression(
             SyntaxKind.SimpleAssignmentExpression,
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 ThisExpression(),
                 IdentifierName(descriptor.InnerName)),
             IdentifierName(descriptor.InnerName))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemplo n.º 17
0
        public void PreparePrimitive()
        {
            Descriptor = PrimitiveRegistry.GetDescriptor(Instruction.Opcode);
            Operand    = (VMPrimitiveOperand)Activator.CreateInstance(Descriptor.OperandType);
            Operand.Read(Instruction.Operand);

            Nodes         = new PrimitiveNode[2];
            Nodes[0]      = new PrimitiveNode();
            Nodes[0].Type = NodeType.False;
            Nodes[1]      = new PrimitiveNode();
            Nodes[1].Type = NodeType.True;

            Title           = new UILabel();
            Title.Alignment = TextAlignment.Middle | TextAlignment.Center;
            Title.Y         = 0;
            Title.X         = 0;
            this.Add(Title);
            Title.CaptionStyle       = TextStyle.DefaultLabel.Clone();
            Title.CaptionStyle.Font  = FSO.Client.GameFacade.EdithFont;
            Title.CaptionStyle.VFont = FSO.Client.GameFacade.EdithVectorFont;
            Title.CaptionStyle.Size  = 14;

            Index           = new UILabel();
            Index.Alignment = TextAlignment.Right | TextAlignment.Center;
            Index.Y         = 0;
            Index.X         = 0;
            this.Add(Index);
            Index.CaptionStyle       = TextStyle.DefaultLabel.Clone();
            Index.CaptionStyle.Font  = FSO.Client.GameFacade.EdithFont;
            Index.CaptionStyle.VFont = FSO.Client.GameFacade.EdithVectorFont;
            Index.CaptionStyle.Size  = 10;
            Index.Visible            = false;

            BodyTextStyle       = TextStyle.DefaultLabel.Clone();
            BodyTextStyle.Font  = FSO.Client.GameFacade.EdithFont;
            BodyTextStyle.VFont = FSO.Client.GameFacade.EdithVectorFont;
            BodyTextStyle.Size  = 12;

            CommentNode = new CommentContainer(TreeBox.Comment);
            CommentNode.OnCommentChanged += CommentNode_OnCommentChanged;
            CommentNode.Y = -3;
            Add(CommentNode);

            this.Add(Nodes[0]);
            this.Add(Nodes[1]);

            UpdateDisplay();
        }
Exemplo n.º 18
0
 public static MemberDeclarationSyntax IEquatableTEqualsSyntax(this PrimitiveDescriptor descriptor)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.BoolKeyword)), Identifier("Equals"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
 .WithParameterList(ParameterList(SingletonSeparatedList(Parameter(Identifier("other")).WithType(IdentifierName(descriptor.Name)))))
 .WithExpressionBody(
     ArrowExpressionClause(
         BinaryExpression(
             SyntaxKind.EqualsExpression,
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 ThisExpression(),
                 IdentifierName(descriptor.InnerName)),
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 IdentifierName("other"),
                 IdentifierName(descriptor.InnerName)))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemplo n.º 19
0
 public static MemberDeclarationSyntax IConvertibleGetTypeCodeSyntax(this PrimitiveDescriptor descriptor)
 => MethodDeclaration(IdentifierName("TypeCode"), Identifier("GetTypeCode"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
 .WithExpressionBody(
     ArrowExpressionClause(
         InvocationExpression(
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 IdentifierName("Convert"),
                 IdentifierName("GetTypeCode")))
         .WithArgumentList(
             ArgumentList(
                 SingletonSeparatedList(
                     Argument(
                         MemberAccessExpression(
                             SyntaxKind.SimpleMemberAccessExpression,
                             ThisExpression(),
                             IdentifierName(descriptor.InnerName))))))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemplo n.º 20
0
        public PrimitiveBox(BHAVInstruction inst, byte ptr, BHAVContainer master)
        {
            Type        = PrimBoxType.Primitive;
            Instruction = inst;
            Descriptor  = PrimitiveRegistry.GetDescriptor(inst.Opcode);
            Operand     = (VMPrimitiveOperand)Activator.CreateInstance(Descriptor.OperandType);
            Operand.Read(inst.Operand);
            InstPtr = ptr;

            Nodes    = new PrimitiveNode[2];
            Nodes[0] = new PrimitiveNode
            {
                Type = NodeType.False
            };
            Nodes[1] = new PrimitiveNode
            {
                Type = NodeType.True
            };

            Title = new UILabel
            {
                Alignment = TextAlignment.Middle | TextAlignment.Center,
                Y         = 0,
                X         = 0
            };
            this.Add(Title);
            Title.CaptionStyle      = TextStyle.DefaultLabel.Clone();
            Title.CaptionStyle.Font = Client.GameFacade.EdithFont;
            Title.CaptionStyle.Size = 14;

            BodyTextStyle      = TextStyle.DefaultLabel.Clone();
            BodyTextStyle.Font = Client.GameFacade.EdithFont;
            BodyTextStyle.Size = 12;

            this.Add(Nodes[0]);
            this.Add(Nodes[1]);

            HitTest = ListenForMouse(new Rectangle(0, 0, Width, Height), new UIMouseEvent(MouseEvents));

            Master = master;
            UpdateDisplay();
        }
Exemplo n.º 21
0
 public static MemberDeclarationSyntax OperatorEqualsSyntax(this PrimitiveDescriptor descriptor)
 => OperatorDeclaration(PredefinedType(Token(SyntaxKind.BoolKeyword)), Token(SyntaxKind.EqualsEqualsToken))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
 .WithParameterList(
     ParameterList(
         SeparatedList <ParameterSyntax>(
             new SyntaxNodeOrToken[]
 {
     Parameter(Identifier("value1")).WithType(IdentifierName(descriptor.Name)), Token(SyntaxKind.CommaToken),
     Parameter(Identifier("value2")).WithType(IdentifierName(descriptor.Name))
 })))
 .WithExpressionBody(
     ArrowExpressionClause(
         InvocationExpression(
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 IdentifierName("value1"),
                 IdentifierName("Equals")))
         .WithArgumentList(ArgumentList(SingletonSeparatedList(Argument(IdentifierName("value2")))))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemplo n.º 22
0
 public static MemberDeclarationSyntax EqualsSyntax(this PrimitiveDescriptor descriptor)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.BoolKeyword)), Identifier("Equals"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword)))
 .WithParameterList(
     ParameterList(
         SingletonSeparatedList(
             Parameter(Identifier("obj"))
             .WithType(descriptor.TryWrapInNullableTypeSyntax(PredefinedType(Token(SyntaxKind.ObjectKeyword)))))))
 .WithExpressionBody(
     ArrowExpressionClause(
         BinaryExpression(
             SyntaxKind.LogicalAndExpression,
             IsPatternExpression(
                 IdentifierName("obj"),
                 DeclarationPattern(
                     IdentifierName(descriptor.Name),
                     SingleVariableDesignation(Identifier("value")))),
             InvocationExpression(IdentifierName("Equals"))
             .WithArgumentList(ArgumentList(SingletonSeparatedList(Argument(IdentifierName("value"))))))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemplo n.º 23
0
 public static MemberDeclarationSyntax OperatorNotEqualsSyntax(this PrimitiveDescriptor descriptor)
 => OperatorDeclaration(PredefinedType(Token(SyntaxKind.BoolKeyword)), Token(SyntaxKind.ExclamationEqualsToken))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
 .WithParameterList(
     ParameterList(
         SeparatedList <ParameterSyntax>(
             new SyntaxNodeOrToken[]
 {
     Parameter(Identifier("value1")).WithType(IdentifierName(descriptor.Name)), Token(SyntaxKind.CommaToken),
     Parameter(Identifier("value2")).WithType(IdentifierName(descriptor.Name))
 })))
 .WithExpressionBody(
     ArrowExpressionClause(
         PrefixUnaryExpression(
             SyntaxKind.LogicalNotExpression,
             ParenthesizedExpression(
                 BinaryExpression(
                     SyntaxKind.EqualsExpression,
                     IdentifierName("value1"),
                     IdentifierName("value2"))))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemplo n.º 24
0
 public static MemberDeclarationSyntax IComparableCompareToSyntax(this PrimitiveDescriptor descriptor)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword)), Identifier("CompareTo"))
 .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName("IComparable")))
 .WithParameterList(ParameterList(SingletonSeparatedList(Parameter(Identifier("obj")).WithType(PredefinedType(Token(SyntaxKind.ObjectKeyword))))))
 .WithBody(
     Block(
         IfStatement(
             PrefixUnaryExpression(
                 SyntaxKind.LogicalNotExpression,
                 ParenthesizedExpression(
                     IsPatternExpression(
                         IdentifierName("obj"),
                         DeclarationPattern(
                             IdentifierName(descriptor.Name),
                             SingleVariableDesignation(Identifier("other")))))),
             ThrowStatement(
                 ObjectCreationExpression(IdentifierName("ArgumentException"))
                 .WithArgumentList(
                     ArgumentList(
                         SingletonSeparatedList(
                             Argument(
                                 LiteralExpression(SyntaxKind.StringLiteralExpression,
                                                   Literal($"Object must be of type {descriptor.Name}")))))))),
         ReturnStatement(
             InvocationExpression(
                 MemberAccessExpression(
                     SyntaxKind.SimpleMemberAccessExpression,
                     MemberAccessExpression(
                         SyntaxKind.SimpleMemberAccessExpression,
                         ThisExpression(),
                         IdentifierName(descriptor.InnerName)),
                     IdentifierName("CompareTo")))
             .WithArgumentList(
                 ArgumentList(
                     SingletonSeparatedList(
                         Argument(
                             MemberAccessExpression(
                                 SyntaxKind.SimpleMemberAccessExpression,
                                 IdentifierName("other"),
                                 IdentifierName(descriptor.InnerName)))))))));
Exemplo n.º 25
0
 public static MemberDeclarationSyntax StringGetHashCodeSyntax(this PrimitiveDescriptor descriptor, StringComparison stringComparison)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword)), Identifier("GetHashCode"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword)))
 .WithExpressionBody(
     ArrowExpressionClause(
         ConditionalExpression(
             BinaryExpression(
                 SyntaxKind.NotEqualsExpression,
                 IdentifierName(descriptor.InnerName),
                 LiteralExpression(SyntaxKind.NullLiteralExpression)),
             InvocationExpression(
                 MemberAccessExpression(
                     SyntaxKind.SimpleMemberAccessExpression,
                     MemberAccessExpression(
                         SyntaxKind.SimpleMemberAccessExpression,
                         IdentifierName("StringComparer"),
                         IdentifierName(stringComparison.ToString())),
                     IdentifierName("GetHashCode")))
             .WithArgumentList(
                 ArgumentList(
                     SingletonSeparatedList(Argument(IdentifierName(descriptor.InnerName))))),
             LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemplo n.º 26
0
 public static MemberDeclarationSyntax IComparableTCompareToSyntax(this PrimitiveDescriptor descriptor)
 => MethodDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword)), Identifier("CompareTo"))
 .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
 .WithParameterList(
     ParameterList(SingletonSeparatedList(Parameter(Identifier("other")).WithType(IdentifierName(descriptor.Name)))))
 .WithExpressionBody(
     ArrowExpressionClause(
         InvocationExpression(
             MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 MemberAccessExpression(
                     SyntaxKind.SimpleMemberAccessExpression,
                     ThisExpression(),
                     IdentifierName(descriptor.InnerName)),
                 IdentifierName("CompareTo")))
         .WithArgumentList(
             ArgumentList(
                 SingletonSeparatedList(
                     Argument(
                         MemberAccessExpression(
                             SyntaxKind.SimpleMemberAccessExpression,
                             IdentifierName("other"),
                             IdentifierName(descriptor.InnerName))))))))
 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
Exemplo n.º 27
0
        public object Create(Type type)
        {
            type = GetDefaultImplementation(type);

            // We can't instantiate primitive or arrays
            if (PrimitiveDescriptor.IsPrimitive(type) || type.IsArray)
            {
                return(null);
            }

            if (type.GetConstructor(EmptyTypes) != null || type.IsValueType)
            {
                try
                {
                    return(Activator.CreateInstance(type));
                }
                catch (Exception ex)
                {
                    throw new InstanceCreationException($"'{typeof(Activator)}' failed to create instance of type '{type}'.", ex);
                }
            }

            return(null);
        }
Exemplo n.º 28
0
    static StructDeclarationSyntax StructSyntax(PrimitiveDescriptor descriptor)
    {
        var @struct = StructDeclaration(descriptor.Name)
                      .WithModifiers(
            TokenList(Token(SyntaxKind.ReadOnlyKeyword), Token(SyntaxKind.PartialKeyword)));

        if (descriptor.MarkAsNonUserCode)
        {
            @struct = @struct
                      .WithAttributeLists(
                SingletonList(AttributeList(SingletonSeparatedList(Attribute(IdentifierName("DebuggerNonUserCode"))))));
        }

        var baseTypes = GetBaseTypes(descriptor).ToList();

        if (baseTypes.Count >= 1)
        {
            @struct = @struct
                      .WithBaseList(
                BaseList(
                    SeparatedList(
                        baseTypes,
                        Enumerable.Repeat(Token(SyntaxKind.CommaToken), baseTypes.Count - 1))));
        }

        @struct = @struct
                  .WithMembers(List(GetMembers(descriptor)));

        if (descriptor.IsInnerTypeNullableReferenceType())
        {
            @struct = @struct
                      .WithLeadingTrivia(Trivia(NullableDirectiveTrivia(Token(SyntaxKind.EnableKeyword), true)));
        }

        return(@struct);
    }
 public override void VisitPrimitive(object primitive, PrimitiveDescriptor descriptor)
 {
     Collected.Add(primitive);
 }
Exemplo n.º 30
0
 static TypeSyntax TryWrapInNullableTypeSyntax(this PrimitiveDescriptor descriptor, TypeSyntax typeSyntax)
 => descriptor.IsInnerTypeNullableReferenceType() ? NullableType(typeSyntax) : typeSyntax;