gpt4 book ai didi

c# - 在 C# 中合并两个 .CS 文件以生成一个新类

转载 作者:行者123 更新时间:2023-11-30 15:14:29 27 4
gpt4 key购买 nike

我想合并两个 .cs 文件以创建第三个文件。谁能帮帮我。

public partial class A 
{
// some methods
}

假设这段代码写在文件A.cs中

public partial class B 
{
// some methods
}

而这段代码写在文件B.cs中。我想生成一个新的 C.csA.csB.cs 的所有代码都忽略了命名空间。

最佳答案

我假设您确实想要合并相同 类的部分定义。如果您确实需要将不同的类合并为一个类,可以轻松调整代码,但不能保证它可以编译(因为,例如,这些类可能具有同名成员)。


由于符号的含义,问题确实相当复杂:它取决于用途,因此合并它们时需要非常小心。

所以最好的办法不是尝试手动分析代码语义,而是使用一个大锤子:Roslyn 分析器。

让我们开始吧。

首先,您需要按照描述安装扩展开发工作负载 here .在此之后,您将能够创建一个独立的代码分析工具项目。

当你创建它时,你会得到很多有用的样板代码,如下所示:

class Program
{
static async Task Main(string[] args)
{
// ...
using (var workspace = MSBuildWorkspace.Create())
{
var solutionPath = args[0];
WriteLine($"Loading solution '{solutionPath}'");

var solution = await workspace.OpenSolutionAsync(solutionPath,
new ConsoleProgressReporter());
WriteLine($"Finished loading solution '{solutionPath}'");

// insert your code here
}
}

private static VisualStudioInstance SelectVisualStudioInstance(
VisualStudioInstance[] visualStudioInstances)
{
// ...
}

private class ConsoleProgressReporter : IProgress<ProjectLoadProgress>
{
// ...
}
}

让我们填写需要的内容。

代替 //在此处插入您的代码 让我们输入以下代码:

var targetClass = args[1];
var modifiedSolution = await MergePartialClasses(targetClass, solution);
workspace.TryApplyChanges(modifiedSolution);

我们需要在 MergePartialClasses 中实现逻辑。类的名称应作为第二个命令行参数传递。

让我们首先在顶部添加以下用法:

using static System.Console;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

现在我们可以从main方法开始了。我已将有关正在发生的事情的评论直接放在代码中。

