错误 CS0117:命名空间.A 不包含接口的定义
我收到错误:
“Namespace.A”不包含“MyObjectInterface”的定义,并且没有扩展方法“MyObjectInterface”接受类型的第一个参数...
代码如下所示:
public abstract class Base
{
public IObject MyObjectInterface { get; set; }
}
public class A : Base
{
/**/
}
public class Implementation
{
public void Method()
{
Base obj = new A();
obj.MyObjectInterface = /* something */; // Error here
}
}
IObject 是在单独的程序集中定义的,但是:
- IObject 位于单独的程序集/命名空间中
- Base 和 A 位于相同的程序集/命名空间中,并且各自具有正确的 using 指令
- 实现位于第三个独立的程序集命名空间中,也具有正确的 using 指令。
在尝试设置 MyObjectInterface 之前强制转换为 A 不起作用
在- 具体来说,我正在尝试将 MyObjectInterface 的值设置为模拟对象(不过,我创建了一个假的,但没有成功)
我已经尝试了我能想到的一切。请在我失去更多头发之前帮忙。
编辑 我也无法通过创建测试应用程序来重现该错误,这就是我在这里的原因以及我感到沮丧的原因。
@Reed Copsey:/*某些*/要么是NUnit.DynamicMock(IMailer).MockInstance,要么是我创建的从IObject继承并仅返回预设值的Fake对象。
@Preet Sangha:我检查过,没有其他引用的程序集有 IObject 的定义(具体来说,它称为 IMailer)。
问题是智能感知获取了该属性,但是当我编译时,我得到了 CS0117。我什至可以在实现中“转到定义”,它会将我带到我定义它的位置。
I'm getting the error:
'Namespace.A' does not contain a definition for 'MyObjectInterface' and no extension method 'MyObjectInterface' accepting a first argument of type ...
I've looked at this and this and neither seems to apply.
The code looks like:
public abstract class Base
{
public IObject MyObjectInterface { get; set; }
}
public class A : Base
{
/**/
}
public class Implementation
{
public void Method()
{
Base obj = new A();
obj.MyObjectInterface = /* something */; // Error here
}
}
IObject is defined in a separate assembly, but:
- IObject is in a separate assembly/namespace
- Base and A are in the same assembly/namespace each with correct using directives
- Implementation is in a third separate assembly namespace, also with correct using directives.
Casting to A before trying to set MyObjectInterface doesn't work
- Specifically, I'm trying to set the value of MyObjectInterface to a mock object (though, I created a fake instead to no avail)
I've tried everything I can think of. Please help before I lose more hair.
edit
I can't reproduce the error by creating a test app either, which is why I'm here and why I'm frustrated.
@Reed Copsey: /* something */ is either an NUnit.DynamicMock(IMailer).MockInstance or a Fake object I created that inherits from IObject and just returns canned values.
@Preet Sangha: I checked and no other assembly that is referenced has a definition for an IObject (specifically, it's called an IMailer).
Thing is that intellisense picks up the Property, but when I compile, I get CS0117. I can even 'Go To Definition' in the implementation, and it takes me to where I defined it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误与您在引用使用另一个程序集中定义的类型的程序集并且未引用依赖项的依赖项时所得到的错误有些一致。
要解决此问题,请添加对包含
IObject
的程序集的引用,作为对包含Implementation
的项目的引用。这是小图。如果 Assembly2 公开 Assembly3 中定义的类型,则 ASsembly1 也必须引用 Assembly3。以下情况不起作用:
仅当 Assembly3 中定义的类型可通过 Assembly2 访问时,这才会出现问题。这将发生在以下情况之一:
您需要从 Assembly1 添加对 Assembly3 的引用以使其编译。
The error is somewhat consistent with the error you get when you are referencing an assembly that uses a type defined in another assembly and you are not referencing the dependency's dependency.
To fix this add a reference to the assembly containing
IObject
as a reference to the project containingImplementation
.Here is little diagram. If Assembly2 exposes a type defined in Assembly3 then ASsembly1 must reference Assembly3 as well. The below situation will not work:
This is only an issue when a type defined in Assembly3 is accessible through Assembly2. This will be in one of the following situations:
You will need to add a reference to Assembly3 from Assembly1 to make it compile.