EF泛型方法重载

发布于 2024-10-30 06:30:36 字数 1099 浏览 0 评论 0原文

我想让通用方法重载成为可能。 由于我需要在不知道其中包含的泛型类型的情况下创建一个 ObjectSet<..> ,所以我会构建如下内容:

public IQueryable<T> MyMethod<T>() where T : class, (IMyFirst || IMySecond) //notice the syntax..!
{
  if(typeOf(T) is IMyFirst..
  else ...
}

我怎样才能达到我的目的..?

更新:

@BrokenGlass 写道:

这种类型的约束在 C# 中是不可能的 - 但是您可以约束 IFoo 并让 IMyFirst 和 IMySecond 都实现 IFoo。

但该建议不适用,请参阅:

interface1 { property1 {..}}
interface2 { property2 {..}}
interfaceIFoo : interface1, interface2 { }

通过任何方法:

MyWrapper.Retrieve<EntityProduct>(myObjContext); //error-> EntityProduct implements interface1 only!!

通过其他任何方法:

MyWrapper.Retrieve<EntityOrder>(myObjContext); //error-> EntityOrder implements interface2 only!!

以及此处:

public static IQueryable<T> Retrieve<T>(ObjectContext context) where T :class, interfaceIFoo    
{
var query = context.CreateObjectSet<T>().AsQueryable();
//...

I'd like make possible a generic method overload.
Since I need to create an ObjectSet<..> without knowing the generic type contained in, I wold build something like this:

public IQueryable<T> MyMethod<T>() where T : class, (IMyFirst || IMySecond) //notice the syntax..!
{
  if(typeOf(T) is IMyFirst..
  else ...
}

How can I reach my purpose..?

Update:

@BrokenGlass wrote:

This type of constraint is not possible in C# - you could however constrain to IFoo and have IMyFirst and IMySecond both implement IFoo.

But that suggestion is not applicable, please see this:

interface1 { property1 {..}}
interface2 { property2 {..}}
interfaceIFoo : interface1, interface2 { }

by any method:

MyWrapper.Retrieve<EntityProduct>(myObjContext); //error-> EntityProduct implements interface1 only!!

by other any method:

MyWrapper.Retrieve<EntityOrder>(myObjContext); //error-> EntityOrder implements interface2 only!!

and here:

public static IQueryable<T> Retrieve<T>(ObjectContext context) where T :class, interfaceIFoo    
{
var query = context.CreateObjectSet<T>().AsQueryable();
//...

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

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

发布评论

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

评论(2

空宴 2024-11-06 06:30:36

这种类型的约束在 C# 中是不可能的 - 但是您可以约束到 IFoo 并让 IMyFirstIMySecond 都实现 IFoo代码>.

如果您可以忍受对实体框架的依赖,您也可以使用 EntityObject

This type of constraint is not possible in C# - you could however constrain to IFoo and have IMyFirst and IMySecond both implement IFoo.

If you can live with dependencies on Entity Framework you could alternatively also use EntityObject

陌上芳菲 2024-11-06 06:30:36

析取通用约束实际上没有意义。这些约束为方法提供了编译时信息,因此在编译时导致类型不明确的约束没有多大意义。例如,如果您的方法只是要诉诸运行时类型检查,您不妨这样做:

public IQueryable<T> MyMethod<T>() where T : class
{
    if (typeOf(T) is IMyFirst) ...
    else ...
}

如果您觉得需要对输入进行类型检查和伪抽象,也许扩展方法恰好相同命名就足够了:

public static IQueryable<IMyFirst> MyMethod(this IMyFirst input)
{
    return ...
}

public static IQueryable<IMySecond> MyMethod(this IMySecond input)
{
    return ...
}

A disjunctive generic constraint doesn't really make sense. Those constraints provide compile-time information to the method, so there's not much point in constraints that result in an ambiguous type at compile time. For instance, if your method is just going to resort to run-time type checking, you might as well just do this:

public IQueryable<T> MyMethod<T>() where T : class
{
    if (typeOf(T) is IMyFirst) ...
    else ...
}

If you feel you need the type checking on input and a pseudo-abstraction, perhaps extension methods that happen to be identically named would suffice:

public static IQueryable<IMyFirst> MyMethod(this IMyFirst input)
{
    return ...
}

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