[英]Declaring Explicit interfaces and the does not contain a definition error C#
我创建的显式接口有问题,并且出现异常,
'x'不包含'y'的定义,并且找不到包含'x'类型的第一个参数的扩展方法'y'
我有一系列的课程。 基类:
public interface IFactoryResponse
{
object instance { get; set; }
string instanceconfig { get; set; }
}
明确实现它的类:
public class FactoryResponseImpl : IFactoryResponse
{
object IFactoryResponse.instance {
get { return ((IFactoryResponse)this).instance; }
set { ((IFactoryResponse)this).instance = value; }
}
string IFactoryResponse.instanceconfig {
get { return ((IFactoryResponse)this).instanceconfig; }
set { ((IFactoryResponse)this).instanceconfig = value; }
}
}
在另一堂课中,我得到了上面的错误。 Visual Studio可以找到该接口和类,但是无法解析实例属性。 我在这里想念什么。 我可能会缺少显式继承的更为完善的规则之一。
if (facconfig.useabstract) {
response.instance = Activator.CreateInstance(m_entassembly.GetType(entconfig.Classname, true, true));
response.instanceconfig = facconfig.config;
} else {
Assembly assem = Assembly.LoadFrom(facconfig.assemblyfile);
object Obj = Activator.CreateInstance(assem.GetType(facconfig.Classname, true, true));
response.instance = Obj;
response.instanceconfig = facconfig.config;
}
您的实现不正确。 这将导致StackOverflowException
因为属性会自行调用。 您可以使用自动属性轻松实现属性:
public class FactoryResponseImpl : IFactoryResponse { object IFactoryResponse.instance { get; set; } string IFactoryResponse.instanceconfig { get; set; } }
明确实现接口成员后,您必须将变量视为接口,方法是将类实例强制转换为该接口,或者将其分配为该接口的变量类型。
if (facconfig.useabstract) { ((IFactoryResponse)response).instance = Activator.CreateInstance(m_entassembly.GetType(entconfig.Classname, true, true)); ((IFactoryResponse)response).instanceconfig = facconfig.config; } else { Assembly assem = Assembly.LoadFrom(facconfig.assemblyfile); object Obj = Activator.CreateInstance(assem.GetType(facconfig.Classname, true, true)); ((IFactoryResponse)response).instance = Obj; ((IFactoryResponse)response).instanceconfig = facconfig.config; }
为什么需要显式实现接口? 除非您有充分的理由,否则不应该这样做。 使用隐式实现,一切都变得容易得多:
public class FactoryResponseImpl : IFactoryResponse { public object instance { get; set; } public string instanceconfig { get; set; } }
而且您的其他代码也可以正常工作。
您的显式实现正在引用自己。 您应该引用私有字段或公共实现。 例如:
public class FactoryResponseImpl : IFactoryResponse
{
DatabaseFactoryResponseInstance _instance;
public FactoryResponseImpl()
{
_instance = new DatabaseFactoryResponseInstance();
}
object IFactoryResponse.instance {
get { return (object)_instance; }
set {
if (value != null)
{
DatabaseFactoryResponseInstance dbInstance;
dbInstance = value as DatabaseFactoryResponseInstance;
if (dbInstance == null)
throw new InvalidOperationException();
_instance = dbInstance;
}
}
}
这是您的目标吗?
public interface IFactoryResponse
{
object instance { get; set; }
string instanceconfig { get; set; }
}
public class FactoryResponseImpl : IFactoryResponse
{
object IFactoryResponse.instance { get; set; }
string IFactoryResponse.instanceconfig { get; set; }
}
class Test
{
public void TestMethod()
{
IFactoryResponse response = new FactoryResponseImpl();
response.instance = null;
}
}
如果使用显式接口实现(例如IFactoryResponse.instance),则这些方法将不公开可见。 您需要强制转换为IFactoryResponse来访问它们,或者将方法定义为public:公共对象实例{...}。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.