Implement interface's IEnumerable<BaseClass> with IEnumerable<DerivedClass>? - c#

I'm implementing an interface that has a member IEnumerable<BaseClass> Member. But I'd like to store some extra information in each BaseClass item, for which I've created a derived class. This is what I'd like to be able to do:
interface IImplementMe
{
IEnumerable<BaseClass> Member { get; }
}
class DerivedClass : BaseClass
{
// Some extra stuff here
}
class Implementation : IImplementMe
{
IEnumerable<DerivedClass> Member { get; }
}
I don't think there's a way to do this (if there is please let me know!). There may be repeated items in the Member list, so I cannot use a dictionary to store the extra stuff. What would it be the standard, elegant way to achieve this?

You can use explicit interface implementations for this.
class Implementation : IImplementMe
{
public IEnumerable<DerivedClass> Member;
IEnumerable<BaseClass> IImplementMe.Member { get { return Member; } }
}

You have two options I think:
Use the derived class as type parameter:
interface IImplementMe<T> where T : BaseClass
{
IEnumerable<T> Member;
}
class DerivedClass : BaseClass
{
// Some extra stuff here
}
class Implementation : IImplementMe<DerivedClass>
{
IEnumerable<DerivedClass> Member;
}
Use IEnumerable:
interface IImplementMe
{
IEnumerable Member;
}
class DerivedClass : BaseClass
{
// Some extra stuff here
}
class Implementation : IImplementMe
{
IEnumerable Member;
}

Related

Class Keyword - Explicitly Indicating Inherited Interfaces?

