0

我尝试使用以下代码获取字符串的 ToLower() 方法。

 var tolowerMethod = typeof(string).GetMethods().Where(m => m.Name == "ToLower").FirstOrDefault();

我正在尝试获取 DateTime 的 ToString() 方法。我使用了下面的代码

var formatMethod = typeof(DateTime).GetMethods().Where(m => m.Name == "ToString").ElementAt(1);

这不是唯一的。我尝试过类似下面的方法,但没有成功。

var formatMethod2 = typeof(DateTime).GetMethods().Where(m => m.Name == "ToString").Where(x=>x.GetParameters().Select(t=>t.ParameterType).Equals(typeof(string))).FirstOrDefault();

有任何想法吗?

谢谢

4

2 回答 2

3

它必须是linq吗?你可能想要这样的东西:

 var x = typeof(DateTime).GetMethod("ToString", new Type[] { typeof(string) });

或者

 var x = typeof(DateTime).GetMethod("ToString", new Type[] { });

或者 ...

于 2012-07-13T10:09:09.717 回答
1

这取决于您想要的 ToString( ) 方法的哪个重载:

var method = typeof( DateTime ).GetMethods( )
                               .Where ( item => item.Name == "ToString" && 
                                                item.GetParameters( ).Count () == 0 );

// this is the DateTime.Now.ToString( ) method without any parameter
于 2012-07-13T10:09:17.300 回答