Skip to content

Commit 3fbf4d9

Browse files
author
Kapil Borle
committed
Move method checking to constructor
1 parent d8cc876 commit 3fbf4d9

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

Rules/UseIdenticalMandatoryParametersDSC.cs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
using System;
1414
using System.Collections.Generic;
15+
using System.Collections.ObjectModel;
1516
#if !CORECLR
1617
using System.ComponentModel.Composition;
1718
#endif
@@ -41,6 +42,41 @@ public class UseIdenticalMandatoryParametersDSC : IDSCResourceRule
4142
private string fileName;
4243
private IDictionary<string, string> propAttrDict;
4344
private IEnumerable<FunctionDefinitionAst> resourceFunctions;
45+
private Func<string, Tuple<string, Version>, Collection<Exception>, List<CimClass>> dscClassImporter;
46+
47+
public UseIdenticalMandatoryParametersDSC()
48+
{
49+
var importClassesMethod = typeof(DscClassCache).GetMethod(
50+
"ImportClasses",
51+
BindingFlags.Public | BindingFlags.Static);
52+
if (importClassesMethod != null)
53+
{
54+
// In some version of S.M.A ImportClasses classes method takes 4 parameters
55+
// while in others it takes 3.
56+
if (importClassesMethod.GetParameters().Count() == 4)
57+
{
58+
dscClassImporter = (path, moduleInfo, errors) =>
59+
{
60+
return importClassesMethod.Invoke(
61+
null,
62+
new object[] { path, moduleInfo, errors, false }) as List<CimClass>;
63+
};
64+
}
65+
else
66+
{
67+
dscClassImporter = (path, moduleInfo, errors) =>
68+
{
69+
return importClassesMethod.Invoke(
70+
null,
71+
new object[] { path, moduleInfo, errors}) as List<CimClass>;
72+
};
73+
}
74+
}
75+
else
76+
{
77+
dscClassImporter = (path, moduleInfo, errors) => null;
78+
}
79+
}
4480

4581
/// <summary>
4682
/// AnalyzeDSCResource: Analyzes given DSC Resource
@@ -216,21 +252,7 @@ private IDictionary<string, string> GetKeys(string fileName)
216252
isDSCClassCacheInitialized = true;
217253
}
218254

219-
var importClassesMethod = typeof(DscClassCache).GetMethod(
220-
"ImportClasses",
221-
BindingFlags.Public | BindingFlags.Static);
222-
if (importClassesMethod != null)
223-
{
224-
var parameters = new List<object>(new object[] { mofFilepath, moduleInfo, errors });
225-
226-
// In some version of S.M.A ImportClasses classes method takes 4 parameters
227-
// while in others it takes 3.
228-
if (importClassesMethod.GetParameters().Count() == 4)
229-
{
230-
parameters.Add(false);
231-
}
232-
cimClasses = importClassesMethod.Invoke(null, parameters.ToArray()) as List<CimClass>;
233-
}
255+
cimClasses = dscClassImporter(mofFilepath, moduleInfo, errors);
234256
}
235257
catch
236258
{

0 commit comments

Comments
 (0)