I have a generic method where <T> is defined as a certain type when a call to the method is made, the type that is passed is a custom object that incorporates the IEnumerable and IEnumerator interfaces and these work normally when iterating using foreach. When I attempt to iterate that object in the generic method is doesn't allow me and gives me this message >> "foreach statement cannot operate on variables of type 'T' because 'T' does not contain a public definition for 'GetEnumerator'". Note the reason for this method being generic is to cater for different RiskRecord structures.
Here is my code:
//Code to call the generic methods for the different structures are: 1) GenericSummaryResultComparison<PC.RiskRecord>(szRiskRecordFilePath1, szRiskRecordSchemaFilePath); 2) GenericSummaryResultComparison<GV.RiskRecord>(szRiskRecordFilePath1, szRiskRecordSchemaFilePath); 3) GenericSummaryResultComparison<GV.RiskRecord>(szRiskRecordFilePath1, szRiskRecordSchemaFilePath);
//Generic method private bool GenericSummaryResultComparison<T>(string szRiskRecordFilePath, string szRiskRecordSchemaFilePath) where T : class { try { T oRiskRecord;
//Read in the risk files. oRiskRecord = SerializationMethods.DeserializeIt<T>(szResultFilePath1, szRiskRecordSchemaFilePath);
foreach (T o in oRiskRecord) { // actions }
return true; } catch (Exception ex) { throw new ArgumentException(ex.Message, ex.InnerException); } }
How can I get the foreach to work for type T?
Thanks