static async Task<Solution> MergePartialClasses(string targetClass, Solution solution)
{
// https://stackoverflow.com/a/32179708/276994
// we loop through the projects in the solution and process each of the projects
foreach (var projectId in solution.ProjectIds)
{
var project = solution.GetProject(projectId);
WriteLine($"Processing project {project.Name}");
var compilation = await project.GetCompilationAsync();

// finding the type which we want to merge
var type = compilation.GetTypeByMetadataName(targetClass);
if (type == null)
{
WriteLine($"Type {targetClass} is not found");
return solution;
}

// look up number of declarations. if it's only 1, we have nothing to merge
var declarationRefs = type.DeclaringSyntaxReferences;
if (declarationRefs.Length <= 1)
{
WriteLine($"Type {targetClass} has only one location");
return solution;
}

// I didn't implement the case of nested types, which would require to
// split the outer class, too
if (type.ContainingType != null)
throw new NotImplementedException("Splitting nested types");

// we'll accumulate usings and class members as we traverse all the definitions
var accumulatedUsings = new List<UsingDirectiveSyntax>();
var classParts = new List<ClassDeclarationSyntax>();
foreach (var declarationRef in declarationRefs)
{
var declaration = (ClassDeclarationSyntax)await declarationRef.GetSyntaxAsync();
// get hold of the usings
var tree = declaration.SyntaxTree;
var root = await tree.GetRootAsync();
var usings = root.DescendantNodes().OfType<UsingDirectiveSyntax>();
accumulatedUsings.AddRange(usings);
// since we are trying to move the syntax into another file,
// we need to expand everything in order to remove the dependency
// on usings
// in order to do it, we use a custom CSharpSyntaxRewriter (defined later)
var document = project.GetDocument(tree);
var expander = new AllSymbolsExpander(document);
var expandedDeclaration = (ClassDeclarationSyntax)expander.Visit(declaration);
classParts.Add(expandedDeclaration);
// remove the old declaration from the place where it is
// we can't just remove the whole file as it may contain some other classes
var modifiedRoot =
root.RemoveNodes(new[] { declaration }, SyntaxRemoveOptions.KeepNoTrivia);
var modifiedDocument = document.WithSyntaxRoot(modifiedRoot);
project = modifiedDocument.Project;
}

// now, sort the usings and remove the duplicates
// in order to use DistinctBy, I added MoreLinq nuget package and added
// using MoreLinq; at the beginning
// https://stackoverflow.com/a/34063289/276994
var sortedUsings = accumulatedUsings
.DistinctBy(x => x.Name.ToString())
.OrderBy(x => x.StaticKeyword.IsKind(SyntaxKind.StaticKeyword) ?
1 : x.Alias == null ? 0 : 2)
.ThenBy(x => x.Alias?.ToString())
.ThenByDescending(x => x.Name.ToString().StartsWith(nameof(System) + "."))
.ThenBy(x => x.Name.ToString());

// now, we have to merge the class definitions.
// split the name into namespace and class name
var (nsName, className) = SplitName(targetClass);

// gather all the attributes
var attributeLists = List(classParts.SelectMany(p => p.AttributeLists));
// modifiers must be the same, so we are taking them from the
// first definition, but remove partial if it's there
var modifiers = classParts[0].Modifiers;
var partialModifier = modifiers.FirstOrDefault(
m => m.Kind() == SyntaxKind.PartialKeyword);
if (partialModifier != null)
modifiers = modifiers.Remove(partialModifier);
// gather all the base types
var baseTypes =
classParts
.SelectMany(p => p.BaseList?.Types ?? Enumerable.Empty<BaseTypeSyntax>())
.Distinct()
.ToList();
var baseList = baseTypes.Count > 0 ? BaseList(SeparatedList(baseTypes)) : null;
// and constraints (I hope that Distinct() works as expected)
var constraintClauses =
List(classParts.SelectMany(p => p.ConstraintClauses).Distinct());

// now, we construct class members by pasting together the accumulated
// per-part member lists
var members = List(classParts.SelectMany(p => p.Members));

// now we can build the class declaration
var classDef = ClassDeclaration(
attributeLists: attributeLists,
modifiers: modifiers,
identifier: Identifier(className),
typeParameterList: classParts[0].TypeParameterList,
baseList: baseList,
constraintClauses: constraintClauses,
members: members);

// if there was a namespace, let's put the class inside it
var body = (nsName == null) ?
(MemberDeclarationSyntax)classDef :
NamespaceDeclaration(IdentifierName(nsName)).AddMembers(classDef);

// now create the compilation unit and insert it into the project
// http://roslynquoter.azurewebsites.net/
var newTree = CompilationUnit()
.WithUsings(List(sortedUsings))
.AddMembers(body)
.NormalizeWhitespace();
var newDocument = project.AddDocument(className, newTree);
var simplifiedNewDocument = await Simplifier.ReduceAsync(newDocument);
project = simplifiedNewDocument.Project;

solution = project.Solution;
}

// finally, return the modified solution
return solution;
}

剩下的是 AllSymbolsExpander,它只是为每个节点调用 Simplifier.ExpandAsync:

class AllSymbolsExpander : CSharpSyntaxRewriter
{
Document document;
public AllSymbolsExpander(Document document)
{
this.document = document;
}

public override SyntaxNode VisitAttribute(AttributeSyntax node) =>
Expand(node);
public override SyntaxNode VisitAttributeArgument(AttributeArgumentSyntax node) =>
Expand(node);
public override SyntaxNode VisitConstructorInitializer(ConstructorInitializerSyntax node) =>
Expand(node);
public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node) =>
Expand(node);
public override SyntaxNode VisitXmlNameAttribute(XmlNameAttributeSyntax node) =>
Expand(node);
public override SyntaxNode VisitTypeConstraint(TypeConstraintSyntax node) =>
Expand(node);

public override SyntaxNode DefaultVisit(SyntaxNode node)
{
if (node is ExpressionSyntax ||
node is StatementSyntax ||
node is CrefSyntax ||
node is BaseTypeSyntax)
return Expand(node);
return base.DefaultVisit(node);
}

SyntaxNode Expand(SyntaxNode node) =>
Simplifier.ExpandAsync(node, document).Result; //? async-counterpart?
}

和简单的函数SplitName:

static (string, string) SplitName(string name)
{
var pos = name.LastIndexOf('.');
if (pos == -1)
return (null, name);
else
return (name.Substring(0, pos), name.Substring(pos + 1));
}

就是这样!

关于c# - 在 C# 中合并两个 .CS 文件以生成一个新类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54591672/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com