Reputation: 303
I've searched for answers on similar errors, but haven't been able to find any. Also I know how to fix the error (make all public - which I don't really want to do), but I don't really understand why this is not allowed. Anyway - I'm getting
Inconsistent accessibility: property type 'E7XLibrary.Base.MultiSegmentBase' is less accessible than property 'E7XLibrary.E7XGroupBase.GroupSegment'
My MultiSegmentBase class is declared as an internal class (and so is the SegmentBase class):
internal class MultiSegmentBase : SegmentBase
I’m using a MultiSegmentBase instance as a protected abstract property in my public E7XGroupBase class:
public abstract class E7XGroupBase
{
internal abstract UInt64 Length { get; }
protected abstract MultiSegmentBase GroupSegment { get; }
internal virtual void Wrap(BinaryWriter writer)
{
GroupSegment.Length = this.Length;
GroupSegment.Wrap(writer);
}
}
And lastly I have a devided public class E7XSessionGroup class that implements the abstract E7XGroupBase, like this
public class E7XSessionGroup : E7XGroupBase
{
#region Properties
private SessionGroupSegment _groupSegment = null;
protected override MultiSegmentBase GroupSegment
{
get
{
if (_groupSegment == null)
_groupSegment = new SessionGroupSegment();
return _groupSegment;
}
}
As I said, I know that if I just declare MultiSegmentBase as a public class instead of internal, I wouldn’t have this error, but since I’m making an API I don’t want to expose MultiSegmentBase and then further all the classes that derive from from MultiSegmentBase.
Basically, my quistion is why does MultiSegmentBase needs to be as accessible as E7XGroupBase and E7XSessionGroup, when I only use MultiSegmentBase in a protected property? And do any of you have a good workaround?
Upvotes: 2
Views: 929
Reputation: 16403
The problem is you have a public class
with a public property which is an internal class
:
public abstract class E7XGroupBase
{
protected abstract MultiSegmentBase GroupSegment { get; }
}
The E7XGroupBase
can be extended by a class in a different assembly (since it's public) but that extending class couldn't resolve the MultiSegmentBase
class because that is defined as internal.
This may help explain:
Assembly 1:
public abstract class E7XGroupBase
internal class MultiSegmentBase : SegmentBase
Assembly 2:
public class Something : E7XGroupBase
{
// Can't resolve type MultiSegmentBase
}
You either need to make MultiSegmentBase
public, or create a public interface for it and use that in E7XGroupBase
:
public interface IMultiSegmentBase { }
internal class MultiSegmentBase : IMultiSegmentBase {}
public abstract class E7XGroupBase
{
protected abstract IMultiSegmentBase GroupSegment { get; }
}
Upvotes: 6
Reputation: 424
This is because MultiSegmentBase is internal which means it can only be accessed by classes in the same assembly.
Protected means that that it can be only accessed by derived classes. But derived classes does have to sit in the same assembly that's why compiler is giving you this error.
Upvotes: 2
Reputation: 125498
protected abstract MultiSegmentBase GroupSegment { get; }
is a protected property meaning that it can be accessed within E7XGroupBase
or any type that derives from E7XGroupBase
, since E7XGroupBase
is a public abstract class. However, the type of the property MultiSegmentBase
is internal, meaning that it can only be accessed within the assembly in which it is defined.
How to fix this depends; Should types in another assembly be able to inherit from E7XGroupBase
? If so, you need to make MultiSegmentBase
and SegmentBase
more accessible so that they can also be accessed in another assembly.
Upvotes: 0