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); } }
private EventFieldDeclarationSyntax getEvent(EventInfo eventInfo, TypeNameResolver typeNameResolver) { return(EventFieldDeclaration(VariableDeclaration( type: typeNameResolver.ResolveTypeName(eventInfo.EventHandlerType), variables: SingletonSeparatedList(VariableDeclarator(eventInfo.Name)) ))); }
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)))); }
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); }
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); }
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())))); }
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); }
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()))))); }
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; } } }