public static ClassDeclarationSyntax AddBase(ClassDeclarationSyntax type, string baseName) { if (type.BaseList == null) { type = type.WithBaseList(SF.BaseList()); } return(type.WithBaseList(AddBase(type.BaseList, baseName))); }
public static BaseListSyntax BaseList(params IdentifierNameSyntax[] baseNames) => SF.BaseList(SF.SeparatedList(baseNames.Select(n => SF.SimpleBaseType(n)).Cast <BaseTypeSyntax>()));
public static BaseListSyntax RemoveBase(BaseListSyntax baseList, string baseName) { BaseTypeSyntax baseType = baseList.Types.First(type => type.Type.ToString() == baseName); return(SF.BaseList(baseList.Types.Remove(baseType))); }
public static BaseListSyntax BaseList(IEnumerable <string> names) { return(SF.BaseList(SF.SeparatedList <BaseTypeSyntax>(names.Select(name => SF.SimpleBaseType(SF.IdentifierName(name)))))); }
private static ClassDeclarationSyntax GenerateApiControllerForInterface(InterfaceDeclarationSyntax interfaceNode, SemanticModel semanticModel) { if (!RoslynUtils.IsPublic(interfaceNode)) { return(null); } AttributeSyntax apiControllerAttribute = AttributeUtils.SelectAttributeOfType(interfaceNode.AttributeLists, typeof(ApiControllerAttribute), semanticModel); // if the interface is not annotated with the ApiController attribute, do nothing if (apiControllerAttribute == null) { return(null); } var namespaceNode = interfaceNode.Parent as NamespaceDeclarationSyntax; if (namespaceNode == null) { throw new Exception("A grain interface must be declared inside a namespace"); } // copy all attributes except the ApiController attribute SyntaxList <AttributeListSyntax> attributesLists = AttributeUtils.RemoveAttributeOfType(interfaceNode.AttributeLists, typeof(ApiControllerAttribute), semanticModel); // add the RoutePrefix attribute (if any) var attributeInspector = new AttributeInspector(apiControllerAttribute, semanticModel); if (attributeInspector.NamedArguments.ContainsKey(RoutePrefix)) { AttributeSyntax routePrefixAttribute = AttributeUtils.AttributeWithArgument(RoutePrefix, attributeInspector.NamedArguments[RoutePrefix]); attributesLists = attributesLists.Add(AttributeUtils.AttributeList(routePrefixAttribute)); } string grainName = interfaceNode.Identifier.Text; string apiControllerName = GetApiControllerName(grainName); // create the Api controller class, add the attributes to it and make it a subclass of ApiController ClassDeclarationSyntax classDclr = SF.ClassDeclaration(SF.Identifier(apiControllerName)).AddModifiers(SF.Token(SyntaxKind.PublicKeyword), SF.Token(SyntaxKind.PartialKeyword)).WithAttributeLists(SF.List(attributesLists)).WithBaseList(SF.BaseList(SF.SeparatedList <BaseTypeSyntax>().Add(SF.SimpleBaseType(SF.IdentifierName(ApicontrollerClassName))))); // Add the IGrainFactory field and to the constructor classDclr = RoslynUtils.AddField(classDclr, "IGrainFactory", "_grainFactory"); MethodDeclarationSyntax constructor = RoslynUtils.ParseMethod(string.Format(@" public {0}(IGrainFactory grainFactory) {{ _grainFactory = grainFactory; }}", apiControllerName)).WithTrailingTrivia(SF.EndOfLine("")); classDclr = classDclr.AddMembers(constructor); // generate the api controller methods and add them to the class IEnumerable <MethodDeclarationSyntax> apiControllerMethods = GenerateApiControllerMethods(RoslynUtils.GetMethodDeclarations(interfaceNode), grainName); // ReSharper disable once LoopCanBeConvertedToQuery foreach (var method in apiControllerMethods) { classDclr = classDclr.AddMembers(method); } return(classDclr); }