Exemple #1
0
        private IEnumerable <ParameterSyntax> getParameters(TypeNameResolver typeNameResolver, params ParameterInfo[] parameters)
        {
            foreach (var parameterInfo in parameters)
            {
                var parameterSyntax = Parameter(Identifier(parameterInfo.Name))
                                      .WithType(typeNameResolver.ResolveTypeName(parameterInfo.ParameterType));

                if (parameterInfo.IsOut)
                {
                    parameterSyntax = parameterSyntax.AddModifiers(Token(SyntaxKind.OutKeyword));
                }
                else if (parameterInfo.IsIn)
                {
                    parameterSyntax = parameterSyntax.AddModifiers(Token(SyntaxKind.RefKeyword));
                }
                else if (parameterInfo.IsOptional)
                {
                    // TODO: Handle optional parameters. (Daniel Potter, 11/8/2017)
                }
                else if (parameterInfo.GetCustomAttribute(typeof(ParamArrayAttribute)) != null)
                {
                    parameterSyntax = parameterSyntax.AddModifiers(Token(SyntaxKind.ParamKeyword));
                }

                yield return(parameterSyntax);
            }
        }
Exemple #2
0
 private EventFieldDeclarationSyntax getEvent(EventInfo eventInfo, TypeNameResolver typeNameResolver)
 {
     return(EventFieldDeclaration(VariableDeclaration(
                                      type: typeNameResolver.ResolveTypeName(eventInfo.EventHandlerType),
                                      variables: SingletonSeparatedList(VariableDeclarator(eventInfo.Name))
                                      )));
 }
Exemple #3
0
        public NamespaceDeclarationSyntax ReadNamespace(Type type, TypeNameResolver typeNameResolver = null)
        {
            if (typeNameResolver == null)
            {
                typeNameResolver = new TypeNameResolver
                {
                    SimplifyNamespaces = false,
                };
            }

            return(NamespaceDeclaration(IdentifierName(type.Namespace))
                   .WithMembers(SingletonList(readType(type, typeNameResolver))));
        }
Exemple #4
0
        private MethodDeclarationSyntax getMethod(MethodInfo methodInfo, TypeNameResolver typeNameResolver)
        {
            MethodDeclarationSyntax methodDeclaration = MethodDeclaration(typeNameResolver.ResolveTypeName(methodInfo.ReturnType), methodInfo.Name)
                                                        .WithParameterList(ParameterList(SeparatedList(getParameters(typeNameResolver, methodInfo.GetParameters()))))
                                                        .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));

            if (methodInfo.IsGenericMethod)
            {
                methodDeclaration = methodDeclaration
                                    .WithConstraintClauses(List(getTypeConstraints(methodInfo)))
                                    .WithTypeParameterList(TypeParameterList(SeparatedList(getTypeParameters(methodInfo))));
            }

            return(methodDeclaration);
        }
Exemple #5
0
        public CompilationUnitSyntax ReadCompilationUnit(Type type, TypeNameResolver typeNameResolver = null)
        {
            if (typeNameResolver == null)
            {
                typeNameResolver = new TypeNameResolver
                {
                    SimplifyNamespaces = false,
                };
            }

            var compilationUnit = CompilationUnit()
                                  .WithMembers(SingletonList <MemberDeclarationSyntax>(ReadNamespace(type, typeNameResolver)));

            compilationUnit = compilationUnit
                              .WithUsings(List(typeNameResolver.GetRegisteredNamespaces().Select(qualifiedNamespace => UsingDirective(ParseName(qualifiedNamespace)))));

            return(compilationUnit);
        }
Exemple #6
0
        private PropertyDeclarationSyntax getProperty(PropertyInfo propertyInfo, TypeNameResolver typeNameResolver)
        {
            IEnumerable <AccessorDeclarationSyntax> getAccessors()
            {
                if (propertyInfo.CanRead)
                {
                    yield return(AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                                 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
                }

                if (propertyInfo.CanWrite)
                {
                    yield return(AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
                                 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
                }
            }

            return(PropertyDeclaration(typeNameResolver.ResolveTypeName(propertyInfo.PropertyType), propertyInfo.Name)
                   .WithAccessorList(AccessorList(List(getAccessors()))));
        }
Exemple #7
0
        private MemberDeclarationSyntax readType(Type type, TypeNameResolver typeNameResolver)
        {
            if (type.IsClass || type.IsInterface || type.IsValueType)
            {
                var declaration = InterfaceDeclaration(getApiTypeIdentifier(type))
                                  .WithBaseList(getBaseList(type))
                                  .WithMembers(List(getMembers(type, typeNameResolver)))
                                  .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)));

                if (type.IsGenericType)
                {
                    declaration = declaration
                                  .WithConstraintClauses(List(getTypeConstraints(type)))
                                  .WithTypeParameterList(TypeParameterList(SeparatedList(getTypeParameters(type))));
                }

                return(declaration);
            }

            return(null);
        }
