如何检测对象是否为泛型 List 类型并强制转换为所需类型?

发布于 2024-11-06 20:26:19 字数 767 浏览 1 评论 0原文

我有一个用于将通用列表转换为字符串的扩展方法。

public static string ConvertToString<T>(this IList<T> list)
{
    StringBuilder sb = new StringBuilder();
    foreach (T item in list)
    {
        sb.Append(item.ToString());
    }
    return sb.ToString();
}

我有一个对象类型为包含列表的对象;该列表可以是 ListListList 任何类型的列表。

有没有一种方法可以检测到该对象是通用列表,从而转换为该特定通用列表类型以调用 ConvertToString 方法?

//ignore whats happening here
//just need to know its an object which is actually a list
object o = new List<int>() { 1, 2, 3, 4, 5 };

if (o is of type list)
{
    string value = (cast o to generic type).ConvertToString();
}

I have an extension method for converting a generic list to string.

public static string ConvertToString<T>(this IList<T> list)
{
    StringBuilder sb = new StringBuilder();
    foreach (T item in list)
    {
        sb.Append(item.ToString());
    }
    return sb.ToString();
}

I have an object which is of type object that holds a list; the list could be List<string>, List<int>, List<ComplexType> any type of list.

Is there a way that I can detect that this object is a generic list and therefore cast to that specific generic list type to call the ConvertToString method?

//ignore whats happening here
//just need to know its an object which is actually a list
object o = new List<int>() { 1, 2, 3, 4, 5 };

if (o is of type list)
{
    string value = (cast o to generic type).ConvertToString();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晚风撩人 2024-11-13 20:26:19

可以通过大量反射来实现这一点(既找到正确的T,然后通过MakeGenericMethod等进行调用);但是:您没有使用通用功能,因此请删除它们! (或者有一个辅助非通用 API):

public static string ConvertToString(this IEnumerable list)
{
    StringBuilder sb = new StringBuilder();
    foreach (object item in list)
    {
        sb.Append(item.ToString());
    }
    return sb.ToString();
}

并且

IList list = o as IList;
if (list != null)
{
    string value = list.ConvertToString();
}

(您也可以在上述步骤中使用 IEnumerable,但如果这样做,您需要小心 string 等)

You can achieve that, with lots of reflection (both to find the correct T, and then to invoke via MakeGenericMethod etc); however: you aren't using the generic features, so remove them! (or have a secondary non-generic API):

public static string ConvertToString(this IEnumerable list)
{
    StringBuilder sb = new StringBuilder();
    foreach (object item in list)
    {
        sb.Append(item.ToString());
    }
    return sb.ToString();
}

and

IList list = o as IList;
if (list != null)
{
    string value = list.ConvertToString();
}

(you can also use IEnumerable in the above step, but you need to be careful with string etc if you do that)

回眸一遍 2024-11-13 20:26:19

您可以根据 IEnumerable 对其进行编码,而不是针对 IList编码扩展方法,然后它会是:

public static string ConvertToString(this IEnumerable list)
{
    StringBuilder sb = new StringBuilder();
    foreach (object item in list)
    {
        sb.Append(item.ToString());
    }
    return sb.ToString();
}

然后您可以检查 o 是否是 IEnumerable:

object o = new List<int>() { 1, 2, 3, 4, 5 };

if (o is IEnumerable)
{
    string value = ((IEnumerable) o).ConvertToString();
}

Instead of coding the extension method against IList<T>, you could code it against IEnumerable, then it'd be:

public static string ConvertToString(this IEnumerable list)
{
    StringBuilder sb = new StringBuilder();
    foreach (object item in list)
    {
        sb.Append(item.ToString());
    }
    return sb.ToString();
}

Then you could check if o is an IEnumerable:

object o = new List<int>() { 1, 2, 3, 4, 5 };

if (o is IEnumerable)
{
    string value = ((IEnumerable) o).ConvertToString();
}
棒棒糖 2024-11-13 20:26:19

使用 System.Type 查看您的类型是否为 Arrat (IsArray) 以及是否为泛型类型 (IsGenericType)

Use System.Type to see if your type is an arrat (IsArray) and if it's a generic type (IsGenericType)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文