本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax.AddUsings方法的典型用法代码示例。如果您正苦于以下问题:C# CompilationUnitSyntax.AddUsings方法的具体用法?C# CompilationUnitSyntax.AddUsings怎么用?C# CompilationUnitSyntax.AddUsings使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax
的用法示例。
在下文中一共展示了CompilationUnitSyntax.AddUsings方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TranslationUnitPorter
public TranslationUnitPorter(CXCursor translationUnit, string ns, IBindingLocator bindingLocator)
{
if (translationUnit.kind != CXCursorKind.CXCursor_TranslationUnit)
throw new ArgumentException ();
if (string.IsNullOrWhiteSpace (ns))
throw new ArgumentException ();
if (bindingLocator == null)
throw new ArgumentNullException ();
this.translationUnit = translationUnit;
this.bindingLocator = bindingLocator;
cu = SyntaxFactory.CompilationUnit ();
IEnumerable<UsingDirectiveSyntax> usings = CreateUsings ();
foreach (var u in usings)
cu = cu.AddUsings (u);
var nsDecl = CreateNamespace (ns);
foreach (var c in PortClasses ())
nsDecl = nsDecl.AddMembers (c);
cu = cu.AddMembers (nsDecl);
}
示例2: AddRequiredUsings
static CompilationUnitSyntax AddRequiredUsings(CompilationUnitSyntax root)
{
NameSyntax name = SyntaxFactory.IdentifierName(" System");
if (root.Usings.Contains(SyntaxFactory.UsingDirective(name)) == false)
return root.AddUsings(SyntaxFactory.UsingDirective(name));
return root;
}
示例3: ImportNeededNamespace
private static CompilationUnitSyntax ImportNeededNamespace(CompilationUnitSyntax root, SemanticModel semanticModel, MemberAccessExpressionSyntax callerMethod)
{
var symbolInfo = semanticModel.GetSymbolInfo(callerMethod.Name);
var methodSymbol = symbolInfo.Symbol as IMethodSymbol;
if (methodSymbol == null) return root;
var namespaceDisplayString = methodSymbol.ContainingNamespace.ToDisplayString();
var hasNamespaceImported = root
.DescendantNodes()
.OfType<UsingDirectiveSyntax>()
.Select(s => s.Name.ToString())
.Any(n => n == namespaceDisplayString);
if (!hasNamespaceImported)
{
var namespaceQualifiedName = methodSymbol.ContainingNamespace.ToNameSyntax();
root = root.AddUsings(SyntaxFactory.UsingDirective(namespaceQualifiedName));
}
return root;
}
示例4: AddUsingDirectiveIfNeeded
/// <summary>
/// Adds a using directive if one doesn't already exist at the top of file
/// after existing using directives.
///
/// Does not handle the scenarios where usings are defined within an inner node of
/// given root node, ex, if the root node is CompilationUnit and usings are defined
/// within a Namespace Declaration instead of top of the file, the new using is
/// just added at the top of the file.
/// </summary>
/// <param name="namespaceName">The namespace to be added.</param>
/// <param name="rootNode">Parent syntax node for which the childs are examined
/// to see if a using with the given namespace already exists</param>
/// <returns>A new syntax node containing the new using statement as an immediate
/// child of given rootNode. If the using statement is already present, the rootNode
/// is returned. Otherwise, a new statement is added at the end of existing
/// usings and the new node is returned.</returns>
public static CompilationUnitSyntax AddUsingDirectiveIfNeeded(string namespaceName, CompilationUnitSyntax rootNode)
{
Contract.Assert(rootNode != null);
if (String.IsNullOrEmpty(namespaceName))
{
return rootNode;
}
if (rootNode.Usings.Any(usingNode => usingNode.Name.ToString() == namespaceName))
{
// Using already present, return this node
return rootNode;
}
var insertTree = CSharpSyntaxTree.ParseText("using " + namespaceName + ";" + Environment.NewLine);
var usingStatement = insertTree.GetRoot().ChildNodes().First() as UsingDirectiveSyntax;
Debug.Assert(usingStatement != null);
return rootNode.AddUsings(usingStatement);
}
示例5: RetrieveAllUsingDirectivesAndCopyAtRootLevel
/// <summary>
/// A rearrangement of the AST and a replacement of classes into new namespaces need to happen, this will mess up the references,
/// thus we need to transplant the using directives in the new tree as well.
///
/// TODO: This approach generates a lot of duplicated using directives. It is not harmful. Make it better.
/// </summary>
private void RetrieveAllUsingDirectivesAndCopyAtRootLevel()
{
List<UsingDirectiveSyntax> usingDirectives = new List<UsingDirectiveSyntax>();
// Collecting
new MultiPurposeASTWalker(this.newNode,
astNode => astNode as UsingDirectiveSyntax != null,
delegate (SyntaxNode astNode)
{
var usingNode = astNode as UsingDirectiveSyntax;
usingDirectives.Add(usingNode);
})
.Start();
// Copying at root level in the new node
newNode = newNode.AddUsings(usingDirectives.ToArray());
// Add using directives for namespaces of types which has been overriden
var additionalNamespaces = this.GetOriginalNamespaces().Select(ns => SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(ns)).NormalizeWhitespace());
newNode = newNode.AddUsings(additionalNamespaces.ToArray());
}