I have a generic method where
Here is my code:
//Code to call the generic methods for the different structures are:
1) GenericSummaryResultComparison
2) GenericSummaryResultComparison
3) GenericSummaryResultComparison
//Generic method
private bool GenericSummaryResultComparison
{
try
{
T oRiskRecord;
//Read in the risk files.
oRiskRecord = SerializationMethods.DeserializeIt
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

VulpesRank #55 · 29.2K reputationPosted Jun 8, 2011, 3:04 AM EDT
For example if T implements the non-generic interfaces IEnumerator and IEnumerable, you can do:
where T:class, IEnumerator, IEnumerable
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Jun 8, 2011, 3:04 AM EDT
Try this,
for(var o in oRiskRecord)
{
//Do stuff
}
Mark Maguire17 reputationPosted Jun 8, 2011, 3:35 AM EDT
"To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. SCYLLAClientEngine.DataStructures.Motor.SharedStructures.ResultSet.ResultSet does not implement Add(System.Object)."
How do I implement Add(System.Object).
Your helps much appreciated.
Thanks
VulpesRank #55 · 29.2K reputationPosted Jun 8, 2011, 4:14 AM EDT
I imagine it's some sort of collection so presumably you have a method for adding elements though it may be strongly typed.
If so, could you add an overload which takes an object as a parameter, casts it to the appropriate class and then calls your other Add method?