多个 类 的重载扩展方法
Overload extension method for multiple classes
在我的项目中,我有一个代表特定文档类型的 class。每个 class 都有自己的属性和方法,尽管它们之间有一些相似之处。
我正在尝试为每个具有相同名称的 class 实现一个扩展方法( 方法重载 ),但我收到一个不明确的调用错误
进一步解释的代码
文档类型 A class 表示:
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 class 表示:
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();
}
}
但是我遇到了不明确的调用错误。
是否可以为每个 class 同名的扩展方法?
您将扩展方法视为 class 方法。他们不是。
它们实际上是一堆静态方法的语法糖,因此编译器和 Intellisense 可以帮助您调用它们。
您必须使用需要 IEnumerable<XElement>
类型参数的函数。编译器支持调用哪个 IEnumerable<XElement>
?对于过载检查的所有目的,忽略 "this" 参数。之后你有两个具有相同名称和任何参数类型的函数来区分。
我相信你的问题出在这里:
internal static IEnumerable<ViewData> AsViewData(this IEnumerable<XElement> result)
你在 DocType_B 和 DocType_A 中有一个这样的方法......如果你删除静态,它应该可以工作,但我认为这不是一种方法。
您创建的扩展方法不是针对 Doctype_A 或 Doctype_B,而是针对 IEnumerable 创建的。所以你实际上有两个具有相同签名的扩展方法。
如果你想要一个特定于 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] 限定符的东西都是扩展方法将要应用的对象,而不管它在 class 中。你 可能 能够做你想做的事,如果您将每个扩展方法保存在不同的命名空间中,并且只在您想要的特定文件中引用您想要的命名空间,但我从未尝试过,所以您的里程可能会非常多。
此外,正如其他人所指出的,您的示例似乎不适合扩展方法的正常用例。因此,如果这确实是您正在做的事情,您可能想要重新做一些事情。
在我的项目中,我有一个代表特定文档类型的 class。每个 class 都有自己的属性和方法,尽管它们之间有一些相似之处。
我正在尝试为每个具有相同名称的 class 实现一个扩展方法( 方法重载 ),但我收到一个不明确的调用错误
进一步解释的代码
文档类型 A class 表示:
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 class 表示:
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();
}
}
但是我遇到了不明确的调用错误。
是否可以为每个 class 同名的扩展方法?
您将扩展方法视为 class 方法。他们不是。
它们实际上是一堆静态方法的语法糖,因此编译器和 Intellisense 可以帮助您调用它们。
您必须使用需要 IEnumerable<XElement>
类型参数的函数。编译器支持调用哪个 IEnumerable<XElement>
?对于过载检查的所有目的,忽略 "this" 参数。之后你有两个具有相同名称和任何参数类型的函数来区分。
我相信你的问题出在这里:
internal static IEnumerable<ViewData> AsViewData(this IEnumerable<XElement> result)
你在 DocType_B 和 DocType_A 中有一个这样的方法......如果你删除静态,它应该可以工作,但我认为这不是一种方法。
您创建的扩展方法不是针对 Doctype_A 或 Doctype_B,而是针对 IEnumerable
如果你想要一个特定于 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] 限定符的东西都是扩展方法将要应用的对象,而不管它在 class 中。你 可能 能够做你想做的事,如果您将每个扩展方法保存在不同的命名空间中,并且只在您想要的特定文件中引用您想要的命名空间,但我从未尝试过,所以您的里程可能会非常多。
此外,正如其他人所指出的,您的示例似乎不适合扩展方法的正常用例。因此,如果这确实是您正在做的事情,您可能想要重新做一些事情。