I'm writing a abstract file parser (C#) which is extended by two concrete parsers. Both need to perform several checks. Currently there is a validate method in the abstract parser, which uses reflection to call all methods with a name starting with 'test'. That way adding checks is as easy as adding a method with a name that starts with 'test'.
Now recently I've had some comments about the use of reflection and it being better to use dynamic dispatching. My question to you is, why not use reflection and how would you implement this? Also how should I use dynamic dispatch to solve this problem?
public bool Validate()
{
bool combinedResult = true;
Type t = this.GetType();
MethodInfo[] mInfos = t.GetMethods();
foreach (MethodInfo m in mInfos)
{
if (m.Name.StartsWith("Check") && m.IsPublic)
{
combinedResult &= (bool)m.Invoke(this, null);
}
}
return combinedResult;
}