1212
1313using System ;
1414using System . Collections . Generic ;
15+ using System . Collections . ObjectModel ;
1516#if ! CORECLR
1617using System . ComponentModel . Composition ;
1718#endif
1819using System . Globalization ;
1920using System . IO ;
2021using System . Linq ;
2122using System . Management . Automation . Language ;
23+ using System . Reflection ;
2224using Microsoft . Management . Infrastructure ;
2325using Microsoft . PowerShell . DesiredStateConfiguration . Internal ;
2426using Microsoft . Windows . PowerShell . ScriptAnalyzer . Extensions ;
@@ -40,6 +42,44 @@ public class UseIdenticalMandatoryParametersDSC : IDSCResourceRule
4042 private string fileName ;
4143 private IDictionary < string , string > propAttrDict ;
4244 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+ }
4383
4484 /// <summary>
4585 /// AnalyzeDSCResource: Analyzes given DSC Resource
@@ -215,7 +255,7 @@ private IDictionary<string, string> GetKeys(string fileName)
215255 isDSCClassCacheInitialized = true ;
216256 }
217257
218- cimClasses = DscClassCache . ImportClasses ( mofFilepath , moduleInfo , errors ) ;
258+ cimClasses = dscClassImporter ( mofFilepath , moduleInfo , errors ) ;
219259 }
220260 catch
221261 {
0 commit comments