简体   繁体   English

在运行时将字符串类型转换为int,float,decimal等类型。

[英]convert String Type to int,float,decimal etc Type on runtime C#

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, 对于@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 在这里,我不需要硬编码<int><string><decimal> ,我想要的是

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

Is there any way?? 有什么办法吗?

You mean certainly return string value parsed to a specific Type. 您的意思是肯定返回解析为特定Type的字符串值。 Then , I propose generics. 然后,我提出了泛型。 Here's how : 这是如何做 :

1)- Without Generics (I Prefer With Generic in 2)-) 1)-没有泛型(我更喜欢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 2)-具有泛型

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 希望它有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM