A
Arjan de Haan
Hi.
I'm at a loss here. I seem to remember that it is possible to allow a
nested class to implement an interface that is declared on the outer
class, like this:
public class Outer : IMyInterfave
{
private readonly Inner _innerImpl = new Inner();
private class Inner : IMyInterface
{
public int FirstMethod()
{
...
}
public void SecondMethod()
{
...
}
}
public void OtherMethod()
{
...
}
//
// The next 2 methods I DONT want
//
public int FirstMethod()
{
return _innerImpl.FirstMethod();
}
public void SecondMethod()
{
return _innerImpl.SecondMethod();
}
}
I know I can get the behavior I want if I implement FirstMethod() and
SecondMethod() in Outer and call the same methods on the instance of the
inner class (see above).
But, would this be possible with help from the compiler? In other words,
would it be possible to tell (using Attribites?) to tell the compiler
that the interface IMyInterface for class Outer is implemented using
Inner? If so, that would mean the methods for the interface as declared
in Outer would not be necessary. Perhaps by introducing a property of
the Interface and adding a Attribute to it, like so:
public class Outer : IMyInterface
{
private readonly Inner _innerImpl = new Inner();
private class Inner : IMyInterface
{
...
...
}
public void OtherMethod()
{
...
}
[Implements(IMyInterface)]
public Inner InnerClass { get { return _innerImpl; } }
}
Now I don't how to write the boring IMyInterface methods and delegation
to the inner class.
Is this possible using C#?
Tx.
....Arjan...
I'm at a loss here. I seem to remember that it is possible to allow a
nested class to implement an interface that is declared on the outer
class, like this:
public class Outer : IMyInterfave
{
private readonly Inner _innerImpl = new Inner();
private class Inner : IMyInterface
{
public int FirstMethod()
{
...
}
public void SecondMethod()
{
...
}
}
public void OtherMethod()
{
...
}
//
// The next 2 methods I DONT want
//
public int FirstMethod()
{
return _innerImpl.FirstMethod();
}
public void SecondMethod()
{
return _innerImpl.SecondMethod();
}
}
I know I can get the behavior I want if I implement FirstMethod() and
SecondMethod() in Outer and call the same methods on the instance of the
inner class (see above).
But, would this be possible with help from the compiler? In other words,
would it be possible to tell (using Attribites?) to tell the compiler
that the interface IMyInterface for class Outer is implemented using
Inner? If so, that would mean the methods for the interface as declared
in Outer would not be necessary. Perhaps by introducing a property of
the Interface and adding a Attribute to it, like so:
public class Outer : IMyInterface
{
private readonly Inner _innerImpl = new Inner();
private class Inner : IMyInterface
{
...
...
}
public void OtherMethod()
{
...
}
[Implements(IMyInterface)]
public Inner InnerClass { get { return _innerImpl; } }
}
Now I don't how to write the boring IMyInterface methods and delegation
to the inner class.
Is this possible using C#?
Tx.
....Arjan...