C
C#4mo ago
stigzler

Modifier for a class you want as a base class only and making not accessible to external assemblies

I have a base class for other classes:
public class ApiGetOutcome
{// stuff }
public class ApiGetOutcome
{// stuff }
and I inherit it thus:
public class ApiGetFileOutcome: ApiGetOutcome
{// stuff }
public class ApiGetFileOutcome: ApiGetOutcome
{// stuff }
These are all in a class library which accesses an API. I don't want ApiGetOutcome visible outside the library, only ApiGetFileOutcome I've messed around with abstract and various modifiers, but can't seem to crack it. Would appreciate a steer!
2 Replies
Angius
Angius4mo ago
Making it non-public would prevent the consumer from doing
void Foo(ApiGetOutcome outcome){}

Foo(new ApiGetFileOutcome());
Foo(new ApiGetExcelOutcome());
void Foo(ApiGetOutcome outcome){}

Foo(new ApiGetFileOutcome());
Foo(new ApiGetExcelOutcome());
that's why it's impossible
stigzler
stigzler4mo ago
thanks folks I tried the below (as it seemed liek the best option) but it didn't hide ApiGetOutcome to external assemblies:
[EditorBrowsable(EditorBrowsableState.Never)]
public class ApiGetOutcome {}
[EditorBrowsable(EditorBrowsableState.Never)]
public class ApiGetOutcome {}
Sorry I'm being dumb - what specifically are you meaning by the "IDE" - designer view, code view? I'm talking it doesn't hide the Class in the VS code editor for the external assembly Ah - good point. Abstract may be the best option, as VS is clear pre-compile that you can't new it up Good point + food for thought. Thanks to both of you for your help