0

I have made a program in which i wanted to convert all values depending on parameters type which is get by run time methods, what i want is convert all values which are enter by user in textbox in define type of parameter. what i wanted is

private object convertType(Type type, string value)
    {
        Type t = typeof(int);
        //suppose value have stringvalue=33;
        return 33; //of type int
    }

is there any way to get any object type ?

Updated Answer

for @Atmane EL BOUACHRI,

class Program
{
 static void Main()
 {

   var ints =  ConvertType<int>("33");
   var bools = ConvertType<bool>("false");
   var decimals = ConvertType<decimal>("1.33m"); // exception here
   Console.WriteLine(ints);
   Console.WriteLine(bools);
    Console.WriteLine(decimals);

    Console.ReadLine();
 }

 public static T ConvertType<T>(string input)
 {
    T result =  default(T);
    var converter = TypeDescriptor.GetConverter(typeof(T));
    if (converter != null)
    {
        try
        {
            result =  (T)converter.ConvertFromString(input);
        }
        catch 
        {
            // add you exception handling
        }
    }
    return result;
 }
}

Here I don't want hard code <int>, <string> or <decimal>, what I want is

private object convertToAnyType(Type type, string value)
{
    //Type t = typeof(int);
    return ConvertType<type>("33");
}

Is there any way??

3
  • 1
    You'd likely have to use generics. Commented Mar 7, 2016 at 12:56
  • I have made dynamic web service call in which any web service is consume by my project, but i was stuck on parameters of different type, how can i handle all parameters of different methods? Commented Mar 7, 2016 at 12:59
  • 1
    Convert.ChangeType will work for a limited range of built-in types Commented Mar 7, 2016 at 12:59

1 Answer 1

1

You mean certainly return string value parsed to a specific Type. Then , I propose generics. Here's how :

1)- Without Generics (I Prefer With Generic in 2)-)

 class Program
    {
        static void Main()
        {

            var ints = (int)ConvertType(typeof(int),"33"); 
            var bools = (bool)ConvertType(typeof(bool), "true");   

            Console.WriteLine(bools);
            Console.WriteLine(ints);

            Console.ReadLine();
        }

        public static object ConvertType(Type type, string input)
        {
            object result = default(object);
            var converter = TypeDescriptor.GetConverter(type);
            if (converter != null)
            {
                try
                {
                    result = converter.ConvertFromString(input);
                }
                catch
                {
                    // add you exception handling
                }
            }
            return  result;
        }
    }

2)-With Generic

class Program
{
    static void Main()
    {

       var ints =  ConvertType<int>("33");
       var bools = ConvertType<bool>("false");
       var decimals = ConvertType<decimal>("1.33m"); // exception here
       Console.WriteLine(ints);
       Console.WriteLine(bools);
        Console.WriteLine(decimals);

        Console.ReadLine();
    }

    public static T ConvertType<T>(string input)
    {
        T result =  default(T);
        var converter = TypeDescriptor.GetConverter(typeof(T));
        if (converter != null)
        {
            try
            {
                result =  (T)converter.ConvertFromString(input);
            }
            catch 
            {
                // add you exception handling
            }
        }
        return result;
    }
}

Hpe it helps

Sign up to request clarification or add additional context in comments.

7 Comments

In my case this is not working int t = 0; int ints = oo.ConvertType<t.GetType()>("33");
@user6002727 I dont understand
I don't know which type is come from other end, so how can i call this function as "convertType<g.GetType>" , where g is any variable like int, float etc
@user6002727 give me an concret exemple.. You should have a type anyway. a String or Object.
Updated my post, please review
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.