Skip to content

Latest commit

 

History

History
3774 lines (2683 loc) · 32.7 KB

about_Type_Signatures.help.md

File metadata and controls

3774 lines (2683 loc) · 32.7 KB

Type Signatures

Type signatures are a custom query language built into PowerShell type expressions to enable complex searches of the environment. Originally built to more easily search for generic types, but allows for very precise exploration of currently loaded assemblies.

Keywords

assignable

(Back to Top)

By default all type expressions are implicitly interpreted as assignable. Meaning if you enter [IO.FileSystemInfo] then it will also match [IO.FileInfo] and [IO.DirectoryInfo].

Find-Member -ParameterType { [IO.FileSystemInfo] }
# You can also be explicit about assignable:
Find-Member -ParameterType { [assignable[IO.FileSystemInfo]] }

Signature

✔️

void Example(FileInfo file);

✔️

void Example(FileSystemInfo fso);

void Example(object obj);

exact

(Back to Top)

Sometimes you may want to only match a specific type and not any of it's subclasses or implementees.

Find-Member -ParameterType { [exact[IO.FileSystemInfo]] }

Signature

✔️

void Example(FileSystemInfo fso);

void Example(FileInfo file);

contains

(Back to Top)

Recurses a type's elements and generic arguments for a match.

Find-Member -ParameterType { [contains[int]] }

Signature

✔️

void Example(int value);

✔️

void Example(IList<int[]> values);

void Example(IList<long> values);
Find-Member -ParameterType { [contains[exact[IO.FileSystemInfo]]] }

Signature

✔️

void Example(IList<FileSystemInfo> fsos);

void Example(IList<FileInfo> files);

any

(Back to Top)

Matches anything. It's main purpose is as a stand in for generic arguments e.g. [Span[any]] to match any type of Span<>.

Find-Member -ParameterType { [Span[any]] }

Signature

✔️

void Example(Span<int> values);

✔️

void Example(Span<DateTime> dates);

void Example(ReadOnlySpan<int> values);
Find-Member -ParameterType { [any] }

Signature

✔️

all the things

ref

(Back to Top)

An argument passed by ref, excludes out and in.

Find-Member -ParameterType { [ref] [any] }
Find-Member -ParameterType { [ref[any]] }

Signature

✔️

void Example(ref int value);

✔️

void Example(ref string value);

void Example(out long value);
Find-Member -ParameterType { [ref] [DateTime] }

Signature

✔️

void Example(ref DateTime date);

void Example(in DateTime date);

void Example(out int value);

out

(Back to Top)

An argument passed by out, excludes standard ref and in.

Find-Member -ParameterType { [out] [any] }
Find-Member -ParameterType { [out[any]] }

Signature

✔️

void Example(out int value);

✔️

void Example(out string value);

void Example(ref long value);
Find-Member -ParameterType { [out] [DateTime] }

Signature

✔️

void Example(out DateTime date);

void Example(in DateTime date);

void Example(ref int value);

in

(Back to Top)

An argument passed by in, excludes standard ref and out.

Find-Member -ParameterType { [in] [any] }
Find-Member -ParameterType { [in[any]] }

Signature

✔️

void Example(out int value);

✔️

void Example(out string value);

void Example(ref long value);
Find-Member -ReturnType { [in] [any] }

Signature

✔️

ref readonly int Example();

✔️

ref readonly string Example();

ref int Example();

anyof

(Back to Top)

Return true if any of it's arguments match.

Find-Member -ParameterType { [anyof[int, double]] }

Signature

✔️

void Example(int value);

✔️

void Example(double value);

void Example(long value);
Find-Member -ParameterType { [anyof[bool, contains[int]]] }

Signature

✔️

void Example(bool value);

✔️

void Example(IList<int> values);

void Example(long value);

allof

(Back to Top)

Return true if all of it's arguments match.

Find-Member -ParameterType { [allof[primitive, [not[anyof[bool, char]]]]] }

Signature

✔️

void Example(int value);

✔️

void Example(long value);

void Example(bool value);

void Example(object obj);

not

(Back to Top)

Returns true if it's argument does not match.

Find-Member -ParameterType { [not[int]] }

Signature

✔️

void Example(bool value);

void Example(int value);
Find-Member -ParameterType { [not[contains[int]]] }

Signature

✔️

void Example(IList<bool> values);

void Example(IList<int> values);

void Example(int value);

class

(Back to Top)

Only match concrete classes (not an interface or ValueType).

Find-Member -ParameterType { [class] }

Signature

✔️

