Deriving a more specific interface from a generic interface


G

Guest

Hi all - I'm sure I'm being brain-dead here - and I think the answer is 'you
can't do that', but I thought I would try.

I have an interface that is Generic, with two type arguments. I want to
create a second interface that specifices the second type argument. I also
want to have objects implement that second interface without having to
implement the more generic methods that are inherited from the first. This
is especially evident when I implement multiple interfaces:

Some code will illustrate the problem:

// Base Type
interface I
{}

// 'Base' manager
interface IBase<T1, T2> where T2 : ICollection<T1>
{
T2 GetT1s();
}

// Base type collection
interface ICol<T1> : ICollection<T1> where T1 : I {}

// Derived type
interface IDerived<T1> : IBase<T1, ICol<T1>> where T1 : I {}

// Actual entities to manage
class A : I {}
class B : I {}

// Manager
class Mgr : IDerived<A>, IDerived<B>
{
public ICol<A> GetT1s()
{...}

// Note the explicit implementation here
ICol<B> IBase<B, ICol<B>>.GetT1s()
{...}
}

I would really like to get rid of the 'IBase<B, ICol<B>>' - I would like to
use IDerived<B>, but this doesn't work. if I change IDerived to look like
this:

interface IDerived<T1> : IBase<T1, ICol<T1>> where T1 : I
{ new ICol<T1> GetT1s();}

Then I need to implement two forms of the method in the manager:

class Mgr : IDerived<A>, IDerived<B>
{
public ICol<A> GetT1s()
{...}

// I want the following method...
ICol<B> IDerived<B>.GetT1s()
{...}

// But not this one!
ICol<B> IBase<B, ICol<B>>.GetT1s()
{...}
}

What am I doing wrong? I would love to only have to implement
IDerived<B>.GetT1s();

Thanks,
Phil
 
Ad

Advertisements

J

Jesse McGrew

When you explicitly implement an interface method, you have to qualify
it with the name of the interface where it was defined. In your
example, that is IBase<B, ICol<B>>. IDerived<B> doesn't define any
methods, so you can't use it in an explicit implementation.

It is annoying, yes, but the VS2005 IDE will help by writing all the
explicit implementations for you with the correct base interface names.
For example, if you add IList<int> to a class's interface list, then
click the little box on the interface name and tell it to explicitly
implement the interface, it'll write some methods for IList<int>, some
for ICollection<int>, one for IEnumerable<int>, and one for
IEnumerable.

Jesse
 
Ad

Advertisements

G

Guest

Thanks Jesse - I was afraid of that. The problem really shows itself when
implement multiple interfaces - like when I use a partial class to define a
data provider (which I happen to be doing). Since some of the methods only
differ by return type, I have to define them explicity.

Thanks again,
Phil
 

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