Exemple #8
0
        private IndexerDeclarationSyntax getIndexer(PropertyInfo propertyInfo, TypeNameResolver typeNameResolver)
        {
            IEnumerable <AccessorDeclarationSyntax> getAccessors()
            {
                if (propertyInfo.CanRead)
                {
                    yield return(AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                                 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
                }

                if (propertyInfo.CanWrite)
                {
                    yield return(AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
                                 .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
                }
            }

            return(IndexerDeclaration(typeNameResolver.ResolveTypeName(propertyInfo.PropertyType))
                   .WithAccessorList(AccessorList(List(getAccessors())))
                   .WithParameterList(BracketedParameterList(SeparatedList(getParameters(typeNameResolver, propertyInfo.GetIndexParameters())))));
        }
Exemple #9
0
        private IEnumerable <MemberDeclarationSyntax> getMembers(Type type, TypeNameResolver typeNameResolver)
        {
            // TODO: Add static members to a manager interface. (Daniel Potter, 11/8/2017)
            foreach (MemberInfo memberInfo in type.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
            {
                if (memberInfo.GetCustomAttribute(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute)) != null)
                {
                    continue;
                }

                MemberExtensionKind extensionKind;
                switch (memberInfo.MemberType)
                {
                case MemberTypes.Constructor:
                    // TODO: Constructors should be added to a manager interface. (Daniel
                    //       Potter, 11/8/2017)
                    break;

                case MemberTypes.Event:
                    var eventInfo = (EventInfo)memberInfo;

                    extensionKind = eventInfo.GetExtensionKind();
                    if (extensionKind == MemberExtensionKind.Override)
                    {
                        continue;
                    }

                    EventFieldDeclarationSyntax eventDeclaration = getEvent(eventInfo, typeNameResolver);

                    if (extensionKind == MemberExtensionKind.New)
                    {
                        eventDeclaration = eventDeclaration
                                           .AddModifiers(Token(SyntaxKind.NewKeyword));
                    }

                    yield return(eventDeclaration);

                    break;

                case MemberTypes.Field:
                    // TODO: Constants need to be handled somehow. (Daniel Potter, 11/8/2017)
                    break;

                case MemberTypes.Method:
                    var methodInfo = (MethodInfo)memberInfo;

                    if (methodInfo.IsSpecialName)
                    {
                        continue;
                    }

                    extensionKind = methodInfo.GetExtensionKind();
                    if (extensionKind == MemberExtensionKind.Override)
                    {
                        continue;
                    }

                    MethodDeclarationSyntax methodDeclaration = getMethod(methodInfo, typeNameResolver);

                    if (extensionKind == MemberExtensionKind.New)
                    {
                        methodDeclaration = methodDeclaration
                                            .AddModifiers(Token(SyntaxKind.NewKeyword));
                    }

                    yield return(methodDeclaration);

                    break;

                case MemberTypes.Property:
                    var propertyInfo = (PropertyInfo)memberInfo;

                    extensionKind = propertyInfo.GetExtensionKind();
                    if (extensionKind == MemberExtensionKind.Override)
                    {
                        continue;
                    }

                    if (propertyInfo.GetIndexParameters().Length > 0)
                    {
                        IndexerDeclarationSyntax indexerDeclaration = getIndexer(propertyInfo, typeNameResolver);

                        if (extensionKind == MemberExtensionKind.New)
                        {
                            indexerDeclaration = indexerDeclaration
                                                 .AddModifiers(Token(SyntaxKind.NewKeyword));
                        }

                        yield return(indexerDeclaration);
                    }
                    else
                    {
                        PropertyDeclarationSyntax propertyDeclaration = getProperty(propertyInfo, typeNameResolver);

                        if (extensionKind == MemberExtensionKind.New)
                        {
                            propertyDeclaration = propertyDeclaration
                                                  .AddModifiers(Token(SyntaxKind.NewKeyword));
                        }

                        yield return(propertyDeclaration);
                    }
                    break;

                case MemberTypes.NestedType:
                    // TODO: Nested types need to be handled somehow. (Daniel Potter, 11/8/2017)
                    break;

                default:
                    // TODO: Log these for diagnostics. (Daniel Potter, 11/8/2017)
                    break;
                }
            }
        }