首页 > 解决方案 > 多个类的重载扩展方法

问题描述

在我的项目中,我有一个代表某种文档类型的类。每个类都有自己的属性和方法,尽管它们之间有一些相似之处。

我正在尝试为每个具有相同名称的类(方法重载)实现扩展方法,但我得到一个模棱两可的调用错误

代码进一步解释

文档类型 A 类表示:

static class DocType_A
{
    private static XElement baseDocType_A = XmlFiles[0].Root;

    // Extension Method
    internal static IEnumerable<ViewData> AsViewData(this IEnumerable<XElement> result)
    {
        return
            from doc in result select new ViewData { };
    }


    // Contains methods for querying through the XML file of this [DocType_A] type
    internal static class Query
    {
        internal static IEnumerable<XElement> ByStatusOrAny(byte status = 0)
        {
            return baseDocType_A.Descendants("DocType_A").Select(doc => doc);
        }

        internal static IEnumerable<XElement> Expired()
        {
            return baseDocType_A.Descendants("DocType_A").Where("some conditions").Select(doc => doc);
        }

    }

    // Represents the data needed to be displayed to the user via the DGV
    internal class ViewData
    {
        // [DocType_A] related Property 
    }

    internal class Counter
    {
        // some property
    }

}

文档类型 B 类表示:

static class DocType_B
{
    private static XElement baseDocType_B = XmlFiles[1].Root;

    // Extension Method
    internal static IEnumerable<ViewData> AsViewData(this IEnumerable<XElement> result)
    {
        return
            from doc in result select new ViewData { };
    }


    // Contains methods for querying through the XML file of this [DocType_A] type
    internal static class Query
    {
        internal static IEnumerable<XElement> ByStatusOrAny(byte status = 0)
        {
            return baseDocType_B.Descendants("DocType_B").Select(doc => doc);
        }

        internal static IEnumerable<XElement> Expired()
        {
            return baseDocType_B.Descendants("DocType_B").Where("some conditions").Select(doc => doc);
        }

    }

    // Represents the data needed to be displayed to the user via the DGV
    internal class ViewData
    {
        // [DocType_B] related Property 
    }

    internal class Counter
    {
        // some property
    }
}

用法:

class SomeApplicationClass
{
    void Method()
    {
        IEnumerable<DocType_A.ViewData> query = DocType_A.Query.ByStatusOrAny().AsViewData();
    }

    void SomeOtherMethod()
    {
        IEnumerable<DocType_B.ViewData> query = DocType_B.Query.ByStatusOrAny().AsViewData();
    }
}

但我收到模棱两可的呼叫错误。

是否可以为每个具有相同名称的类创建扩展方法?

错误

标签: c#extension-methodsoverloading

解决方案


您创建的扩展方法不是用于 Doctype_A 或 Doctype_B,而是为 IEnumerable < XElement > 创建的。所以你实际上有两个具有相同签名的扩展方法。

如果你想要一个特定于 A 或 B 的,你会这样做

    internal static IEnumerable<XElement> ByStatusOrAny(this DocType_A doc, byte status = 0)
    {
        return doc.Query.Descendants("DocType_A").Select(doc => doc);
    }

然后像这样称呼它

    DocType_A.ByStatusOrAny().AsViewData();

我知道你希望它被限定,但这不是它的工作方式。无论 [this] 限定符是什么,扩展方法都将被应用到,无论它在哪个类中。如果您将每个扩展方法保留在不同的命名空间中并且只引用命名空间,您可能能够做您想做的事情你想在你想要的特定文件中,但我从来没有尝试过,所以你的里程可能非常。

此外,正如其他人指出的那样,您的示例似乎不适合扩展方法的正常用例。因此,如果这真的是您正在做的事情,您可能想要重新设计一些东西。


推荐阅读