I have an Interface called as IObSrc, I have few classes that implement this interface called as ObSrc1, ObScr2 etc..
Interface IObSrc
{
string id;
}
I also have an interface called as IDtSrc, this interface inturn is implemented by a few classes called as DtSrc1, DtSrc2.
Interface IDtSrc
{
IObSrc getObject();
Dictionary<string,IObSrc> getObjects();
}
When i implement this interface from DtSrc1, and I return ObSrc1 from DtSrc1, there is no issue, but when i return Dictionary<string,ObSrc1> or Dictionary<string,ObSrc2>, i get an indication of typemismatch.
public class DtSrc : IDtSrc
{
public Dictionary<string,IObSrc> getObjects()
{
Dictionary<string,ObSrc1> val1 = new ...'
...
return val1;
}
}
How can i solve this issue.
* EDITED *
I have one more query, what if a method should return a generic type that can be varied by the implementation.
Example:
interface IDtSrc<T> where T:IObSrc
{
T ExecuteObject(string objId);
}
This will be used as
class DtSrc1
{
string ExecuteObject(string objId)
{..}
}
and
class DtSrc2
{
Dictionary<string,string> ExecuteObject(string objId)
{..}
}
Kindly suggest a right procedure for this also.
You could use generics with a constraint
But your implementation of
IDtSrcwill only be able to return one type ofIObSrc.