コード例 #1
0
ファイル: CodeFixProvider.cs プロジェクト: kabua/MVA-Roslyn
        private async Task <Document> MakePrivateDeclarationAsync(Document document, FieldDeclarationSyntax declaration, CancellationToken c)
        {
            var accessToken = declaration.ChildTokens()
                              .SingleOrDefault(token => token.Kind() == SyntaxKind.PublicKeyword);

            var privateAccessToken = SyntaxFactory.Token(SyntaxKind.PrivateKeyword);

            var root = await document.GetSyntaxRootAsync(c);

            var newRoot = root.ReplaceToken(accessToken, privateAccessToken);

            return(document.WithSyntaxRoot(newRoot));
        }
コード例 #2
0
ファイル: TypeConversion.cs プロジェクト: SharpNative/CS2C
        /// <summary>
        /// Converts a variable name from C# to C
        /// </summary>
        /// <param name="node">The node</param>
        /// <returns>The converted variable name</returns>
        public string ConvertVariableName(SyntaxNode node)
        {
            string  typeNameConverted;
            ISymbol symbol = m_context.Model.GetSymbolInfo(node).Symbol;

            // Property
            if (symbol.Kind == SymbolKind.Property)
            {
                if (symbol.IsStatic)
                {
                    typeNameConverted = string.Format("{0}_{1}_getter()", symbol.ContainingType.ToString().Replace(".", "_"), symbol.Name);
                }
                else
                {
                    typeNameConverted = string.Format("{0}_{1}_getter", symbol.ContainingType.ToString().Replace(".", "_"), symbol.Name);
                    string currentClass = m_context.CurrentNamespace.Name + "." + m_context.CurrentClass.Identifier;
                    if (currentClass == symbol.ContainingType.ToString())
                    {
                        typeNameConverted += "(obj)";
                    }
                }
            }
            // Method
            else if (symbol.Kind == SymbolKind.Method)
            {
                typeNameConverted = m_context.Generators.MethodDeclaration.CreateMethodPrototype((IMethodSymbol)symbol, false, false);
            }
            // Static field
            else if (symbol.IsStatic)
            {
                FieldDeclarationSyntax    fieldDeclaration = symbol.DeclaringSyntaxReferences[0].GetSyntax().Parent.Parent as FieldDeclarationSyntax;
                IEnumerable <SyntaxToken> children         = fieldDeclaration.ChildTokens();

                bool isConst = false;
                foreach (SyntaxToken token in children)
                {
                    if (token.Kind() == SyntaxKind.ConstKeyword)
                    {
                        isConst = true;
                        break;
                    }
                }

                if (isConst)
                {
                    typeNameConverted = string.Format("const_{0}_{1}", symbol.ContainingType.ToString().Replace(".", "_"), symbol.Name);
                }
                else
                {
                    typeNameConverted = string.Format("classStatics_{0}.{1}", symbol.ContainingType.ToString().Replace(".", "_"), symbol.Name);
                }
            }
            // Argument or local variable
            else if (symbol.ContainingSymbol.Kind == SymbolKind.Method)
            {
                typeNameConverted = symbol.Name;
            }
            // Field
            else
            {
                typeNameConverted = string.Format("field_{0}", symbol.Name);
            }

            return(typeNameConverted);
        }