Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8222907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:26:12+00:00 2026-06-07T14:26:12+00:00

Is there a way to call TryParse dynamically? Some kind of: public static bool

  • 0

Is there a way to call TryParse dynamically? Some kind of:

public static bool TryParse<T>(string toConvert, out T result)

Of course one can use Typeonverters for this. However, an invalid conversion will result in an exception and I want to get rid of this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:26:15+00:00Added an answer on June 7, 2026 at 2:26 pm

    You could call the TryParse method dynamically using Reflection. This way you won’t get a time consuming Exception if the conversion fails.

    This method is a slightly optimized version of this one.

        //Try Parse using Reflection
    public static bool TryConvertValue<T>(string stringValue, out T convertedValue)
    {
        var targetType = typeof(T);
        if (targetType == typeof(string))
        {
            convertedValue = (T)Convert.ChangeType(stringValue, typeof(T));
            return true;
        }
            var nullableType = targetType.IsGenericType &&
                           targetType.GetGenericTypeDefinition() == typeof (Nullable<>);
        if (nullableType)
        {
            if (string.IsNullOrEmpty(stringValue))
            {
                convertedValue = default(T);
                return true;
            }
                targetType = new NullableConverter(targetType).UnderlyingType;
        }
    
        Type[] argTypes = { typeof(string), targetType.MakeByRefType() };
        var tryParseMethodInfo = targetType.GetMethod("TryParse", argTypes);
        if (tryParseMethodInfo == null)
        {
            convertedValue = default(T);
            return false;
        }
    
        object[] args = { stringValue, null };
        var successfulParse = (bool)tryParseMethodInfo.Invoke(null, args);
        if (!successfulParse)
        {
            convertedValue = default(T);
            return false;
        }
    
        convertedValue = (T)args[1];
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there a way to call java me application out of Symbian's C++ app?
Is there a way to call Static Classes / Methods by name? Example: $name
Is there a way to call R functions from C# and retrieve the result
Is there any way to call my website to receive some data about it?
Is there a way to call my private constructor from my public constructor? public
Is there some way to call a function each-time the value of a variable
Is there any way to call an action script function from an anchor which
Is there a way to call send / recv passing in a vector ?
Is there any way to call a php class (eg. $var = new className)
is there any way to call servlet or make a gwt-rpc call from gwt

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.