通用接口
这是我的代码
public interface ITranslator<E, R>
{
E ToEntity<T>(R record);
}
class Gens : ITranslator<string, int>
{
#region ITranslator<string,int> Members
public string ToEntity<MyOtherClass>(int record)
{
return record.ToString();
}
#endregion
}
当我编译它时,我得到一个错误类型参数声明必须是标识符而不是类型
为什么我不能有ToEntity
但只能有 ToEntity
??
编辑:MyOtherClass
在做什么?我正在实体(相当于实体框架的 POCO)和记录(框架返回的对象)之间转换多个表/类。所以我想用它来进行我的班级特定转换
Here is my code
public interface ITranslator<E, R>
{
E ToEntity<T>(R record);
}
class Gens : ITranslator<string, int>
{
#region ITranslator<string,int> Members
public string ToEntity<MyOtherClass>(int record)
{
return record.ToString();
}
#endregion
}
When I compile this, I get an error Type parameter declaration must be an identifier not a type
Why is that I cannot have ToEntity<MyOtherClass>
but can only have ToEntity<T>
??
Edit: what is MyOtherClass
doing ? I am converting between entities(POCOs equivalent of Entity framework) and record(Object returned by the framework) for multiple tables/classes. So I would want to use this to do my class specific conversion
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的接口有一个通用
ToEntity
方法,您已在实现类Gens
中将其设为非通用方法ToEntity
。 (泛型方法可以采用任何类型参数,可能会对T
给予一定的限制。您的Gens
类正在尝试为提供定义>ToEntity
仅适用于类型参数MyOtherClass
,这违背了泛型的目的。)在您的代码示例中,尚不清楚您的
Gens 类正在尝试使用
MyOtherClass
类型;它当然不涉及ToEntity
的逻辑。我们需要更多信息才能进一步指导您。为了说明这一点,以下是您当前对
ITranslator
界面提供的简单英语定义:另一方面,您的
Gens
类按照当前的设计方式“实现”了上述接口,如下所示:从这两个描述中,很明显
Gens
类并没有真正执行ITranslator
接口所保证的<也就是说,它不愿意接受用户为其ToEntity
方法指定的类型,这就是无法为您编译此代码的原因。Your interface has a generic
ToEntity<T>
method that you've made non-generic in your implementation classGens
asToEntity<MyOtherClass>
. (A generic method could take any type parameter, possibly given certain constraints onT
. YourGens
class is trying to provide a definition forToEntity
only for the type parameterMyOtherClass
, which defeats the purpose of generics.)In your code example, it's unclear how your
Gens
class is trying to use theMyOtherClass
type; it's certainly not involved in the logic ofToEntity
. We'd need more information to be able to guide you further.To illustrate, here's what your current definition of the
ITranslator<E, R>
interface offers, in plain English:Your
Gens
class, on the other hand, the way it's currently designed, "implements" the above interface like so:From these two descriptions, it's clear that the
Gens
class is not really doing what theITranslator<E, R>
interface guarantees. Namely, it is not willing to accept a user-specified type for itsToEntity
method. That's why this code won't compile for you.您必须声明对泛型类型的约束。
You must declare a constraint on a generic type.
这对我来说在 LINQpad 中编译没问题。也许你在某处有一个名为 E、R 或 T 的类型?啊,我明白你想要做什么...你在某处将 MyOtherClass 定义为类,但你试图将它用作类型ToEntity 中的参数。您究竟希望 MyOtherClass 如何参与 ToEntity?
That compiles OK for me in LINQpad. Maybe you have a type named E, R, or T somewhere?Ahh I see what you're trying to do... you have MyOtherClass defined as a class somewhere yet you're trying to use it as a type argument in ToEntity. How exactly do you want MyOtherClass involved in ToEntity?