I haven't found an answer to this, so I will pose the question...
Assume that I have declared a generic interface such as this.
interface IStuff<T>
{
void DoStuff(T item);
T GetStuff();
}
Now, I declare one or more classes that implement the interface using classes called SomeStuff and SomeOtherStuff like this:
public class MyStuff: IStuff<SomeStuff>
{
void DoStuff(SomeStuff item) { /*body of method*/ }
SomeStuff GetStuff() { /*body of method*/ }
}
public class MyStuff2: IStuff<SomeOtherStuff>
{
void DoStuff(SomeOtherStuff item) { /*body of method*/ }
SomeOtherStuff GetStuff() { /*body of method*/ }
}
So far, no problems. All of the above compiles with ease. Now comes the tricky part, that I don't know how to handle. I have another class, an abstract class, that contains a property. I want that property to be of type interface IStuff. I want the derived classes to implement the typed interfaces IStuff<SomeStuff> and IStuff<SomeOtherStuff> similar to the following.
public abstract class MyParentClass
{
public abstract IStuff<T> myProperty;
}
public class MyDerivedClass1:MyParentClass
{
public override IStuff<SomeStuff> myProperty { get; set; }
}
public class MyDerivedClass2:MyParentClass
{
public override IStuff<SomeOtherStuff> myProperty { get; set; }
}
In the property defined above, I want to pass in an instance of the class Mystuff or MyStuff2, depending on the concrete implementation. The compiler doesn't like the abstract class, it says there is no definition of T. So, is there a way for me to acomplish what I state? Maybe there is a different way? Thoughts would be great.
Thanks.