简体   繁体   English

C#扩展方法重载

[英]C# Extension Method Overload

I have two extension methods: 我有两种扩展方法:

    public static string ToString(this List<object> list, char delimiter)
    {
        return ToString<object>(list, delimiter.ToString());
    }

    public static string ToString(this List<object> list, string delimiter)
    {
        return ToString<object>(list, delimiter);
    }

When I use this: 我用这个时:

    char delimiter = ' ';
    return tokens.ToString(delimiter);

It won't work. 它不会起作用。 The char overload doesn't show up in the code completion list either. char重载也不会出现在代码完成列表中。 Can anybody tell me how to make this work? 任何人都可以告诉我如何使这项工作?

EDIT 编辑

I accidentally forgot to mention that there are in fact 3 extension methods, the third being: 我不小心忘了提到实际上有3种扩展方法,第三种是:

    public static string ToString<T>(this List<T> list, string delimiter)
    {
        if (list.Count > 0)
        {
            string s = list[0].ToString();

            for (int i = 1; i < list.Count; i++)
                s += delimiter + list[i].ToString();

            return s;
        }

        return "";
    }

Add reference to the class in which you have the extension methods: 添加对具有扩展方法的类的引用:

using MyApplicationNamespace.ToStringExtensionClass;

VS / ReSharper doesn't offer to add reference automatically simply because the method is already recognized, just not with that particular signature. VS / ReSharper不提供自动添加引用,因为该方法已被识别,而不是使用该特定签名。

Also, your methods themselves don't compile unless you have a third extension methods with generic parameter. 此外,除非您有第三个带泛型参数的扩展方法,否则您的方法本身不会编译。

The way they work for me (compile and logically): 他们为我工作的方式(编译和逻辑):

public static string ToString(this List<object> list, char delimiter)
{
    return ToString(list, delimiter.ToString());
}

public static string ToString(this List<object> list, string delimiter)
{
    return string.Join(delimiter, list);
}

Usage will then be: 用法将是:

var list = new List<int> { 1, 2, 3, 4, 5 };
var str = list.Cast<object>().ToList().ToString(' ');

If you want to avoid casting and make the methods generic, change them to: 如果您想避免强制转换并使方法通用,请将它们更改为:

public static string ToString<T>(this List<T> list, char delimiter)
{
    return ToString(list, delimiter.ToString());
}

public static string ToString<T>(this List<T> list, string delimiter)
{
    return string.Join(delimiter, list);
}

And then the usage is much cleaner: 然后使用更清洁:

var list = new List<int> { 1, 2, 3, 4, 5 };
var str = list.ToString(' ');

EDIT 编辑

So after your edit I understand your problem better. 因此,在您编辑后,我更了解您的问题。 You should lose the non-generic methods and have generic overload to accept char as well. 您应该丢失非泛型方法并具有泛型重载以接受char。

public static string ToString<T>(this List<T> list, char delimiter)
{
    return ToString(list, delimiter.ToString());
}

public static string ToString<T>(this List<T> list, string delimiter)
{
    ...
}

Also, the logic you are trying to implement can be easily achieved with: 此外,您尝试实现的逻辑可以通过以下方式轻松实现:

string.Join(delimiter, list);

So you can basically delete all of those methods and just use that, unless you really want it as an extension method for lists. 所以你基本上可以删除所有这些方法并使用它,除非你真的希望它作为列表的扩展方法。

I think your problem is that you are specifying the type of object for your generic List and not making it a generic method. 我认为您的问题是您正在为通用List指定对象的类型,而不是将其作为通用方法。

See if it works when you define something like the following: 在定义如下内容时查看它是否有效:

public static string ToString<T>(this List<T> list, char delimiter) 
{ 
    return ToString<T>(list, delimiter.ToString()); 
} 

public static string ToString<T>(this List<T> list, string delimiter) 
{ 
    return String.join(list, delimiter); 
} 

Your original function with the string delimiter was just calling itself so you'll have to change your ToString<T>(this List<T> list, string delimiter) to do something useful here like a String.join 您使用字符串分隔符的原始函数只是调用自身,因此您必须更改ToString<T>(this List<T> list, string delimiter)以执行此处有用的操作,如String.join

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

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