Skip to content

Commit 4ba8e3e

Browse files
committed
PR Feedback
1 parent 025ce21 commit 4ba8e3e

File tree

1 file changed

+20
-41
lines changed

1 file changed

+20
-41
lines changed

src/Compatibility/Microsoft.DotNet.Compatibility/ValidatePackage.cs

+20-41
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ namespace Microsoft.DotNet.Compatibility
1717
{
1818
public class ValidatePackage : TaskBase
1919
{
20-
private const string CODE_ANALYSIS_ASSEMBLY_NAME = "Microsoft.CodeAnalysis";
21-
private const string CODE_ANALYSIS_CSHARP_ASSEMBLY_NAME = "Microsoft.CodeAnalysis.CSharp";
22-
2320
[Required]
2421
public string PackageTargetPath { get; set; }
2522

@@ -120,57 +117,39 @@ protected override void ExecuteCore()
120117
#if NETCOREAPP
121118
private Assembly ResolverForRoslyn(AssemblyLoadContext context, AssemblyName assemblyName)
122119
{
123-
if (assemblyName.Name.StartsWith(CODE_ANALYSIS_ASSEMBLY_NAME))
124-
{
125-
(string requested, string extra) = GetRoslynPathsToLoad(assemblyName.Name);
126-
Assembly asm = context.LoadFromAssemblyPath(requested);
127-
ThrowIfVersionIsLower(assemblyName.Version, asm.GetName().Version);
128-
129-
// Being extra defensive but we want to avoid that we accidentally load two different versions of either
130-
// of the roslyn assemblies from a different location, so let's load them both on the first request.
131-
Assembly _ = context.LoadFromAssemblyPath(extra);
132-
133-
return asm;
134-
}
135-
136-
return null;
120+
return LoadRoslyn(assemblyName, path => context.LoadFromAssemblyPath(path));
137121
}
138122
#else
139123
private Assembly ResolverForRoslyn(object sender, ResolveEventArgs args)
140124
{
141125
AssemblyName name = new(args.Name);
142-
if (name.Name.StartsWith(CODE_ANALYSIS_ASSEMBLY_NAME))
126+
return LoadRoslyn(name, path => Assembly.LoadFrom(path));
127+
}
128+
#endif
129+
130+
private Assembly LoadRoslyn(AssemblyName name, Func<string, Assembly> loadFromPath)
131+
{
132+
const string codeAnalysisName = "Microsoft.CodeAnalysis";
133+
const string codeAnalysisCsharpName = "Microsoft.CodeAnalysis.CSharp";
134+
if (name.Name == codeAnalysisName || name.Name == codeAnalysisCsharpName)
143135
{
144-
(string requested, string extra) = GetRoslynPathsToLoad(name.Name);
145-
Assembly asm = Assembly.LoadFrom(requested);
146-
ThrowIfVersionIsLower(name.Version, asm.GetName().Version);
136+
Assembly asm = loadFromPath(Path.Combine(RoslynAssembliesPath, $"{name.Name}.dll"));
137+
Version resolvedVersion = asm.GetName().Version;
138+
if (resolvedVersion < name.Version)
139+
{
140+
throw new Exception(string.Format(Resources.UpdateSdkVersion, resolvedVersion, name.Version));
141+
}
147142

148143
// Being extra defensive but we want to avoid that we accidentally load two different versions of either
149144
// of the roslyn assemblies from a different location, so let's load them both on the first request.
150-
Assembly _ = Assembly.LoadFrom(extra);
145+
Assembly _ = name.Name == codeAnalysisName ?
146+
loadFromPath(Path.Combine(RoslynAssembliesPath, $"{codeAnalysisCsharpName}.dll")) :
147+
loadFromPath(Path.Combine(RoslynAssembliesPath, $"{codeAnalysisName}.dll"));
151148

152149
return asm;
153150
}
154-
return null;
155-
}
156-
#endif
157-
158-
private (string requested, string extra) GetRoslynPathsToLoad(string name)
159-
{
160-
string requested = Path.Combine(RoslynAssembliesPath, $"{name}.dll");
161-
string extra = name == CODE_ANALYSIS_ASSEMBLY_NAME ?
162-
Path.Combine(RoslynAssembliesPath, $"{CODE_ANALYSIS_CSHARP_ASSEMBLY_NAME}.dll") :
163-
Path.Combine(RoslynAssembliesPath, $"{CODE_ANALYSIS_ASSEMBLY_NAME}.dll");
164151

165-
return (requested, extra);
166-
}
167-
168-
private static void ThrowIfVersionIsLower(Version expected, Version actual)
169-
{
170-
if (actual < expected)
171-
{
172-
throw new Exception(string.Format(Resources.UpdateSdkVersion, actual, expected));
173-
}
152+
return null;
174153
}
175154
}
176155
}

0 commit comments

Comments
 (0)