Repository navigation
Expand file tree
/
Copy pathAssemblyMethodAnalyzer.cs
More file actions
94 lines (85 loc) · 5.18 KB
/
Copy pathAssemblyMethodAnalyzer.cs
File metadata and controls
94 lines (85 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Linq;
using FolderDiffIL4DotNet.Models;
namespace FolderDiffIL4DotNet.Services
{
/// <summary>
/// Compares two .NET assemblies at the metadata level using <see cref="System.Reflection.Metadata"/>
/// to detect type, method, property, and field additions/removals/modifications.
/// Modified detection includes: method IL body changes, access modifier changes (e.g. public → internal),
/// modifier changes (e.g. adding/removing static/virtual), and property/field type changes.
/// Returns structured <see cref="MemberChangeEntry"/> records for table-style rendering.
/// <see cref="System.Reflection.Metadata"/> を使用して 2 つの .NET アセンブリのメタデータを比較し、
/// 型・メソッド・プロパティ・フィールドの追加・削除・変更を検出します。
/// 変更検出にはメソッド IL ボディ変更、アクセス修飾子変更(例: public → internal)、
/// 修飾子変更(例: static/virtual の追加・削除)、プロパティ/フィールドの型変更を含みます。
/// 表形式レンダリング向けの構造化 <see cref="MemberChangeEntry"/> レコードを返します。
/// </summary>
internal static partial class AssemblyMethodAnalyzer
{
/// <summary>
/// Analyses two assembly files and returns a summary of assembly semantic changes.
/// Returns <see langword="null"/> if analysis fails (best-effort).
/// When <paramref name="onError"/> is provided, it is invoked with the caught exception
/// before returning <see langword="null"/>, enabling callers to log diagnostic details.
/// 2 つのアセンブリファイルを解析し、アセンブリセマンティック変更要約を返します。
/// 解析に失敗した場合は <see langword="null"/> を返します(ベストエフォート)。
/// <paramref name="onError"/> が指定されている場合、<see langword="null"/> を返す前に
/// キャッチした例外を渡して呼び出し、呼び出し元で診断詳細をログ可能にします。
/// </summary>
public static AssemblySemanticChangesSummary? Analyze(string oldAssemblyPath, string newAssemblyPath, Action<Exception>? onError = null)
{
try
{
var oldSnapshot = ReadAssemblySnapshot(oldAssemblyPath);
var newSnapshot = ReadAssemblySnapshot(newAssemblyPath);
var entries = new List<MemberChangeEntry>();
CompareTypes(oldSnapshot, newSnapshot, entries);
CompareMethods(oldSnapshot, newSnapshot, entries);
CompareProperties(oldSnapshot, newSnapshot, entries);
CompareFields(oldSnapshot, newSnapshot, entries);
// Annotate compiler-generated members with their user-authored origin.
// コンパイラ生成メンバーにユーザー記述元の注釈を付加。
CompilerGeneratedResolver.Annotate(entries);
// Classify importance for each entry.
// 各エントリの重要度を分類。
for (int i = 0; i < entries.Count; i++)
entries[i] = ChangeImportanceClassifier.WithClassifiedImportance(entries[i]);
// Sort: by TypeName, then by Change order (Added → Removed → Modified)
entries.Sort((a, b) =>
{
int cmp = StringComparer.Ordinal.Compare(a.TypeName, b.TypeName);
if (cmp != 0) return cmp;
return ChangeOrder(a.Change).CompareTo(ChangeOrder(b.Change));
});
return new AssemblySemanticChangesSummary
{
Entries = entries,
};
}
#pragma warning disable CA1031 // ベストエフォート解析のため全例外をキャッチ / Catch-all for best-effort analysis
catch (Exception ex)
{
onError?.Invoke(ex);
return null;
}
#pragma warning restore CA1031
}
/// <summary>Sort order for Change column: Added=0, Removed=1, Modified=2. / Change 列のソート順。</summary>
private static int ChangeOrder(string change)
=> change switch { "Added" => 0, "Removed" => 1, "Modified" => 2, _ => 3 };
/// <summary>
/// Looks up the BaseType string for a given type name from the preferred snapshot,
/// falling back to the other snapshot if the type is not found in the preferred one.
/// 指定した型名の BaseType 文字列を preferred スナップショットから検索し、
/// 見つからない場合は fallback スナップショットから取得します。
/// </summary>
private static string LookupBaseType(string typeName, AssemblySnapshot preferred, AssemblySnapshot fallback)
{
if (preferred.TypeNames.TryGetValue(typeName, out var info)) return info.BaseType;
if (fallback.TypeNames.TryGetValue(typeName, out info)) return info.BaseType;
return "";
}
}
}