Deferring implementation of interface to inner class


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...
 
Ad

Advertisements

P

Peter Morris

It was in Delphi for example but not in C#. In fact even Delphi for .NET
removed this feature so I suspect it isn't possible in .NET at all, the
compile would have to implement those methods automatically and pass them
on.
 
A

Arjan de Haan

Peter said:
It was in Delphi for example but not in C#. In fact even Delphi for
.NET removed this feature so I suspect it isn't possible in .NET at all,
the compile would have to implement those methods automatically and pass
them on.

Yes. I'm coming from a Delphi background :-D
I thought it was a handy feature, and since Anders Hejlsberg designed
(most of) C# too, I was hoping this feature got in somehow.

....Arjan...
 
I

Ignacio Machin ( .NET/ C# MVP )

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:

Hi,

No it's not , and IIRC neither in C++ or in any other language I
remember , The nested class is simply another type that is declared
like that to prevent naming conflict (and also to signally the
dependence between the types)
 
A

Arjan de Haan

Ignacio said:
Hi,

No it's not , and IIRC neither in C++ or in any other language I
remember , The nested class is simply another type that is declared
like that to prevent naming conflict (and also to signally the
dependence between the types)

Ignacio,

thanks for your reply. I didn't remember for sure if it was C# code I
saw it in. Apparently not.

But Delphi DOES support this kind of construct. Very convenient.

....Arjan...
 
Ad

Advertisements

P

Paul

Personally I am at aloss as to why this would ever be useful.

Where do I start with the example. It makes no sense for Outer to even
implement the interface in any case if it is not implementing the methods.
It clearly is not a IMyInterface.

If it is an aggregated object then why Aggregate at type level?

From what you have described you seem to be creating some wierd and
wonderful Composite pattern one that until I understand differently Im glad
they left out of C#.

Yes all these techy things are a bit of fun but does it actually reflect
something you are creating for the real world.
 
Ad

Advertisements

P

Paul

Personally I am at aloss as to why this would ever be useful.

Where do I start with the example. It makes no sense for Outer to even
implement the interface in any case if it is not implementing the methods.
It clearly is not a IMyInterface.

If it is an aggregated object then why Aggregate at type level?

From what you have described you seem to be creating some wierd and
wonderful Composite pattern one that until I understand differently Im glad
they left out of C#.

Yes all these techy things are a bit of fun but does it actually reflect
something you are creating for the real world.
 

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