本文整理汇总了C#中NamespaceDeclaration.AddMember方法的典型用法代码示例。如果您正苦于以下问题:C# NamespaceDeclaration.AddMember方法的具体用法?C# NamespaceDeclaration.AddMember怎么用?C# NamespaceDeclaration.AddMember使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NamespaceDeclaration
的用法示例。
在下文中一共展示了NamespaceDeclaration.AddMember方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddChildTypeDeclaration
public static void AddChildTypeDeclaration(this AstNode tree, TypeDeclaration newClass,
NamespaceDeclaration parentNamespace = null)
{
if (null != parentNamespace)
{
var newNamespaceNode = new NamespaceDeclaration(
parentNamespace.Name);
newNamespaceNode.AddMember(newClass);
tree.AddChild(newNamespaceNode, SyntaxTree.MemberRole);
}
else
{
tree.AddChild(newClass, Roles.TypeMemberRole);
}
}
示例2: Execute
public override async void Execute(EditorRefactoringContext context)
{
SyntaxTree st = await context.GetSyntaxTreeAsync().ConfigureAwait(false);
ICompilation compilation = await context.GetCompilationAsync().ConfigureAwait(false);
CSharpFullParseInformation info = await context.GetParseInformationAsync().ConfigureAwait(false) as CSharpFullParseInformation;
EntityDeclaration node = (EntityDeclaration)st.GetNodeAt(context.CaretLocation, n => n is TypeDeclaration || n is DelegateDeclaration);
IDocument document = context.Editor.Document;
FileName newFileName = FileName.Create(Path.Combine(Path.GetDirectoryName(context.FileName), MakeValidFileName(node.Name)));
string header = CopyFileHeader(document, info);
string footer = CopyFileEnd(document, info);
AstNode newNode = node.Clone();
foreach (var ns in node.Ancestors.OfType<NamespaceDeclaration>()) {
var newNS = new NamespaceDeclaration(ns.Name);
newNS.Members.AddRange(ns.Children.Where(ch => ch is UsingDeclaration
|| ch is UsingAliasDeclaration
|| ch is ExternAliasDeclaration).Select(usingDecl => usingDecl.Clone()));
newNS.AddMember(newNode);
newNode = newNS;
}
var topLevelUsings = st.Children.Where(ch => ch is UsingDeclaration
|| ch is UsingAliasDeclaration
|| ch is ExternAliasDeclaration);
StringBuilder newCode = new StringBuilder(header);
CSharpOutputVisitor visitor = new CSharpOutputVisitor(new StringWriter(newCode), FormattingOptionsFactory.CreateSharpDevelop());
foreach (var topLevelUsing in topLevelUsings)
topLevelUsing.AcceptVisitor(visitor);
newNode.AcceptVisitor(visitor);
newCode.AppendLine(footer);
IViewContent viewContent = FileService.NewFile(newFileName, newCode.ToString());
viewContent.PrimaryFile.SaveToDisk(newFileName);
// now that the code is saved in the other file, remove it from the original document
RemoveExtractedNode(context, node);
IProject project = (IProject)compilation.GetProject();
if (project != null) {
FileProjectItem projectItem = new FileProjectItem(project, ItemType.Compile);
projectItem.FileName = newFileName;
ProjectService.AddProjectItem(project, projectItem);
FileService.FireFileCreated(newFileName, false);
project.Save();
ProjectBrowserPad.RefreshViewAsync();
}
}