I am beginner.Practicing in C# 3.0. The requirement is "i have to design model in such a way that ,it should iterate through all classes which implement particular interface
(in this case IExpressWords
) and execute the implemented method (void ExpressWords()
)"
I collected all classes in a List and iterated.
namespace InterfaceExample
{
public interface IExpressWords
{
void ExpressWords();
}
class GroupOne:IExpressWords
{
string[] str = { "Good", "Better", "Best" };
public void ExpressWords()
{
foreach (string s in str)
{
Console.WriteLine(s);
}
}
}
class GroupTwo:IExpressWords
{
string[] str = { "one", "two", "three" };
public void ExpressWords()
{
foreach (string s in str)
{
Console.WriteLine(s);
}
}
}
class Test
{
static void Main()
{
List<IExpressWords> word = new List<IExpressWords>();
word.Add(new GroupOne());
word.Add(new GroupTwo());
foreach (IExpressWords Exp in word)
{
Exp.ExpressWords();
}
Console.ReadKey(true);
}
}
}
Questions :
- What is the name of this pattern ? ( chain-of-responsibility? )
- How can i achieve it using delegates ( I am not strong in delegates).
- How can i find out all classes that implement the interface and execute the method using reflection ?(Curious to know ,how to tackle it using reflection).
(If i am not so clear in description,kindly let me know).
Thanks all for the pouring perennial responses.