I'm trying to dynamically create sort clauses by reading in an XML configuration file.
The XML file is as follows:
<SortConfiguration>
<Entity Name="User" Default="LastName">
<SortType Name="LastName">
<SortClause FieldName="LastName" SortOperator="Ascending"/>
<SortClause FieldName="FirstName" SortOperator="Ascending"/>
<SortClause FieldName="MiddleName" SortOperator="Ascending"/>
</SortType>
<SortType Name="LastNameDescending">
<SortClause FieldName="LastName" SortOperator="Descending"/>
<SortClause FieldName="FirstName" SortOperator="Descending"/>
<SortClause FieldName="MiddleName" SortOperator="Descending"/>
</SortType>
<SortType Name="Birthdate">
<SortClause FieldName="Birthdate" SortOperator="Ascending"/>
<SortClause FieldName="LastName" SortOperator="Ascending"/>
<SortClause FieldName="FirstName" SortOperator="Ascending"/>
<SortClause FieldName="MiddleName" SortOperator="Ascending"/>
</SortType>
<SortType Name="BirthdateDescending">
<SortClause FieldName="Birthdate" SortOperator="Ascending"/>
<SortClause FieldName="LastName" SortOperator="Descending"/>
<SortClause FieldName="FirstName" SortOperator="Descending"/>
<SortClause FieldName="MiddleName" SortOperator="Descending"/>
</SortType>
</Entity>
</SortConfiguration>
Of course, there are going to be many entities with many different sort configurations.
I've managed to use reflection to find the correct entity, enum, and selection. My problem is that now I can't take that enum and pass it into the SortClauseFactory to create the proper sort clause. I get a compiler error: "Argument '1': cannot convert from 'object' to 'UserFieldIndex'. I've managed to get this far and now I'm stuck.
Here is the section my code that performs the reflection. It will not compile and you probably have to step through the debugger to see what's going on:
ISortExpression sorter = new SortExpression();
// The field name to sort by
string fieldName = reader.GetAttribute('FieldName');
// Determine the correct sort operator
SortOperator sortOperator;
if (reader.GetAttribute('SortOperator').Equals('Ascending')) {
sortOperator = SortOperator.Ascending;
}
else {
sortOperator = SortOperator.Descending;
}
// Use reflection to find the field index enum
Assembly dataAssembly = Assembly.LoadFrom('Data.dll');
Type fieldIndexType = dataAssembly.GetType('Data.' + entityName + 'FieldIndex');
FieldInfo fieldIndexSelection = fieldIndexType.GetField(fieldName);
sorter.Add(SortClauseFactory.Create(Enum.Parse(fieldIndexType, fieldName),
sortOperator));
Note: Double quotes were replaced with single quotes to render correctly in the forum. Looping and control flow elements were removed for clarity.
Any thoughts as to how I can solve this problem?
Should the SortClauseFactory contain a more generalized overload?
Thanks!