Ejemplo n.º 1
0
        /// <summary>
        /// Returns a list of all namespaces referenced/used in this property.
        /// </summary>
        internal IEnumerable <string> GetNamespaces()
        {
            foreach (string ns in Type.GetNamespaces())
            {
                yield return(ns);
            }

            foreach (string?ns in Attributes.Select(x => x.Identifier.Namespace))
            {
                if (ns != null)
                {
                    yield return(ns);
                }
            }

            if (Initializer != null)
            {
                foreach (string ns in Initializer.GetNamespaces())
                {
                    yield return(ns);
                }
            }

            if (GetterExpression != null)
            {
                foreach (string ns in GetterExpression.GetNamespaces())
                {
                    yield return(ns);
                }
            }
        }
Ejemplo n.º 2
0
        public PropertyDeclarationSyntax ToSyntax()
        {
            var propertyDeclaration = PropertyDeclaration(Type.ToSyntax(), Identifier(Name))
                                      .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
                                      .WithDocumentation(Description);

            return((GetterExpression == null)
                ? propertyDeclaration.WithAccessorList(AccessorList(List(GetAccessors())))
                : propertyDeclaration.WithExpressionBody(ArrowExpressionClause(GetterExpression.ToNewSyntax()))
                   .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns a Roslyn syntax for the property.
        /// </summary>
        /// <param name="makePublic">Controls whether to make the property public or not.</param>
        internal PropertyDeclarationSyntax ToSyntax(bool makePublic = false)
        {
            var declaration = PropertyDeclaration(Type.ToSyntax(), Identifier(Name));

            if (makePublic)
            {
                declaration = declaration.AddModifiers(Token(SyntaxKind.PublicKeyword));
            }

            declaration = declaration.WithAttributeLists(List(Attributes.Select(x => x.ToSyntax())))
                          .WithDocumentation(Summary);

            if (GetterExpression != null)
            {
                if (Initializer != null)
                {
                    throw new InvalidOperationException($"{nameof(GetterExpression)} and {nameof(Initializer)} may not be both set for the same {nameof(CSharpProperty)}.");
                }

                if (HasSetter)
                {
                    throw new InvalidOperationException($"{nameof(GetterExpression)} and {nameof(HasSetter)} may not be both set for the same {nameof(CSharpProperty)}.");
                }

                declaration = declaration.WithExpressionBody(ArrowExpressionClause(GetterExpression.ToInvocationSyntax()))
                              .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
            }
            else
            {
                var accessors = new List <SyntaxKind> {
                    SyntaxKind.GetAccessorDeclaration
                };
                if (HasSetter)
                {
                    accessors.Add(SyntaxKind.SetAccessorDeclaration);
                }

                declaration = declaration.WithAccessorList(AccessorList(List(
                                                                            accessors.Select(x => AccessorDeclaration(x).WithSemicolonToken(Token(SyntaxKind.SemicolonToken))))));
            }

            if (Initializer != null)
            {
                declaration = declaration.WithInitializer(EqualsValueClause(Initializer.ToInvocationSyntax()))
                              .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
            }

            return(declaration);
        }
Ejemplo n.º 4
0
        public IEnumerable <string> GetNamespaces()
        {
            foreach (string ns in Type.GetNamespaces())
            {
                yield return(ns);
            }

            if (GetterExpression != null)
            {
                foreach (string ns in GetterExpression.GetNamespaces())
                {
                    yield return(ns);
                }
            }
        }
Ejemplo n.º 5
0
        void SetExpressions()
        {
            var objParam   = Expression.Parameter(typeof(object), "obj");
            var getterExpr = GetterExpression.GetBody(Expression.Convert(objParam, TypeAccessor.Type));
            var getter     = Expression.Lambda <Func <object, object> >(Expression.Convert(getterExpr, typeof(object)), objParam);

            Getter = getter.Compile();

            var valueParam = Expression.Parameter(typeof(object), "value");
            var setterExpr = SetterExpression.GetBody(
                Expression.Convert(objParam, TypeAccessor.Type),
                Expression.Convert(valueParam, Type));
            var setter = Expression.Lambda <Action <object, object> >(setterExpr, objParam, valueParam);

            Setter = setter.Compile();
        }
Ejemplo n.º 6
0
        void SetExpressions()
        {
            var objParam   = Expression.Parameter(typeof(object), "obj");
            var getterExpr = GetterExpression.GetBody(Expression.Convert(objParam, TypeAccessor.Type));
            var getter     = Expression.Lambda <Func <object, object?> >(Expression.Convert(getterExpr, typeof(object)), objParam);

            try
            {
                Getter = getter.Compile();
            }
            catch (Exception e)
            {
                ThrowCompileException(getter, e, true);
            }

            var valueParam = Expression.Parameter(typeof(object), "value");

            if (SetterExpression != null)
            {
                var setterExpr = SetterExpression.GetBody(
                    Expression.Convert(objParam, TypeAccessor.Type),
                    Expression.Convert(valueParam, Type));
                var setter = Expression.Lambda <Action <object, object?> >(setterExpr, objParam, valueParam);
                try
                {
                    Setter = setter.Compile();
                }
                catch (Exception e)
                {
                    ThrowCompileException(setter, e, false);
                }
            }

            void ThrowCompileException(Expression expression, Exception e, bool isGetter)
            {
                string message = String.Format("Failed to compile {0} expression for '{1}'. Expression: {2}",
                                               (isGetter ? "Getter" : "Setter"), this.Name, expression.ToString()
                                               );

                throw new InvalidOperationException(message, e);
            }
        }