逗号分隔字符串到通用列表 [英] Comma separated string to generic list

查看:121
本文介绍了逗号分隔字符串到通用列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够将逗号分隔字符串转换为 IList< int> 但是如何修改相同的$ IList< T& / code>其中T将作为输入参数之一传递?

I was able to convert comma separated string to an IList<int> but how can I modify the same to get IList<T> where T will be passed as one of the input parameter?

即如果我需要 IList< int> 我将传递int作为参数,如果我需要 IList< string> 我将传递string作为参数。

i.e if I need IList<int> I will pass "int" as parameter, if I need IList<string> I will pass "string" as parameter.

我的想法是通过输入参数获取类型是否为int或string,并使用反射并将字符串转换为相应的列表

My idea is to get the type whether it is int or string through input parameter and use reflection and convert the string to respective list

将逗号分隔字符串转换为 IList< int> 的代码

public static IList<int> SplitStringUsing(this string source, string seperator =",")
{
     return source.Split(Convert.ToChar(seperator))
                  .Select(x => x.Trim())
                  .Where(x => !string.IsNullOrWhiteSpace(x))
                  .Select(int.Parse).ToList();
}

注意:

我正在寻找像

public static IList<T> SplitStringUsing(this string source, string seperator =",", T t)
{
find the type of t and convert it to respective List
}


推荐答案

您可以使用 Convert.ChangeType(object,string),用于解析 System.Convert 类或任何其他实现 IConvertible 界面

You can use Convert.ChangeType(object,string) for parsing to the base types supported by the System.Convert class, or any other class that implements the IConvertible interface

public static IList<T> SplitStringUsing<T>(string source, string seperator = ",")
where T:IConvertible
{
        return source.Split(Convert.ToChar(seperator))
                     .Where(x => !string.IsNullOrWhiteSpace(x))
                     .Select(x=>Convert.ChangeType(x,typeof(T)))
                     .Cast<T>()
                     .ToList();
}


$ b $ p

为避免本地化问题,您应该添加一个IFormatProvider参数,允许调用者指定要使用的文化或默认当前文化,例如:

To avoid localization issues, you should probably add an IFormatProvider parameter as well, to allow the caller to specify the culture to use or default to the current culture, eg:

public static IList<T> SplitStringUsing<T>(string source, 
    string seperator = ",",
    IFormatProvider provider =null)
    where T:IConvertible
{
    return source.Split(Convert.ToChar(seperator))
                    .Where(x => !string.IsNullOrWhiteSpace(x))
                    .Select(x=>Convert.ChangeType(x,typeof(T),provider))
                    .Cast<T>().ToList();
}

对于更通用的情况,您可以将解析代码作为lambda传递函数:

For a more generic case, you can pass the parsing code as a lambda to the function:

    public static IList<T> SplitStringUsing<T>(string source, 
        Func<string,T> parser,  
        string seperator = ",")
    {
        return source.Split(Convert.ToChar(seperator))
            .Where(x => !string.IsNullOrWhiteSpace(x))
            .Select(parser)
            .ToList();
    }

并像这样调用:

var l1 = SplitStringUsing(x,s=>double.Parse(s,NumberStyles.HexNumber,
                                              CultureInfo.InvariantCulture));

您可以在代码中使用这两种方法,编译器会选择正确的重载。

You can have both methods in your code and the compiler will pick the correct overload.

这篇关于逗号分隔字符串到通用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