错误 CS0117:命名空间.A 不包含接口的定义

发布于 2024-08-31 23:35:11 字数 1315 浏览 4 评论 0原文

我收到错误:


“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就像说晚安 2024-09-07 23:35:11

该错误与您在引用使用另一个程序集中定义的类型的程序集并且未引用依赖项的依赖项时所得到的错误有些一致。

要解决此问题,请添加对包含 IObject 的程序集的引用,作为对包含 Implementation 的项目的引用。

这是小图。如果 Assembly2 公开 Assembly3 中定义的类型,则 ASsembly1 也必须引用 Assembly3。以下情况不起作用:

_____________             _____________               _____________
| Assembly1  |references  | Assembly2  |references    | Assembly3  |
|           -|------------|->         -|--------------|->          |
|            |            | public     |              | IObject    |
|            |            |  IObject   |              |            |
|            |            |            |              |            |
-------------             -------------               -------------

仅当 Assembly3 中定义的类型可通过 Assembly2 访问时,这才会出现问题。这将发生在以下情况之一:

  • Assembly2 中定义的类型派生自 Assembly3 中的类型。
  • Assembly2 中定义的方法使用 Assembly3 中的类型作为返回类型或参数。
  • Assembly2 中定义的类型将 Assembly3 中的类型公开为属性或公共字段。

您需要从 Assembly1 添加对 Assembly3 的引用以使其编译。

_____________             _____________               _____________
| Assembly1  |references  | Assembly2  |references    | Assembly3  |
|           -|------------|->         -|--------------|->          |
|            |            | public     |              | IObject    |
|            |references  |  IObject   |              |            |
|           -|------------|------------|--------------|->          |
|            |            |            |              |            |
-------------             -------------               -------------

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 containing Implementation.

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:

_____________             _____________               _____________
| Assembly1  |references  | Assembly2  |references    | Assembly3  |
|           -|------------|->         -|--------------|->          |
|            |            | public     |              | IObject    |
|            |            |  IObject   |              |            |
|            |            |            |              |            |
-------------             -------------               -------------

This is only an issue when a type defined in Assembly3 is accessible through Assembly2. This will be in one of the following situations:

  • Type defined in Assembly2 derives from a type in Assembly3.
  • Method defined in Assembly2 uses a type from Assembly3 as return type or as an argument.
  • Type defined in Assembly2 exposes a type from Assembly3 as a property or a public field.

You will need to add a reference to Assembly3 from Assembly1 to make it compile.

_____________             _____________               _____________
| Assembly1  |references  | Assembly2  |references    | Assembly3  |
|           -|------------|->         -|--------------|->          |
|            |            | public     |              | IObject    |
|            |references  |  IObject   |              |            |
|           -|------------|------------|--------------|->          |
|            |            |            |              |            |
-------------             -------------               -------------
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文