Get Try Parse Method Info - CSharp Reflection

CSharp examples for Reflection:Method

Description

Get Try Parse Method Info

Demo Code


using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Text;
using System.Reflection;
using System.Linq;
using System.ComponentModel;
using System.Collections.Generic;
using System;/*from  w  ww.  j a va 2 s .  c o m*/

public class Main{
        static MethodInfo GetTryParseMethodInfo(Type numericType)
        {
            MethodInfo method = null;
            if (numericType == typeof(int) || numericType == typeof(byte) || numericType == typeof(float)
                || numericType == typeof(double) || numericType == typeof(decimal))
            {
                var methodInfos = numericType.GetMember("TryParse", MemberTypes.Method, BindingFlags.Public | BindingFlags.Static);
                if (methodInfos.Length > 0)
                {
                    foreach (MethodInfo mi in methodInfos)
                    {
                        if (mi.GetParameters().Length == 2)
                        {
                            method = mi;
                            break;
                        }
                    }
                }
            }

            return method;
        }
}

Related Tutorials