I'm trying to refactor some code but I'm not sure if I'm going about it the right way.
Below is a simplified switch where I'm creating an observable collection based on 2 parameters passed to a generic function. The first parameter is always an observable collection and the second is the type of the object stored in the observable collection:
switch (csvDataType)
{
case CsvDataTypeEnum.Supplier:
dataCollection = ConverterService.ConvertCsvDataToCollection
<SupplierCollection, Supplier>(csvData);
break;
case CsvDataTypeEnum.Currency:
dataCollection = ConverterService.ConvertCsvDataToCollection
<CurrencyCollection, Currency>(csvData);
break;
case CsvDataTypeEnum.Custom:
dataCollection = ConverterService.ConvertCsvDataToCollection
<CustomDataObjectCollection, CustomDataObject>(csvData);
break;
}
Based on the above, I was hoping to refactor the code to something similar to this:
ObjectInfo objectInfo = new ObjectInfo(csvDataType);
Type objectType = objectInfo.ObjectType;
Type collectionType = objectInfo.CollectionType;
dataCollection = ConverterService.ConvertCsvDataToCollection
<collectionType, objectType>(csvData);
My ConvertCsvDataToCollection
generic function is defined as follows:
public static U ConvertCsvDataToCollection<U, T>(string csvData)
where U : ObservableCollection<T>
{
....
}
I then use an implicit type using var
:
var dataCollection = Activator.CreateInstance(collectionType);
and it create the correct observable collection of the required type but I can't get my generic function to take in the Type collectionType and objectType types as I was hoping for by calling it this way:
dataCollection = ConverterService.ConvertCsvDataToCollection
<collectionType, objectType>(csvData);
but I get the following error:
the 'type or namespace name 'collectionType' could not be found. Are you missing a directive or assembly reference'
for both the collectionType and the objectType parameters I'm passing to my generic function.
Is there a way to achieve this? I know this may have been previously asked in the past but I need clarification as the other similar issue I've read so far are still not addressing my problem, or I've totally missed their points. Sorry if that's the case!
Thanks.