0

Possible Duplicate:
C# -Generics Help

Let’s start with this interface IBar as below.

public interface IBar<T> : IQueryable<T>, IEnumerable<T> where T : class
    {

    }

I have following 3 classes Bar1, Bar2 and Bar3.

public class Bar1
    {

    }

    public class Bar2
    {

    }

    public class Bar3
    {

    }

Then I have another interface called IFoo which contains generic IBar for each of above three classes as shown below.

public interface IFoo
    {
        IBar<Bar1> Bar1 { get; set; }
        IBar<Bar2> Bar2 { get; set; }
        IBar<Bar3> Bar3 { get; set; }
    }

Finally I have three classes Foo1Sample, Foo2Sample and Foo3Sample. Each of this class contains an instance of IFace interface and also has a Data property that returns IQueryable as shown below.

public class Foo1Sample
    {  
        IFoo foo;

        public IQueryable<Bar1> Data
        {
            get { return foo.Bar1; }
        }
    }

    public class Foo2Sample
    {
        IFoo foo;

        public IQueryable<Bar2> Data
        {
            get { return foo.Bar2; }
        }
    }  

    public class Foo3Sample
    {
        IFoo foo;

        public IQueryable<Bar3> Data
        {
            get { return foo.Bar3; }
        }
    }

I was just wondering if I can create a base class that could be used for Foo1Sample, Foo2Sample and Foo3Sample such that Data property can return appropriate type from that IFoo interface.

Community
  • 1
  • 1
  • Not really, since your `Sample` classes each return a different *property* of the IFoo. Generics only abstract over types. – millimoose Nov 19 '12 at 19:53

3 Answers3

0

You could have something like

public interface IBar<T> : IQueryable<T>, IEnumerable<T> where T : class   
{
    T Bar { get; set;}
}

public class Foo<T>
{  
    IFoo<T> foo;

    public IQueryable<T> Data
    {
        get { return foo.Bar; }
    }
}
Mihai
  • 2,740
  • 31
  • 45
0

The closest thing that comes to mind is something like: class FooGenericSample { Func _getter; IFoo _foo;

    public FooGenericSample(Func<IFoo, TBar> getter) {
        _getter = getter;
    }

    public IQueryable<TBar> Data {
        get { 
            return _getter(_foo);
        }
    }
}

// ...

var foo1Sample = new FooGenericSample(f => f.Bar1);
// foo2Sample etc.

This avoids the boilerplate and only makes the property access differ between sample instances. If all you need is the Data property, I'd just pass a Func<IBar<T>> around directly:

var func = () => foo.Bar1;
millimoose
  • 39,073
  • 9
  • 82
  • 134
0

This will only work if you're able to have IFoo only have one Bar property. If not, then you can't do anything.

public interface IBar<T> : IQueryable<T>, IEnumerable<T> where T : class
{

}
public interface IFoo<T> where T : IBar
{
    IBar<T> Bar { get; set; }
}

And then either

public class Foo1Sample
{  
    IFoo<Bar1> foo;

    public IQueryable Data
    {
        get { return foo.Bar; }
    }
}

or

public class FooSample<T> where T: IBar
{
    IFoo<T> foo;

    public IQueryable Data
    {
        get { return foo.Bar; }
    }
}
Bobson
  • 13,498
  • 5
  • 55
  • 80