调用基本构造函数不起作用 C#
我有以下代码返回错误“‘object’不包含采用 x 参数的构造函数。”尝试调用基本构造函数。
解决方案 1,项目 1
namespace Project.Sub.A
{
internal class Foo
{
internal Foo(int a, long b) {}
}
}
解决方案 1,项目 2
namespace Project.Sub.B{
internal class Bar : Foo
{
internal Bar(int a, long b,long c) :base(a,b+c) {}
}
}
我不知道为什么这不起作用。我的命名空间配置不正确吗?
I have the following code returning the error " 'object' does not contain a constructor that takes x arguments." on the line trying to call the base constructor.
Solution 1, project 1
namespace Project.Sub.A
{
internal class Foo
{
internal Foo(int a, long b) {}
}
}
Solution 1,project 2
namespace Project.Sub.B{
internal class Bar : Foo
{
internal Bar(int a, long b,long c) :base(a,b+c) {}
}
}
I have NO idea why this does not want to work. Could be my namespaces configured incorrectly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
内部
访问是按程序集进行的,而不是按名称空间进行的。由于基类中的构造函数被声明为
internal
,因此其他项目中的子类无法访问它。尝试将其更改为受保护的内部
,或只是受保护
。更新
刚刚注意到基类也是
内部
。如果您希望在第二个项目中看到它,您需要将其设为public
。或者,您可以在 Project1 的AssemblyInfo.cs
中添加[assemble:InternalsVisibleTo("Project2")]
。 (不过,我个人不会推荐这个选项。)internal
access is per assembly, not namespace.Because the constructor in the base class is declared
internal
, it is not accessible to the subclass in the other project. Try changing it toprotected internal
, or justprotected
.update
Just noticed that the base class is also
internal
. You will need to make itpublic
if you want it to be seen in the second project. Or, you can add[assembly:InternalsVisibleTo("Project2")]
inAssemblyInfo.cs
in Project1. (I wouldn't personally recommend this option, though.)这里有许多令人困惑的问题。
internal
表示类仅在其自己的程序集中可见,而对程序集外部的客户端代码不可见。 Foo 应该是公共的,以便可以在其他程序集中使用There are a number of confounding issues here.
internal
means that a class is only visible within its own assembly and not to client code outside of the assembly. Foo should be public so that it can be used in other assemblies内部意味着
对当前程序集中的其他类可见
因为您在第二个项目中定义第二个类,所以它看不到该基本构造函数。
尝试同时创建 Foo 类和 Foo 类。构造函数
protected
改为internal
。internal means
visible to other classes in the current assembly
Because you're defining your second class in a second project, it can't see that base constructor.
Try making both the Foo Class & Constructor
protected
instead orinternal
.如果正如您的问题所示,它位于一个单独的项目中,并且您的基类被标记为内部,那么它不应该能够找到整个类型,更不用说构造函数了。
将 Foo 的访问器更改为公共。
If it's in a separate project as your question suggests, and your base class is marked internal, then it shouldn't be able to find the entire type, let alone the constructor.
Change Foo' accessor to public.