Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot make publicly visible class from internal class?

Tags:

c#

I have an internal class:

internal abstract class Reader
{
    //internal abstract void methods
}

I cannot make this class:

public abstract class CustomFileReader : Reader
{
}

Because it is more visible than the class it inherits from. I want to do this to enforce that you must inherit from a publicly accessible inheritor of the Reader class and not the base class. Is this possible or do I have to expose the base class?

like image 892
jokulmorder Avatar asked Dec 19 '22 15:12

jokulmorder


1 Answers

You can't have an internal class in the inheritance tree of a public class, but you can force users to derive from CustomFileReader instead of Reader, by making the only constructor of Reader internal:

public abstract class Reader
{
    internal Reader()
    {
    }
}

public abstract class CustomFileReader : Reader
{
    // Public and protected constructors here
}

Now anything which tries to inherit directly from Reader outside your assembly will be told that it can't find an accessible base class constructor.

like image 163
Jon Skeet Avatar answered Jan 11 '23 23:01

Jon Skeet