问题
I have a generic class X<T>
; This class has a covariant part that I want to be able to access covariantly. So I factor it out into an interface IX<out T>
. However, I want this interface to be visible only to the class itself, because it contains also methods that are ment to be private
.
I.e., inside the class itself, I can upcast to IX<T>
and use it covariantly. E.g.:
class X<T> : IX<T> {
private interface IX<out T>{ // the private covariant interface
void foo();
}
// It grants access to the private method `foo`
private T foo(){...}
public T IX.foo(){ return foo(); }
private static void someMethod(IX<T> x) {
// Here I can use `x` covariantly
}
}
Is this possible? I have never heard of private
nested interfaces before, since a private interface usually makes no sence at all. However, with generics such an interface becomes necessary for implementing "private-only covariance".
When I try to compile this, I receive the following error:
foo.cs(1,14): error CS0246: The type or namespace name `IX' could not be found. Are you missing an assembly reference?
foo.cs(9,14): error CS0305: Using the generic type `X<T>.IX<S>' requires `1' type argument(s)
Which is basically clear, an inner type of a generic type needs a type parameter for the outer type. Is there a way to get this code to compile correctly?
回答1:
Edit: it looks like this compiles on the Roslyn / C# 6 tech preview, but does not compile on the MS C# 5 compiler or the mono compiler.
Yes, like this - but note that actually the inner T
is unnecessary in many ways, and if you retain it - it would be useful to name it TInner
or something to avoid confusion, since the T
in X<T>
is technically a different thing than X<>.IX<T>
, even though they will always be the same actual type in practice:
class X<T> : X<T>.IX<T>
{
private interface IX<out TInner>
{ // the private covariant interface
void foo();
}
// It grants access to the private method `foo`
private T foo() { throw new NotImplementedException(); }
void X<T>.IX<T>.foo() { throw new NotImplementedException(); }
private static void someMethod(IX<T> x)
{
// Here I can use `x` covariantly
}
}
回答2:
In order for it to compile and limit the visibility of your interface just to your assembly you can mark it as internal. The thing is if it's declared as an inner type it won't be seen by your class. This code should work:
internal interface IX<out T> // the private covariant interface
{
T foo();
}
class X<T> : IX<T>
{
// It grants access to the private method `foo`
private T foo(){ return default(T); }
T IX<T>.foo(){ return foo(); }
private static void someMethod(IX<T> x)
{
// Here I can use `x` covariantly
}
}
This way the interface is still private, but as it's not an inner type anymore it can be used on your class.
来源:https://stackoverflow.com/questions/24820885/c-is-a-private-inner-interface-possible