12
12
13
13
using System ;
14
14
using System . Collections . Generic ;
15
+ using System . Collections . ObjectModel ;
15
16
#if ! CORECLR
16
17
using System . ComponentModel . Composition ;
17
18
#endif
18
19
using System . Globalization ;
19
20
using System . IO ;
20
21
using System . Linq ;
21
22
using System . Management . Automation . Language ;
23
+ using System . Reflection ;
22
24
using Microsoft . Management . Infrastructure ;
23
25
using Microsoft . PowerShell . DesiredStateConfiguration . Internal ;
24
26
using Microsoft . Windows . PowerShell . ScriptAnalyzer . Extensions ;
@@ -40,6 +42,44 @@ public class UseIdenticalMandatoryParametersDSC : IDSCResourceRule
40
42
private string fileName ;
41
43
private IDictionary < string , string > propAttrDict ;
42
44
private IEnumerable < FunctionDefinitionAst > resourceFunctions ;
45
+ private Func < string , Tuple < string , Version > , Collection < Exception > , List < CimClass > > dscClassImporter ;
46
+
47
+ /// <summary>
48
+ /// Constructs an object of type UseIdenticalMandatoryParametersDSC
49
+ /// </summary>
50
+ public UseIdenticalMandatoryParametersDSC ( )
51
+ {
52
+ var importClassesMethod = typeof ( DscClassCache ) . GetMethod (
53
+ "ImportClasses" ,
54
+ BindingFlags . Public | BindingFlags . Static ) ;
55
+ if ( importClassesMethod != null )
56
+ {
57
+ // In some version of S.M.A DscClassCache.ImportClasses method takes 4 parameters
58
+ // while in others it takes 3.
59
+ if ( importClassesMethod . GetParameters ( ) . Count ( ) == 4 )
60
+ {
61
+ dscClassImporter = ( path , moduleInfo , errors ) =>
62
+ {
63
+ return importClassesMethod . Invoke (
64
+ null ,
65
+ new object [ ] { path , moduleInfo , errors , false } ) as List < CimClass > ;
66
+ } ;
67
+ }
68
+ else
69
+ {
70
+ dscClassImporter = ( path , moduleInfo , errors ) =>
71
+ {
72
+ return importClassesMethod . Invoke (
73
+ null ,
74
+ new object [ ] { path , moduleInfo , errors } ) as List < CimClass > ;
75
+ } ;
76
+ }
77
+ }
78
+ else
79
+ {
80
+ dscClassImporter = ( path , moduleInfo , errors ) => null ;
81
+ }
82
+ }
43
83
44
84
/// <summary>
45
85
/// AnalyzeDSCResource: Analyzes given DSC Resource
@@ -215,7 +255,7 @@ private IDictionary<string, string> GetKeys(string fileName)
215
255
isDSCClassCacheInitialized = true ;
216
256
}
217
257
218
- cimClasses = DscClassCache . ImportClasses ( mofFilepath , moduleInfo , errors ) ;
258
+ cimClasses = dscClassImporter ( mofFilepath , moduleInfo , errors ) ;
219
259
}
220
260
catch
221
261
{
0 commit comments