I'm offering a more object-oriented solution to your problem. Read about double-dispatch design pattern. In your case this goes like this:
1) I'd remove the static methods. You could adapt this to use the static processors but this is how I would do it:
public interface IFieldProcessors
{
string DoWork(TextFieldType fieldType);
string DoWork(FieldTypeBase fieldType);
}
public class FieldProcessors : IFieldProcessors
{
public string DoWork(TextFieldType fieldType) => "DoWork[TextFieldType]";
public string DoWork(FieldTypeBase fieldType) => "DoWork[error:NoWorkerDefined]";
}
2) Add a Dispatch method to your field types. Note that this is where the magic happens as for each field type the compiler knows exactly what processor method to call because it knows the type of
this
. For unknown field it defaults to the base class as there is no more specific overload.
public abstract class FieldTypeBase
{
public abstract string Dispatch(IFieldProcessors processors);
}
public class TextFieldType : FieldTypeBase
{
public override string Dispatch(IFieldProcessors processors)
{
return processors.DoWork(this);
}
}
public class UnknownFieldType : FieldTypeBase
{
public override string Dispatch(IFieldProcessors processors)
{
return processors.DoWork(this);
}
}
3) Test that the correct method is called:
var field1 = new TextFieldType();
var field2 = new UnknownFieldType();
var processors = new FieldProcessors();
Console.WriteLine(field1.Dispatch(processors));
Console.WriteLine(field2.Dispatch(processors));
The output is like this:
DoWork[TextFieldType]
DoWork[error:NoWorkerDefined]