日期:2014-05-17  浏览次数:20811 次

关于反射获取带out参数的静态方法问题
MethodInfo m = typeof(int).GetMethod("TryParse", new Type[] { typeof(string), typeof(int) });
bool r = m == null;

想要通过反射获取int的TryParse(string s, out value)这个静态方法,为什么执行后m为null?应该怎么写才对?

------解决方案--------------------


        private static MethodInfo GetTryParseMethod<T>()
        {
            var type = typeof(T);

            return _tryMethodCache.GetOrAdd(type, t =>
            {
                var method = t.GetMethod("TryParse",
                    BindingFlags.Public 
------解决方案--------------------
 BindingFlags.Static,
                    null,
                    new[] { typeof(string), t.MakeByRefType() },
                    null);

                return method;
            });
        }