具有原始参数的扩展方法
我有一个以这种方式声明的扩展方法:
public static IEnumerable<TEntity> AsEnumerable<TEntity>(this IDBQueryable<TEntity> dbQueryable) where TEntity : class
{
return dbQueryable.Query.AsEnumerable();
}
使用示例将是下一个:
//It works properly
IDBQueryable<Customer> customers = GetCustomerSet();
customers.AsEnumerable();
// It cannot be compiled because int type doesn't fullfill
// the constrains of the extension method
// It must be a reference type in order to be passed as a parammeter.
IDBQueryable<int> customerIDs = GetCustomerIDs();
customers.AsEnumerable();
如何拥有适用于我展示的两个用例的扩展方法? 先感谢您。
I have an extension method declared in this way:
public static IEnumerable<TEntity> AsEnumerable<TEntity>(this IDBQueryable<TEntity> dbQueryable) where TEntity : class
{
return dbQueryable.Query.AsEnumerable();
}
A sample of use would be the next one:
//It works properly
IDBQueryable<Customer> customers = GetCustomerSet();
customers.AsEnumerable();
// It cannot be compiled because int type doesn't fullfill
// the constrains of the extension method
// It must be a reference type in order to be passed as a parammeter.
IDBQueryable<int> customerIDs = GetCustomerIDs();
customers.AsEnumerable();
How can I have an extension method that works for the two use cases I have shown?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需删除
where TEntity : class
约束即可。这里没有明显的约束需要,任何: class
或: struct
的使用都会阻止这两个用例之一。Just remove the
where TEntity : class
constraint. There is no obvious need for a constraint here, and any use of: class
or: struct
will prevent one of the two use-cases.