C# 中的泛型问题
首先,我对仿制药很陌生。我对以下代码有疑问:
namespace YvanSoftware_V5.Models
{
public interface ISession : IDisposable
{
void CommitChanges();
Db4objects.Db4o.IObjectContainer Container { get; }
void Delete(System.Linq.Expressions.Expression<Func< T, bool>> expression);
void Delete(object item);
void DeleteAll();
void Dispose();
T Single(System.Linq.Expressions.Expression<Func< T, bool>> expression);
System.Linq.IQueryable All();
void Save(T item);
}
}
我收到以下编译错误,但我不知道它在这种情况下意味着什么。
找不到类型或命名空间名称“T”(您是否缺少 using 指令或程序集引用?)
我从 http://www.itslet.nl/?p=125 。
谢谢你的帮助,
伊万
Fist of all, I'm new to generics. I have a problem with the following code:
namespace YvanSoftware_V5.Models
{
public interface ISession : IDisposable
{
void CommitChanges();
Db4objects.Db4o.IObjectContainer Container { get; }
void Delete(System.Linq.Expressions.Expression<Func< T, bool>> expression);
void Delete(object item);
void DeleteAll();
void Dispose();
T Single(System.Linq.Expressions.Expression<Func< T, bool>> expression);
System.Linq.IQueryable All();
void Save(T item);
}
}
I get the following compilation error, but I don't know what it means in this context.
The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)
I got this code from http://www.itslet.nl/?p=125 .
Thank you for your help,
Yvan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为 T 没有定义。编译器将尝试查找名为
T
的类型,但该类型并不存在。尝试将你的接口签名更改为:如果你不希望整个接口是通用的,你可以添加到每个方法,例如:
事实上,这就是这段代码的作者所做的,看看他的实现类
Db4oSession
在您提供的链接中。正确的接口定义应该是:It's because T is not defined. The compiler will try to find a type named
T
, which does not exist. Try changing your interface signature to:If you don't want the whole interface to be generic, you can add to each method, like:
In fact, that's what the author of this code does, look at his implementation class
Db4oSession
in the link you provided. The correct interface definition should be:您需要将 T 指定为泛型函数的模板参数,如下所示:
不过,对于您第一次使用泛型,我将从更简单的内容开始。
You need to specify T as a template parameter on the generic functions like this:
I would start with something more simple for your first use of generics, though.
您需要在接口或方法上定义类型参数
T
:或者
You need to define the type parameter
T
either on the interface or on the method:or
嗯,我是关于Itslet 的文章的作者。我刚刚改进了文章中的代码。语法荧光笔似乎删除了我的
。感谢您的通知。
Well, I am the writer of the article on Itslet. I just improved the code on the article. It seems that the syntax highlighter removed my
<T>
's.Thanks for notifying.