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
VulpesPosted Jun 8, 2011, 8:14 AM
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?
Mark MaguirePosted Jun 8, 2011, 7:35 AM
"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
Jaganathan BantheswaranPosted Jun 8, 2011, 7:04 AM
Try this,
for(var o in oRiskRecord)
{
//Do stuff
}
VulpesPosted Jun 8, 2011, 7:04 AM
For example if T implements the non-generic interfaces IEnumerator and IEnumerable, you can do:
where T:class, IEnumerator, IEnumerable