@@ -669,28 +669,57 @@ private List<string> GetValidCustomRulePaths(string[] customizedRulePath, PathIn
669669 }
670670
671671
672- private IEnumerable < IScriptRule > GetRulesFromDLL ( )
672+ private IEnumerable < T > GetRulesFromDLL < T > ( ) where T : class , IRule
673673 {
674674 string dirName = Path . GetDirectoryName ( typeof ( ScriptAnalyzer ) . GetTypeInfo ( ) . Assembly . Location ) ;
675675 var dllPaths = Directory . EnumerateFiles ( dirName , "*.dll" , SearchOption . TopDirectoryOnly ) ;
676- var rules = new List < IScriptRule > ( ) ;
676+ var rules = new List < T > ( ) ;
677677 foreach ( var dllPath in dllPaths )
678678 {
679- var rulesFromOneFile = GetRulesFromDLL ( dllPath ) ;
679+ outputWriter . WriteVerbose ( string . Format ( "Found Assembly: {0}" , dllPath ) ) ;
680+ var rulesFromOneFile = GetRulesFromDLL < T > ( dllPath ) ;
680681 rules . AddRange ( rulesFromOneFile ) ;
681682 }
682683 return rules ;
683684 }
684685
685- private IEnumerable < IScriptRule > GetRulesFromDLL ( string ruleDllPath )
686+ private IEnumerable < T > GetRulesFromDLL < T > ( string ruleDllPath ) where T : class , IRule
686687 {
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+ }
689699 foreach ( var type in dll . ExportedTypes )
690700 {
691- if ( type == typeof ( IScriptRule ) )
701+ var typeInfo = type . GetTypeInfo ( ) ;
702+ if ( ! typeInfo . IsInterface
703+ && ! typeInfo . IsAbstract
704+ && typeInfo . ImplementedInterfaces . Contains ( typeof ( T ) ) )
692705 {
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+ }
694723 rules . Add ( rule ) ;
695724 }
696725 }
@@ -711,7 +740,9 @@ private void LoadRules(Dictionary<string, List<string>> result, CommandInvocatio
711740 this . ExternalRules = null ;
712741
713742#if CORECLR
714- this . ScriptRules = GetRulesFromDLL ( ) ;
743+ this . ScriptRules = GetRulesFromDLL < IScriptRule > ( ) ;
744+ this . TokenRules = GetRulesFromDLL < ITokenRule > ( ) ;
745+ this . DSCResourceRules = GetRulesFromDLL < IDSCResourceRule > ( ) ;
715746#else
716747 // An aggregate catalog that combines multiple catalogs.
717748 using ( AggregateCatalog catalog = new AggregateCatalog ( ) )
0 commit comments