void Example(object value);

void Example(int value);
Find-Member -ParameterType { [Collections.Generic.List[class]] }

Signature

✔️

void Example(List<string> values);

void Example(List<int> values);

void Example(List<IDisposable> values);

struct

(Back to Top)

Only match ValueType types that are not exactly ValueType or Enum.

Find-Member -ParameterType { [struct] }

Signature

✔️

void Example(int value);

✔️

void Example(DateTime date);

void Example(object obj);

void Example(Enum value);

void Example(ValueType value);
Find-Member -ParameterType { [Collections.Generic.List[class]] }

Signature

✔️

void Example(List<string> values);

void Example(List<int> values);

void Example(List<DateTime> values);

record

(Back to Top)

Only match types defined with the record keyword in C#.

Find-Member -ParameterType { [record] }

Signature

✔️

public record Person(string Name);

void Example(Person person);

void Example(object obj);

readonlystruct

(Back to Top)

Only match structs defined with the readonly keyword in C#.

Find-Member -ParameterType { [readonlystruct] }

Signature

✔️

public readonly struct Person
{
   public readonly string Name;
}

void Example(Person person);

void Example(object obj);

readonlyrefstruct

(Back to Top)

Only match structs defined with the readonly and ref keywords in C#.

Find-Member -ParameterType { [readonlyrefstruct] }

Signature

✔️

public readonly ref struct Person
{
   public readonly ReadOnlySpan<char> Name;
}

void Example(Person person);

void Example(object obj);

refstruct

(Back to Top)

Only match structs defined with the ref keyword in C#.

Find-Member -ParameterType { [refstruct] }

Signature

✔️

public ref struct Person
{
   public ReadOnlySpan<char> Name;
}

void Example(Person person);

void Example(object obj);

enum

(Back to Top)

Only concrete Enum types.

Find-Member -ParameterType { [enum] }

Signature

✔️

void Example(BindingFlags flags);

void Example(Enum value);

void Example(object value);

referencetype

(Back to Top)

Any reference type including classes, interfaces, and boxed value types.

Find-Member -ParameterType { [referencetype] }

Signature

✔️

void Example(object value);

✔️

void Example(Enum value);

void Example(int value);

Pointers

(Back to Top)

References raw pointer types replacing C#'s * symbol with +.

Find-Member -ParameterType { [void++] }

Signature

✔️

void Example(void** ptr);

void Example(void* ptr);

void Example(int* ptr);

void Example(object value);
Find-Member -ReturnType { [pointer[any, a1..]] }

Signature

✔️

void** Example();

✔️

int* Example();

void Example();

object Example();
Find-Member -ReturnType { [pointer[any, a2..3]] }

Signature

✔️

void** Example();

int* Example();

✔️

void*** Example();

void**** Example();

Generic Parameters (T, TT, and TM)

(Back to Top)

References a generic parameter. T matches any kind, TT matches generic type parameters and TM matches generic method parameters. Optionally add a number to indicate generic parameter position (e.g. T0). Add generic arguments to indicate required generic constraints (e.g. [T[unmanaged]]).

Find-Member -ParameterType { [T] }

Signature

✔️

void Example(T value);
Find-Member -ParameterType { [TM] }

Signature

✔️

void Example<TM>(TM value);

void Example(T value);
Find-Member -ParameterType { [TM0] }

Signature

✔️

void Example<TM>(TM value);

TM0 Example<TM0, TM1>(TM0 value);
Find-Member -ParameterType { [T[unmanaged]] }

Signature

✔️

public class MyClass<T> where T : unmanaged
{ }

void Example(T value);
Find-Member -ParameterType { [T[new]] }

Signature

✔️

public class MyClass<T> where T : new()
{ }

void Example(T value);
Find-Member -ParameterType { [T[Collections.IList]] }

Signature

✔️

public class MyClass<T> where T : IList
{ }

void Example(T value);
Find-Member -ParameterType { [T[struct]] }

Signature

✔️

public class MyClass<T> where T : struct
{ }

void Example(T value);
Find-Member -ParameterType { [T[class]] }

Signature

✔️

public class MyClass<T> where T : class
{ }

void Example(T value);

primitive

(Back to Top)

Matches bool, byte, char, double, short, int, long, IntPtr, sbyte, float, ushort, uint, ulong, or UIntPtr.

Find-Member -ParameterType { [primitive] }

Signature

✔️

void Example(char value);

✔️

void Example(float value);

void Example(string value);

interface

(Back to Top)

Matches only interfaces, does not match concrete types.

Find-Member -ParameterType { [interface] }

Signature

✔️

void Example(IDisposable disposable);

void Example(object obj);

void Example(int value);

abstract

(Back to Top)

Matches only abstract types.

Find-Member -ParameterType { [abstract] }

Signature

void Example(IDisposable disposable);

void Example(object obj);

void Example(int value);

void Example(T value);

✔️

void Example(FileSystemInfo value);

✔️

void Example(FileInfo value);

concrete

(Back to Top)

Matches only concrete types. No abstract classes, interfaces, or generic parameters.

Find-Member -ParameterType { [abstract] }

Signature

void Example(IDisposable disposable);

✔️

void Example(object obj);

✔️

void Example(int value);

void Example(T value);

void Example(FileSystemInfo value);

✔️

void Example(FileInfo value);

index

(Back to Top)

Matches only parameters in at a specific index in the method.

Find-Member -ParameterType { [allof[index0, string]] }

Signature

void Example(IDisposable disposable);

✔️

void Example(string str);

void Example(int value, string str);
Find-Member -ParameterType { [allof[index1.., string]] }

Signature

void Example(IDisposable disposable);

void Example(string str);

✔️

void Example(int value, string str);

✔️

void Example(int value, object obj, string str);

✔️

void Example(string value, object obj, string str);
# You can also use `i`
Find-Member -ParameterType { [allof[i0..1, string]] }

Signature

void Example(IDisposable disposable);

✔️

void Example(string str);

✔️

void Example(int value, string str);

void Example(int value, object obj, string str);

✔️

void Example(string value, object obj, string str);

number

(Back to Top)

Matches a hard coded list of types representing numbers.

Find-Type -Signature { [number] }

Signature

✔️

public readonly struct SByte

✔️

public readonly struct Byte

✔️

public readonly struct Int16

✔️

public readonly struct UInt16

✔️

public readonly struct Int32

✔️

public readonly struct UInt32

✔️

public readonly struct Int64

✔️

public readonly struct UInt64

✔️

public readonly struct Single

✔️

public readonly struct Double

✔️

public readonly struct Half

✔️

public readonly struct IntPtr

✔️

public readonly struct UIntPtr

✔️

public readonly struct BigInteger

public class Object

public readonly struct DateTime

hasdefault

(Back to Top)

Matches only parameters with a default value.

Find-Member -ParameterType { [hasdefault] }

Signature

void Example(string str);

✔️

void Example(string str = "something");

✔️

void Example(CancellationToken token = default);

decoration, hasattr

(Back to Top)

Matches parameters or types decorated with this attribute.

Find-Member -ParameterType { [hasattr[ParamArrayAttribute]] }

Signature

✔️

void Example(params object[] args);

void Example(object[] args);
Find-Member -ParameterType {
   [hasattr[Management.Automation.CmdletAttribute]]
}

Signature

✔️

void Example(OutStringCommand command);

generic

(Back to Top)

Provides a way to specify a signature that takes arguments for a generic type definition.

Find-Member -ParameterType {
   [generic[exact[Collections.Generic.IList`1], args[struct]]]
}

Signature

✔️

void Example(IList<DateTime> values);

void Example(List<DateTime> values);

void Example(IList<object> values);

Resolution Maps

(Back to Top)

You can provide a hashtable of name to Signature/Type to the -ResolutionMap parameter to create your own keywords or override type resolution.

$map = @{
   number = {
       [anyof[bigint, [allof[primitive, [not[anyof[bool, char]]]]]]]
   }

   anymemory = {
       [anyof[Span[any], ReadOnlySpan[any], Memory[any], ReadOnlyMemory[any]]]
   }

   LocalRunspace = (Find-Type LocalRunspace -Force)
}

Find-Type -Force -ResolutionMap $map -Signature {
   [anyof[number, anymemory, LocalRunspace]]
}

Signature

✔️

public struct Byte { }

✔️

public struct Double { }

✔️

public struct Int16 { }

✔️

public struct Int32 { }

✔️

public struct Int64 { }

✔️

public struct IntPtr { }

✔️

public struct Memory<T> { }

✔️

public struct ReadOnlyMemory<T> { }

✔️

public struct ReadOnlySpan<T> { }

✔️

public struct SByte { }

✔️

public struct Single { }

✔️

public struct Span<T> { }

✔️

public struct UInt16 { }

✔️

public struct UInt32 { }

✔️

public struct UInt64 { }

✔️

public struct UIntPtr { }

✔️

private class LocalRunspace { }

✔️

public struct BigInteger { }