@@ -28,7 +28,7 @@ public class Helper
28
28
private readonly static Version minSupportedPSVersion = new Version ( 3 , 0 ) ;
29
29
private Dictionary < string , Dictionary < string , object > > ruleArguments ;
30
30
private PSVersionTable psVersionTable ;
31
- private Dictionary < string , CommandInfo > commandInfoCache ;
31
+ private Dictionary < CommandLookupKey , CommandInfo > commandInfoCache ;
32
32
33
33
#endregion
34
34
@@ -142,7 +142,7 @@ public void Initialize()
142
142
ruleArguments = new Dictionary < string , Dictionary < string , object > > ( StringComparer . OrdinalIgnoreCase ) ;
143
143
if ( commandInfoCache == null )
144
144
{
145
- commandInfoCache = new Dictionary < string , CommandInfo > ( StringComparer . OrdinalIgnoreCase ) ;
145
+ commandInfoCache = new Dictionary < CommandLookupKey , CommandInfo > ( ) ;
146
146
}
147
147
148
148
IEnumerable < CommandInfo > aliases = this . invokeCommand . GetCommands ( "*" , CommandTypes . Alias , true ) ;
@@ -700,15 +700,16 @@ public CommandInfo GetCommandInfoLegacy(string name, CommandTypes? commandType =
700
700
cmdletName = name ;
701
701
}
702
702
703
+ var key = new CommandLookupKey ( name , commandType ) ;
703
704
lock ( getCommandLock )
704
705
{
705
- if ( commandInfoCache . ContainsKey ( cmdletName ) )
706
+ if ( commandInfoCache . ContainsKey ( key ) )
706
707
{
707
- return commandInfoCache [ cmdletName ] ;
708
+ return commandInfoCache [ key ] ;
708
709
}
709
710
710
711
var commandInfo = GetCommandInfoInternal ( cmdletName , commandType ) ;
711
- commandInfoCache . Add ( cmdletName , commandInfo ) ;
712
+ commandInfoCache . Add ( key , commandInfo ) ;
712
713
return commandInfo ;
713
714
}
714
715
}
@@ -726,15 +727,16 @@ public CommandInfo GetCommandInfo(string name, CommandTypes? commandType = null)
726
727
return null ;
727
728
}
728
729
730
+ var key = new CommandLookupKey ( name , commandType ) ;
729
731
lock ( getCommandLock )
730
732
{
731
- if ( commandInfoCache . ContainsKey ( name ) )
733
+ if ( commandInfoCache . ContainsKey ( key ) )
732
734
{
733
- return commandInfoCache [ name ] ;
735
+ return commandInfoCache [ key ] ;
734
736
}
735
737
736
738
var commandInfo = GetCommandInfoInternal ( name , commandType ) ;
737
-
739
+ commandInfoCache . Add ( key , commandInfo ) ;
738
740
return commandInfo ;
739
741
}
740
742
}
@@ -1910,6 +1912,37 @@ public Version GetPSVersion()
1910
1912
}
1911
1913
1912
1914
#endregion Methods
1915
+
1916
+ private struct CommandLookupKey : IEquatable < CommandLookupKey >
1917
+ {
1918
+ private readonly string Name ;
1919
+
1920
+ private readonly CommandTypes CommandTypes ;
1921
+
1922
+ internal CommandLookupKey ( string name , CommandTypes ? commandTypes )
1923
+ {
1924
+ Name = name ;
1925
+ CommandTypes = commandTypes ?? CommandTypes . All ;
1926
+ }
1927
+
1928
+ public bool Equals ( CommandLookupKey other )
1929
+ {
1930
+ return CommandTypes == other . CommandTypes
1931
+ && Name . Equals ( other . Name , StringComparison . OrdinalIgnoreCase ) ;
1932
+ }
1933
+
1934
+ public override int GetHashCode ( )
1935
+ {
1936
+ // Algorithm from https://stackoverflow.com/questions/1646807/quick-and-simple-hash-code-combinations
1937
+ unchecked
1938
+ {
1939
+ int hash = 17 ;
1940
+ hash = hash * 31 + Name . ToUpperInvariant ( ) . GetHashCode ( ) ;
1941
+ hash = hash * 31 + CommandTypes . GetHashCode ( ) ;
1942
+ return hash ;
1943
+ }
1944
+ }
1945
+ }
1913
1946
}
1914
1947
1915
1948
0 commit comments