Last active
March 14, 2016 10:41
-
-
Save lbargaoanu/a2deff11b8040d65b81b to your computer and use it in GitHub Desktop.
Query inherited interfaces to find member #1182
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var interfaceType = typeof(IQueryableInterface); | |
new[]{interfaceType} | |
.RecursiveSelect(i=>i.GetTypeInfo().ImplementedInterfaces) | |
.Distinct() | |
.Select(i=>i.GetMember("PropertyInterface").FirstOrDefault()) | |
.FirstOrDefault(m=>m!=null) | |
.Dump(); | |
typeof(IQueryableInterface).GetMember("PropertyInterface").Dump(); | |
} | |
public interface IBaseBaseQueryableInterface | |
{ | |
} | |
public interface IBaseQueryableInterface : IBaseBaseQueryableInterface | |
{ | |
IPropertyInterface PropertyInterface { get; set; } | |
} | |
public interface IQueryableInterface : IBaseQueryableInterface | |
{ | |
string AnotherProperty { get; set; } | |
} | |
public interface IPropertyInterface | |
{ | |
int PropertyInterfaceId { get; set; } | |
} | |
public class QueryableInterfaceImpl : IQueryableInterface | |
{ | |
public IPropertyInterface PropertyInterface { get; set; } | |
public string AnotherProperty { get; set; } | |
} | |
public class PropertyInterfaceImpl : IPropertyInterface | |
{ | |
public int PropertyInterfaceId { get; set; } | |
} | |
public class QueryableDto | |
{ | |
public int PropertyInterfaceId { get; set; } | |
public string AnotherProperty { get; set; } | |
} | |
public static class MyExtensions | |
{ | |
public static IEnumerable<TSource> RecursiveSelect<TSource>(this IEnumerable<TSource> source, | |
Func<TSource, IEnumerable<TSource>> childSelector) | |
{ | |
return RecursiveSelect(source, childSelector, element => element); | |
} | |
public static IEnumerable<TResult> RecursiveSelect<TSource, TResult>(this IEnumerable<TSource> source, | |
Func<TSource, IEnumerable<TSource>> childSelector, Func<TSource, TResult> selector) | |
{ | |
return RecursiveSelect(source, childSelector, (element, index, depth) => selector(element)); | |
} | |
public static IEnumerable<TResult> RecursiveSelect<TSource, TResult>(this IEnumerable<TSource> source, | |
Func<TSource, IEnumerable<TSource>> childSelector, Func<TSource, int, TResult> selector) | |
{ | |
return RecursiveSelect(source, childSelector, (element, index, depth) => selector(element, index)); | |
} | |
public static IEnumerable<TResult> RecursiveSelect<TSource, TResult>(this IEnumerable<TSource> source, | |
Func<TSource, IEnumerable<TSource>> childSelector, Func<TSource, int, int, TResult> selector) | |
{ | |
return RecursiveSelect(source, childSelector, selector, 0); | |
} | |
private static IEnumerable<TResult> RecursiveSelect<TSource, TResult>(this IEnumerable<TSource> source, | |
Func<TSource, IEnumerable<TSource>> childSelector, Func<TSource, int, int, TResult> selector, int depth) | |
{ | |
return source.SelectMany((element, index) => Enumerable.Repeat(selector(element, index, depth), 1) | |
.Concat(RecursiveSelect(childSelector(element) ?? Enumerable.Empty<TSource>(), | |
childSelector, selector, depth + 1))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment