internal static void Specialize(Context context, RewriteInfo rewriteInfo) { rewriteInfo.ElementFieldColumns = rewriteInfo.ElementType.GetFields().Select(field => { var declaredSymbol = field.IsImplicitlyDeclared ? field.AssociatedSymbol : field; if (null != declaredSymbol) { return new { field, groupName = GetGroupName(context, declaredSymbol) } } ; else { throw new TranspilationException($"Could not find associated symbol for implicitly declared field: {field}"); } }).ToDictionary(x => x.field, x => x.groupName); rewriteInfo.Specialization = SF.ClassDeclaration($"{context.UnboundValueArrayType.Name}_{rewriteInfo.ElementType.Name}") .WithMembers(SF.List(GenerateMembers(context, rewriteInfo))); }
private static ClassDeclarationSyntax GenerateClassSqueleton(string className) { return(SF.ClassDeclaration(SF.Identifier(className)).AddModifiers(SF.Token(SyntaxKind.PublicKeyword))); }
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); }