简体   繁体   中英

Can't access inherited class in another namespace?

I have a base class in a namespace:

namespace n1
{
 public class c1
 {
 }
}

Another class in the same namespace that inherits from c1 :

namespace n1
{
 public class c2:c1
 {
 }
}

I want to create an instance of c2 in another namespace n2 , because n1 is a library and n2 is a Windows Form project where I want to use the library's classes. I have added a reference to n1 . But when I try:

using n1;    

namespace n2
{
 public class c3
 {
  public c1 c1Instance=new c1();
  public c2 c2Instance=new c2();
 }
}

I get this error message:

The type or namespace name 'c2' could not be found

I'm not getting this error with c1 .

Is an inherited class private only?

您需要引用项目或包含c2的dll

You just need to remove the referenced dll (which contains mentioned type 'c2' ) from the list References and reference the library again (either as dll or project if it is linked to the solution).

It was discovered by practice without finding the cause. However the most common source of it is to reference dll placed in workshop folder of the other project directly (eg x86\\Release or bin\\Debug ). So during our developer work we often change Configuration and/or Platform many times and finally the list References probably has references to dll libraries which are not in expected compilation place. In this case better solution is to keep our dlls in separate folder named 'libs' and reference to it or if there is no problem to compile other projects every time we compile main project, to reference the library as Project .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM