c# - How to cast generic parameter to generic interface? -


i want have extension method print contents of ienumerable< x >

public static ienumerable<t> log_elements<t>(this ienumerable<t> collection, bool recursive = true) {     form1.log("[");     foreach (t in collection)         if(recursive&&(i.gettype().getgenerictypedefinition()==typeof(ienumerable<>)))             (ienumerable<>)i.log_elements();//this doesn't work         else             form1.log(i);     form1.log("]");     return collection; } 

if contains ienumerable< y >, method should called too.

i cannot add log_elements<t>(this ienumerable<ienumerable<t>>,bool) because ienumerable<t> matches t of original method.

i'm sure, there should solution such simple problem in c#.

change ienumerable<t> non generic ienumerable (which generic version inherits anyway).

public static ienumerable<t> log_elements<t>(this ienumerable<t> collection, bool recursive = true) {    logelementsinternal(collection, recursive);    return collection; }  private static void logelementsinternal(ienumerable collection, bool recursive) {     form1.log("[");     foreach (var in collection)         if(recursive && ienumerable)             logelementsinternal((ienumerable)i);         else             form1.log(i);     form1.log("]"); } 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -