Skip to content

Commit

Permalink
Move method checking to constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kapil Borle committed Jul 7, 2017
1 parent d8cc876 commit 3fbf4d9
Showing 1 changed file with 37 additions and 15 deletions.
52 changes: 37 additions & 15 deletions Rules/UseIdenticalMandatoryParametersDSC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
#if !CORECLR
using System.ComponentModel.Composition;
#endif
Expand Down Expand Up @@ -41,6 +42,41 @@ public class UseIdenticalMandatoryParametersDSC : IDSCResourceRule
private string fileName;
private IDictionary<string, string> propAttrDict;
private IEnumerable<FunctionDefinitionAst> resourceFunctions;
private Func<string, Tuple<string, Version>, Collection<Exception>, List<CimClass>> dscClassImporter;

public UseIdenticalMandatoryParametersDSC()
{
var importClassesMethod = typeof(DscClassCache).GetMethod(
"ImportClasses",
BindingFlags.Public | BindingFlags.Static);
if (importClassesMethod != null)
{
// In some version of S.M.A ImportClasses classes method takes 4 parameters
// while in others it takes 3.
if (importClassesMethod.GetParameters().Count() == 4)
{
dscClassImporter = (path, moduleInfo, errors) =>
{
return importClassesMethod.Invoke(
null,
new object[] { path, moduleInfo, errors, false }) as List<CimClass>;
};
}
else
{
dscClassImporter = (path, moduleInfo, errors) =>
{
return importClassesMethod.Invoke(
null,
new object[] { path, moduleInfo, errors}) as List<CimClass>;
};
}
}
else
{
dscClassImporter = (path, moduleInfo, errors) => null;
}
}

/// <summary>
/// AnalyzeDSCResource: Analyzes given DSC Resource
Expand Down Expand Up @@ -216,21 +252,7 @@ private bool IsNamedAttributeArgumentMandatory(NamedAttributeArgumentAst namedAt
isDSCClassCacheInitialized = true;
}

var importClassesMethod = typeof(DscClassCache).GetMethod(
"ImportClasses",
BindingFlags.Public | BindingFlags.Static);
if (importClassesMethod != null)
{
var parameters = new List<object>(new object[] { mofFilepath, moduleInfo, errors });

// In some version of S.M.A ImportClasses classes method takes 4 parameters
// while in others it takes 3.
if (importClassesMethod.GetParameters().Count() == 4)
{
parameters.Add(false);
}
cimClasses = importClassesMethod.Invoke(null, parameters.ToArray()) as List<CimClass>;
}
cimClasses = dscClassImporter(mofFilepath, moduleInfo, errors);
}
catch
{
Expand Down

0 comments on commit 3fbf4d9

Please sign in to comment.