【发布时间】:2012-12-01 16:54:10
【问题描述】:
如果我有这个界面:
public interface IFoo : IDisposable
{
int PropA {get; set;}
int PropB {get; set;}
}
还有一个班级:
public class Foo : IFoo
{
public int PropA {get; set;}
public int PropB {get; set;}
public void Dispose()
{
Dispose();
GC.SuppressFinalize(this);
}
}
如果没有“无法隐式转换”错误,这是否应该工作?
private Context context = new Context();
private GenericRepository<IFoo> FooRepo;
public GenericRepository<IFoo> Article
{
get
{
if (this.FooRepo == null)
{
this.FooRepo = new GenericRepository<Foo>(context);
}
return FooRepo;
}
}
我认为我做对了,正确的做法是什么?
【问题讨论】:
-
this.FooRepo = new GenericRepository<IFoo>(context); -
你可以使用这个方法:stackoverflow.com/questions/222403/…
-
接口通则:类类型只用于构造;在其他任何地方使用接口类型。