Inherit an internal class to a public class

G

guiroux22

Hi everybody,

I have two assemblies. The first is used by the client sites and
provides some public classes using internal classes (access must be
restrictive). The second is an "administrative" assembly which add
functionalities to the current classes (free access to the classes).

This assembly have access to the internal classes thanks to friendly
assemblies.
http://www.c-sharpcorner.com/UploadFile/rmcochran/reuse10312006141427PM/reuse.aspx

So i want to inherit internal classes from the first assembly to
public classes in the second assembly, but Visual Studio can't compile
it.

namespace Assembly1
{
internal class MyClass
{
...
}
}

namespace Assembly2
{
public class MyClass : Assembly2.MyClass
{
...
}
}

Maybe someone have a solution ?

Thanks
 
J

Jon Skeet [C# MVP]

So i want to inherit internal classes from the first assembly to
public classes in the second assembly, but Visual Studio can't compile
it.

You can't do this whether they're in the same assembly or not. From the
ECMA spec for C# 2, section 17.1.2.1:

<quote>
The direct base class (and any type arguments) of a class type shall be
at least as accessible as the class type itself (§10.5.4). For example,
it is a compile-time error for a public class to derive from a private
or internal class.
</quote>

If you think about it, it makes sense - clients of your public class
would be able to "see" that class from whatever assembly they were in,
but may not be able to "see" its base class. How would they know what
members it had etc?
 
G

Guest

OO define an encapsulation method.
public cannot inherit from internal, but you can solve the issue using an
interface deinfe the Assembly2.MyClass publicity methods, then implement the
interface by using the functionallity from the Assembly2.MyClass . i.e.


namespace Assembly2
{
public class MyClass : IMyClass {

public void A()
{
Assembly2.MyClass.A();
}

public string B
{
get
{
return Assembly2.MyClass.B;
}
}
}
}

In case the Assembly2.MyClass return type or input element (used by the
method) are internal, then add an interface in order to bridge the internal
and public.
 
B

Ben Voigt [C++ MVP]

Hi everybody,

I have two assemblies. The first is used by the client sites and
provides some public classes using internal classes (access must be
restrictive). The second is an "administrative" assembly which add
functionalities to the current classes (free access to the classes).

This assembly have access to the internal classes thanks to friendly
assemblies.
http://www.c-sharpcorner.com/UploadFile/rmcochran/reuse10312006141427PM/reuse.aspx

So i want to inherit internal classes from the first assembly to
public classes in the second assembly, but Visual Studio can't compile
it.

namespace Assembly1
{
internal class MyClass
{
...
}
}

namespace Assembly2
{
public class MyClass : Assembly2.MyClass
{
...
}
}

Maybe someone have a solution ?

How about this:


[assembly: InternalsVisibleTo("Assembly2")]
namespace Assembly1
{
internal class ImplementationDetails
{
...
}
}

namespace Assembly2
{
public interface IMyClass
{
...
}

class MyClass : Assembly2.ImplementationDetails, IMyClass
{
...
}
}
 
B

Ben Voigt [C++ MVP]

Ben Voigt said:
Hi everybody,

I have two assemblies. The first is used by the client sites and
provides some public classes using internal classes (access must be
restrictive). The second is an "administrative" assembly which add
functionalities to the current classes (free access to the classes).

This assembly have access to the internal classes thanks to friendly
assemblies.
http://www.c-sharpcorner.com/UploadFile/rmcochran/reuse10312006141427PM/reuse.aspx

So i want to inherit internal classes from the first assembly to
public classes in the second assembly, but Visual Studio can't compile
it.

namespace Assembly1
{
internal class MyClass
{
...
}
}

namespace Assembly2
{
public class MyClass : Assembly2.MyClass
{
...
}
}

Maybe someone have a solution ?

How about this:


[assembly: InternalsVisibleTo("Assembly2")]
namespace Assembly1
{
internal class ImplementationDetails
{
...
}
}

namespace Assembly2
{
public interface IMyClass
{
...
}

class MyClass : Assembly2.ImplementationDetails, IMyClass

oops, that was supposed to be Assembly1.ImplementationDetails
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top