SqlDataReader's class statement includes IDataReader, IDataRecord and IDisposable even though these are all implemented by its base class, DbDataReader:
public class SqlDataReader : DbDataReader,
IDataReader, IDisposable, IDataRecord {...}
public abstract class DbDataReader : MarshalByRefObject,
IDataReader, IDisposable, IDataRecord, IEnumerable {...}
In this case, is there some technical advantage to indicating that the derived class implements interfaces which its base class already indicates that it implements? (I can't think of one. Wondering if this is a legacy relic, a typo or something done for documentation purposes.)
This could be done in order to add or override explicit interface implementations in the derived class. For example,
interface IFoo
{
string P {get;}
}
class Base: IFoo
{
string IFoo.P
{
get { return "Base"; }
}
}
class Derived: Base, IFoo
{
string IFoo.P
{
get { return "Derived"; }
}
}
If Derived doesn't implement IFoo directly, it cannot define an explicit implementation of IFoo.P, so it cannot override the implementation in the base class.
This makes sense if you want to implement some interface explicitly.
For example:
interface ISome
{
void Method();
}
class A : ISome
{
public void Method()
{
}
}
class B : A, ISome // Try to remove ISome...
{
void ISome.Method()
{
}
}
If you comment out the ISome in the B declaration, compilation will fail.

c# inherits an Interface with a derivate class in a method's parameters

I have an abstract class like this:
public abstract class BaseClass
{
...
}
and then i have a lot of classes that derive from BaseClass:
public class DerivedOne : BaseClass
{
...
}
I need to implement an Interface that manage the possibility to implement a method that can uses alle the class derived from BaseClass like parameters:
public interface IErrorParser
{
List<string> ParseErrorMessage(BaseClass base);
}
At this point, if i try to implement a class starting from the interface, in this way
public class FirstParser: IErrorParser
{
public List<string> ParseErrorMessage(DerivedOne derived)
{
...
}
}
i receive the error:
FirstParser does not implement interface member
'IErrorParser.ParseErrorMessage(BaseClass)'
at this point i think that i need to use the generics... But i can't understand how...
Make the IErrorParser interface generic like this:
public interface IErrorParser<T> where T:BaseClass
{
List<string> ParseErrorMessage(string defaultMessage, T service);
}
And then you can implement it like this:
public class FirstParser: IErrorParser<DerivedOne>
{
public List<string> ParseErrorMessage(string defaultMessage, DerivedOne rerived)
{
...
}
}
This is because when you implement an interface you must exactly match all interface members (methods and properties) defined in it.
In your case you need to write
public class FirstParser: IErrorParser
{
public List<string> ParseErrorMessage(string defaultMessage, BaseClass service);
{
...
}
}
An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.
More info: https://msdn.microsoft.com/en-us/library/87d83y5b.aspx
You can create interface like
public interface IErrorParser<T> where T : BaseClass
{
List<string> ParseErrorMessage(string defaultMessage, T service);
}
If you define
public interface IErrorParser
{
List<string> ParseErrorMessage(BaseClass base);
}
you can implement it in a class using DerivedClass like this
public class FirstParser: IErrorParser
{
public List<string> ParseErrorMessage(BaseClass baseObj)
{
DerivedClass derived = (baseObj as DerivedClass);
if (derived == null)
{
// handle null value
}
...
}
}
Edit
base is a keyword and you should not use it as the name of an argument.

Generic Constraint to allow casting from interface to implementation

I have a small class that implements a dictionary that maps from the type of an interface to an implementation of that interface that extends from a base class. Unfortunately the abstract base class does not implement the interfaces, so once in the dictionary, there seems to be no way to associate the two. There is another method in this class that is dependent on storing the objects as BaseClass (in fact, most of my class is dependent on that--the getter into the dictionary is somewhat of a convenience).
private readonly Dictionary<Type, BaseClass> dictionary;
public void Add<T>(BaseClass base)
{
if (!(base is T)) // How to get rid of this check?
{
throw new ArgumentException("base does not implement " + typeof(T).Name);
}
this.dictionary.Add(typeof(T), base);
}
public T Get<T>()
{
BaseClass base;
this.dictionary.TryGetValue(typeof(T), out base);
return (T)(object)base; // How to get rid of (object) cast?
}
Are there any clever constraints I can use to remove the (base is T) check, the cast to object, or both?
Here is the class setup, for reference:
class BaseClass { }
interface IThing { }
class MyClass : BaseClass, IThing { }
dict.Add<IThing>(new MyClass());
IThing myClass = dict.Get<IThing>();
The only way to get the compile-time enforcement you're looking for would be if you have compile-type knowledge of the derived type being added.
For example, if you also specify a type parameter for the class being added then you could constrain that the class implement the interface type parameter:
public void Add<TInterface, TClass>(TClass #base)
where TClass : BaseClass, TInterface {
this.dictionary.Add(typeof(TInterface), #base);
}
So you could do this:
MyClass ok = new MyClass();
dict.Add<IThing, MyClass>(ok);
But not this:
class MyClassNotIThing : BaseClass { }
MyClassNotIThing notOk = new MyClassNotIThing();
dict.Add<IThing, MyClassNotIThing>(notOk);
Aside from that, generic constraints don't offer a means by which to constrain that a known type (i.e. BaseClass) inherit from a generic type parameter.
Here is the solution I ended up using. There are a few tricks that can make the Add() safe without the check (see the link in a comment to cokeman19's answer), but I opted not to do that as I find this code a bit cleaner.
interface IThing { }
abstract class BaseClass
{
internal T AsInterface<T> where T : class
{
return this as T;
}
}
class MyClass : BaseClass, IThing { }
class DictionaryClass
{
private readonly Dictionary<Type, BaseClass> dictionary;
public void Add<T>(BaseClass base)
{
if (base is T)
{
dictionary.Add(typeof(T), base);
}
}
public T Get<T>() where T : class
{
return dictionary[typeof(T)].AsInterface<T>();
}
}
Note that this solution does allow calls like:
myClass.AsInterface<IThingItDoesntImplement>()
but this returns null and I made the function internal to prevent strange uses anyway.

Calling specific descendant of interface in an implementation of another interface

I have an interface iClass defined. One method in the interface takes another interface, iObject, as an argument.
In one specific implementation of iClass, I need the method to take a specific implementation of iObject, ObjectImplementation - but C# tells me I need to implement the method as is.
Why is this? isn't ObjectImplementation an instance of iObject? How do I get around this? I tried using an abstract class instead and I get into the same mess.
public interface iClass {
bool SomeMethod(iObject object);
}
public interface iObject {
... // some methods here
}
public ObjectImplementation : iObject {
... // some method implementations here
}
public ClassImplementation : iClass {
public bool SomeMethod(ObjectImplementation object) // <- C# compiler yells at me
{
}
}
The contract clearly states that the method requires an iObject. ObjectImplementation is one class implementing this interface. But there might be others. The contract of iClass states that all those implementations are valid parameters.
If you really need to constrain the parameter to ObjectImplementation consider using a generic interface:
public interface IClass<T> where T : IObject
{
bool SomeMethod(T item);
}
public ClassImplementation : IClass<ObjectImplementation>
{
public bool SomeMethod(ObjectImplementation item)
{
}
}
Leaving the iObject as parameter is a way to go, this should also work:
public interface iClass {
bool SomeMethod(iObject obj);
}
public interface iObject {
}
public class ObjectImplementation : iObject {
}
public class ClassImplementation : iClass {
public bool SomeMethod(iObject obj)
{
return false;
}
}

C#: Generic implementation of method doesn't satisfy interface

In this post I talked about using a generic base class to enable me to create repository classes without duplicating loads of basic plumbing code.
Each Repository is accessed through an interface. In the code below, I will only show one of the methods for the sake of brevity:
Interface:
IQueryable<Suggestion> All { get; }
Generic base class
public IQueryable<T> All
{
get { return _unitOfWork.GetList<T>(); }
}
Concrete class (implements the interface and extends the generic base class)
public IQueryable<Suggestion> All
{
get { return _unitOfWork.GetList<Suggestion>(); }
}
I anticipated that I would be able to simply strip the method out of the concrete class, and the compiler would use the generic base class implementation instead and work out that was intended to satisfy the interface. But no!
When I strip the method out I get the old 'does not implement interface member' error.
If I can't do this, have my efforts to use a generic base class not been pointless? Or is there a way around this?
Can you make the interface itself generic then implement a typed version in your concrete class?
public interface IRepository<T>
{
List<T> All { get; }
}
public class Repository<T>
{
public List<T> All
{
get { return new List<T>(); }
}
}
public class SuggestionRepository : Repository<Suggestion>, IRepository<Suggestion>
{ }
I'd still suggest using the generic interface since it will save you from repeating yourself, but this works too.
public interface ISuggestionRepository
{
List<Suggestion> All { get; }
}
public class Repository<T>
{
public List<T> All
{
get { return new List<T>(); }
}
}
public class SuggestionRepository : Repository<Suggestion>, ISuggestionRepository
{ }
Use the virtual keyword and put your interface on your concrete implementation..
public interface IMyInterface<T>
{
IQueryable<T> All { get; }
}
public abstract class MyBaseClass<T> : IMyInterface<T>
{
public virtual IQueryable<T> All
{
get { return _unitOfWork.GetList<T>(); ; }
}
}
public class MyClass : MyBaseClass<Suggestion>, IMyInterface<Suggestion>
{
}

Categories

Resources