Generics and Implementation Help

Hello fine people of the CSharp Subreddit.

I am relatively new to C# but I'm "getting there", in that my code is no longer monstrously bad it is now simply just "bad".

I'm going through some of my older code to see where I can refactor or improve it and have decided now is the time to tackle generics.

I have at the moment five different classes that are doing a very similar job so a lot of code is replicated. The class structure looks like this:

public class BackupHelpers { public static tbl_Backup_Monitoring SubGenerator(List<DateTime> listOfDates, tbl_Backup_Monitoring resultModel, List<tbl_BackupChecks_WindowsServerBackup> listToProcess) 

The output is the same type (tbl_backup_monitoring) each time and the only change between the five different functions is the List<tbl_BackupChecks_WindowsServerBackup>

So to my mind this seems like the perfect place to change it to a generic method.

Can anyone give me an example on how to implement this in the class?

When I change List<tbl_BackupChecks_WindowsServerBackup> to List<T>, I can't access any of its properties using listToProcess.

All though the methods are largely identical, It's not quite clear either how I would go about checking the typeof to run code specific for that particular T. I'm assuming similar to below?:

if (typeof(T) == tbl_BackupChecks_WindowsServerBackup) { // Code Here } 

Any help appreciated!

by TNTGav via /r/csharp

Leave a Reply