I am using a recursive function for a object tree. That is, my object collection is like:
Object1
--Object2
----Object3
------Object4
All objects inherits from a base object (abstract class), where has a Validate() method, and its collection inherits from ITreeCollection. I have written a recursive function to perform that:
private bool Validate<T>(ITreeCollection items) where T : TreeNodeBase
{
foreach (var itm in items as Collection<T>)
{
if (((TreeNodeBase)itm).Items != null)
{
return Validate<T>(((TreeNodeBase)itm).Items);
}
else return true;
}
return true;
}
How can I derive the the type parameter T for the inner function (i.e. return Validate<T>(((TreeNodeBase)itm).Items))
Firstly, as it stands, you are not using the type parameter
Tso it could safely be removed. However I imagine you might want to do some type specific validation so this is perhaps not that helpful a suggestion. But, without example of what you want to do withTit’s difficult to make suggestions.Anyhow, here’s one approach of what I think you are trying to do:
You could get snazzy with the dynamic keyword and get rid of some of this type checking but it does get a bit confusing and I’d advise against it: