Search code examples
c#stringreflectionstartswithmethodinfo

Get MethodInfo of String.TrimStart with reflection?


I gonna get MethodInfo of String.TrimStart()
The following code returns null.

typeof(string).GetMethod("TrimStart", new Type[ ] {});

and the following code returns {System.String TrimStart(Char[])}

typeof(string).GetMethod("TrimStart", BindingFlags.Public | BindingFlags.Instance);

I wanna get {System.String TrimStart()} exactly ?


Solution

  • There is no String.TrimStart() method.

    There is only String.TrimStart(params Char[] source) overload. You can invoke it without any paramteres thanks to params keyword.

    In other words: String.TrimStart() "invokes" String.TrimStart(new char[0]).

    More: params keyword on msdn