Calling inheriting class methods via interface.
Posted
by Stacey
on Stack Overflow
See other posts from Stack Overflow
or by Stacey
Published on 2010-05-25T17:26:02Z
Indexed on
2010/05/25
17:31 UTC
Read the original article
Hit count: 153
c#
Given the scenario...
interface IBase{
void Process(int value);
}
abstract class Base : IBase
{
public virtual void Process(int value){ throw new NotImplementedException(); }
}
class Implemented: Base, IBase
{
public void Process(int value)
{
// .. some code here..
}
}
I'm trying to write a loop similar to the following.
foreach( Base b in CollectionOfImplemented )
{
b.Process( // something will go here // );
}
Trying this, it keeps calling Base.Process, instead of Implemented.Process; but the type in the collection is Implemented, not Base. Boxing it seems to work, but I was hoping to see if I could find a more intelligent approach to it, since the Collection will contain other types of objects that also inherit from Base.
© Stack Overflow or respective owner