@@ -17,9 +17,6 @@ namespace Microsoft.DotNet.Compatibility
17
17
{
18
18
public class ValidatePackage : TaskBase
19
19
{
20
- private const string CODE_ANALYSIS_ASSEMBLY_NAME = "Microsoft.CodeAnalysis" ;
21
- private const string CODE_ANALYSIS_CSHARP_ASSEMBLY_NAME = "Microsoft.CodeAnalysis.CSharp" ;
22
-
23
20
[ Required ]
24
21
public string PackageTargetPath { get ; set ; }
25
22
@@ -120,57 +117,39 @@ protected override void ExecuteCore()
120
117
#if NETCOREAPP
121
118
private Assembly ResolverForRoslyn ( AssemblyLoadContext context , AssemblyName assemblyName )
122
119
{
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 ) ) ;
137
121
}
138
122
#else
139
123
private Assembly ResolverForRoslyn ( object sender , ResolveEventArgs args )
140
124
{
141
125
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 )
143
135
{
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
+ }
147
142
148
143
// Being extra defensive but we want to avoid that we accidentally load two different versions of either
149
144
// 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") ) ;
151
148
152
149
return asm ;
153
150
}
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") ;
164
151
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 ;
174
153
}
175
154
}
176
155
}
0 commit comments