@@ -669,28 +669,57 @@ private List<string> GetValidCustomRulePaths(string[] customizedRulePath, PathIn
669
669
}
670
670
671
671
672
- private IEnumerable < IScriptRule > GetRulesFromDLL ( )
672
+ private IEnumerable < T > GetRulesFromDLL < T > ( ) where T : class , IRule
673
673
{
674
674
string dirName = Path . GetDirectoryName ( typeof ( ScriptAnalyzer ) . GetTypeInfo ( ) . Assembly . Location ) ;
675
675
var dllPaths = Directory . EnumerateFiles ( dirName , "*.dll" , SearchOption . TopDirectoryOnly ) ;
676
- var rules = new List < IScriptRule > ( ) ;
676
+ var rules = new List < T > ( ) ;
677
677
foreach ( var dllPath in dllPaths )
678
678
{
679
- var rulesFromOneFile = GetRulesFromDLL ( dllPath ) ;
679
+ outputWriter . WriteVerbose ( string . Format ( "Found Assembly: {0}" , dllPath ) ) ;
680
+ var rulesFromOneFile = GetRulesFromDLL < T > ( dllPath ) ;
680
681
rules . AddRange ( rulesFromOneFile ) ;
681
682
}
682
683
return rules ;
683
684
}
684
685
685
- private IEnumerable < IScriptRule > GetRulesFromDLL ( string ruleDllPath )
686
+ private IEnumerable < T > GetRulesFromDLL < T > ( string ruleDllPath ) where T : class , IRule
686
687
{
687
- var dll = Assembly . Load ( new AssemblyName ( Path . GetFileNameWithoutExtension ( ruleDllPath ) ) ) ;
688
- var rules = new List < IScriptRule > ( ) ;
688
+ var fileName = Path . GetFileNameWithoutExtension ( ruleDllPath ) ;
689
+ var assName = new AssemblyName ( fileName ) ;
690
+ outputWriter . WriteVerbose ( string . Format ( "Loading Assembly:{0}" , assName . FullName ) ) ;
691
+
692
+ var dll = Assembly . Load ( assName ) ;
693
+ var rules = new List < T > ( ) ;
694
+ if ( dll == null )
695
+ {
696
+ outputWriter . WriteVerbose ( string . Format ( "Cannot load {0}" , ruleDllPath ) ) ;
697
+ return rules ;
698
+ }
689
699
foreach ( var type in dll . ExportedTypes )
690
700
{
691
- if ( type == typeof ( IScriptRule ) )
701
+ var typeInfo = type . GetTypeInfo ( ) ;
702
+ if ( ! typeInfo . IsInterface
703
+ && ! typeInfo . IsAbstract
704
+ && typeInfo . ImplementedInterfaces . Contains ( typeof ( T ) ) )
692
705
{
693
- IScriptRule rule = Activator . CreateInstance ( type ) as IScriptRule ;
706
+ outputWriter . WriteVerbose (
707
+ string . Format (
708
+ "Creating Instance of {0}" , type . Name ) ) ;
709
+
710
+ var ruleObj = Activator . CreateInstance ( type ) ;
711
+ outputWriter . WriteVerbose (
712
+ string . Format (
713
+ "Created Instance of {0}" , type . Name ) ) ;
714
+
715
+ T rule = ruleObj as T ;
716
+ if ( rule == null )
717
+ {
718
+ outputWriter . WriteVerbose (
719
+ string . Format (
720
+ "Cannot cast instance of type {0} to {1}" , type . Name , typeof ( T ) . GetTypeInfo ( ) . Name ) ) ;
721
+ continue ;
722
+ }
694
723
rules . Add ( rule ) ;
695
724
}
696
725
}
@@ -711,7 +740,9 @@ private void LoadRules(Dictionary<string, List<string>> result, CommandInvocatio
711
740
this . ExternalRules = null ;
712
741
713
742
#if CORECLR
714
- this . ScriptRules = GetRulesFromDLL ( ) ;
743
+ this . ScriptRules = GetRulesFromDLL < IScriptRule > ( ) ;
744
+ this . TokenRules = GetRulesFromDLL < ITokenRule > ( ) ;
745
+ this . DSCResourceRules = GetRulesFromDLL < IDSCResourceRule > ( ) ;
715
746
#else
716
747
// An aggregate catalog that combines multiple catalogs.
717
748
using ( AggregateCatalog catalog = new AggregateCatalog ( ) )
0 commit comments