Abrast or interface static property

  • Thread starter Thread starter ACalafiore
  • Start date Start date
A

ACalafiore

Guest
Hi experts,

I'm implementing a parser using regular expressions and I have different type of rows that i need to analyze differently.

eg:
LINE 16,0,0,0
LINE 1,1,0,0,-1,"9052"
LINE 1,0,1,0,-1,"9046"
LINE 8,0,0,1,0,0,4000,0,0,
"1","2","3"
LINE 11,0,0,0,4,
"1","2","T4V7Z3","5Y6"
LINE 7,0,0,2,0
LINE 7,0,0,3,0
LINE 7,0,0,4,0
LINE 7,0,0,5,0

To do so my idea was to implement different classes using the same interface and/or abstract class (IRow and/or ARow) and to create the correct one using a factory method. To choose which class instantiate I thought about using a different const called Regex with different implementations in each of the differect classes implementing the interface or abstract class and checking it using Reflection to get the const string and then call the constructor.

public TMACParser(string filename) : base(filename)
{
_iMacroRowsTypes = new Dictionary<string, ConstructorInfo>();

//I get all types implementing the correct interface or abstract class
List<Type> iMacroRowsType = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).Where(x => typeof(IMacroRow).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract).ToList();

foreach(Type type in iMacroRowsType)
{
//I save the regex string and the constructor so i can check the regex and eventually call the associated constructor
_iMacroRowsTypes.Add((string)type.GetField("Regex").GetValue(null), type.GetConstructor(new Type[] { typeof(Match) }));
}


The problem is that I don't know how to force this inside the interface or the abstract class and I need a static property to check it before instantiating the correct object.

I know I can implement a specific const inside each of the subclasses but it's not very clean as a method. Is there another way?

Continue reading...
 
Back
Top