Reputation: 2653
I have tried to get the ToLower() method of string using the below code.
var tolowerMethod = typeof(string).GetMethods().Where(m => m.Name == "ToLower").FirstOrDefault();
I am trying to get the ToString() method of DateTime. I have used the below code
var formatMethod = typeof(DateTime).GetMethods().Where(m => m.Name == "ToString").ElementAt(1);
This is not unique. I have tried something like below but without success.
var formatMethod2 = typeof(DateTime).GetMethods().Where(m => m.Name == "ToString").Where(x=>x.GetParameters().Select(t=>t.ParameterType).Equals(typeof(string))).FirstOrDefault();
any Ideas?
Thanks
Upvotes: 0
Views: 327
Reputation: 2236
It depends on which overload of the ToString( ) method you want:
var method = typeof( DateTime ).GetMethods( )
.Where ( item => item.Name == "ToString" &&
item.GetParameters( ).Count () == 0 );
// this is the DateTime.Now.ToString( ) method without any parameter
Upvotes: 1
Reputation: 16067
Does it have to be linq? You probably want something like :
var x = typeof(DateTime).GetMethod("ToString", new Type[] { typeof(string) });
or
var x = typeof(DateTime).GetMethod("ToString", new Type[] { });
or ...
Upvotes: 3