Generics And Interfaces....

I'm currently trying to use generics to make my interfaces a little bit more flexible. 
Unfortunately I seem to have hit a problem with the compiler complaining when I try to use an interface as an input parameter, if that interface requires a type to be defined. 

I'm not sure if I explained that at all well, but given the following example, the compiler complains that I haven't specified a type in the defintion of someMethod.  I've tried putting in <T> but it still complains.  It only works if I strongly type (by putting <string> in there.



//An underlying type to hold some data.

public interface IDic<T>: IDictionary<string, T>
{
   T this[string key]
   {
     get;
     set;
   }
}

//A higher level object which uses the underlying type plus some others.
public interface IFoo<T>
{
   IDic<T> someProperty
   {
      get;
      set;
   }
}

//Something that takes the object in and does something with it.
public interface IBar
{
   bool someMethod(IFoo foo);
}

 


 


I really don't want to strongly type at the IBar level and would prefer that I could genericise this somehow.  Does anyone have any ideas


Answer this question

Generics And Interfaces....

  • Pete2

    D'oh, solved it myself.  In effect I had to update IBar to do this...

    public interface IBar<T>
    {
       bool someMethod(IFoo<T> foo);
    }

  • trc_pdx

    Depending on what IBar is supposed to do, you might want to also consider...


    //Something that takes the object in and does something with it.
    public interface IBar
    {
       bool someMethod<T>(IFoo<T> foo);
    }

     


  • Generics And Interfaces....