C# sted IEnumerables,您可以在完成后对每个元素执行任何操作 internal static class ExtensionMethods { public static IEnumerable<T> SelectManyRecusive<T>(this IEnumerable enumerable) { foreach (var item in enumerable) { var castEnumerable = item as IEnumerable; if (castEnumerable != null && ((typeof(T) != typeof(string)) || !(castEnumerable is string))) //Don't split string to char if string is our target { foreach (var inner in SelectManyRecusive<T>(castEnumerable)) { yield return inner; } } else { if (item is T) { yield return (T)item; } } } } } 内部静态类扩展方法 { 公共静态IEnumerable SelectManyRecuctive(此IEnumerable可枚举) { foreach(可枚举中的变量项) { var castEnumerable=项目为IEnumerable; 如果(可数!=null &&((typeof(T)!=typeof(string))| |!(castEnumerable是string))//如果string是我们的目标,不要将string拆分为char { foreach(SelectManyRecuctive中的内部变量(可计算)) { 内部收益率; } } 其他的 { 如果(项目为T) { 收益回报(T)项; } } } } }
还有一个我遇到的bug,我想可能会影响我回答的第一部分,C# sted IEnumerables,您可以在完成后对每个元素执行任何操作 internal static class ExtensionMethods { public static IEnumerable<T> SelectManyRecusive<T>(this IEnumerable enumerable) { foreach (var item in enumerable) { var castEnumerable = item as IEnumerable; if (castEnumerable != null && ((typeof(T) != typeof(string)) || !(castEnumerable is string))) //Don't split string to char if string is our target { foreach (var inner in SelectManyRecusive<T>(castEnumerable)) { yield return inner; } } else { if (item is T) { yield return (T)item; } } } } } 内部静态类扩展方法 { 公共静态IEnumerable SelectManyRecuctive(此IEnumerable可枚举) { foreach(可枚举中的变量项) { var castEnumerable=项目为IEnumerable; 如果(可数!=null &&((typeof(T)!=typeof(string))| |!(castEnumerable是string))//如果string是我们的目标,不要将string拆分为char { foreach(SelectManyRecuctive中的内部变量(可计算)) { 内部收益率; } } 其他的 { 如果(项目为T) { 收益回报(T)项; } } } } },c#,generics,extension-methods,reflection,ienumerable,makegenericmethod,C#,Generics,Extension Methods,Reflection,Ienumerable,Makegenericmethod,还有一个我遇到的bug,我想可能会影响我回答的第一部分,字符串在技术上是一个IEnumerable,因此IEnumerable也可以被看作是一个IEnumerable,它可能会放入太多的,。第二个版本对此进行了检查 演示如何使用此方法和字符串。将连接在一起。有帮助吗?您正在寻找单个嵌套的IEnumerable解决方案吗?嗨@YuvalItzchakov,我正在寻找的是一个无限的解决方案,嵌套可以更深。这对单个嵌套的IEnumerable有效。如果嵌套更深入,这将不起作用。我认为这段代码无法通过编
字符串
在技术上是一个IEnumerable
,因此IEnumerable
也可以被看作是一个IEnumerable
,它可能会放入太多的,
。第二个版本对此进行了检查
演示如何使用此方法和
字符串。将连接在一起。有帮助吗?您正在寻找单个嵌套的IEnumerable
解决方案吗?嗨@YuvalItzchakov,我正在寻找的是一个无限的解决方案,嵌套可以更深。这对单个嵌套的IEnumerable
有效。如果嵌套更深入,这将不起作用。我认为这段代码无法通过编译器,我需要在Join上定义一个非泛型版本才能使其起作用。谢谢,虽然它不是“泛型的”,但对于ToString问题,它确实很好且简单enough@huangcd你说的“这不是一般的”是什么意思?@huangcd我添加了一个更新,该更新提供了一个类似的方法,该方法将递归地沿着IEnumerable链,将其展平为单个类型的IEnumerable
。它就像linq方法一样,过滤掉不匹配的项目。谢谢@ScottChamberlain!解决了我的问题
public static String Join(this IEnumerable enumerable)
{
var enumerable2 = enumerable as IEnumerable<IEnumerable>;
if (enumerable2 != null)
{
return String.Join(",", enumerable2.Select(e => e.Join()));
}
return String.Join(",", enumerable.Select(e => e.ToString()));
}
public static String Join(this IEnumerable enumerable)
{
var stringEnumerable = enumerable as IEnumerable<string>;
if (stringEnumerable != null)
{
return String.Join(",", stringEnumerable);
}
var enumerable2 = enumerable as IEnumerable<IEnumerable>;
if (enumerable2 != null)
{
return String.Join(",", enumerable2.Select(e => e.Join()));
}
return String.Join(",", enumerable.Select(e => e.ToString()));
}
static public Type[] ListeTypeArgumentZuBaseOderInterface(
this Type Type,
Type BaseGenericTypeDefinition)
{
if (null == Type || null == BaseGenericTypeDefinition)
{
return null;
}
if (BaseGenericTypeDefinition.IsInterface)
{
var MengeInterface = Type.GetInterfaces();
if (null != MengeInterface)
{
foreach (var Interface in MengeInterface)
{
if (!Interface.IsGenericType)
{
continue;
}
var InterfaceGenericTypeDefinition = Interface.GetGenericTypeDefinition();
if (!InterfaceGenericTypeDefinition.Equals(BaseGenericTypeDefinition))
{
continue;
}
return Interface.GenericTypeArguments;
}
}
}
else
{
var BaseTypeAktuel = Type;
while (null != BaseTypeAktuel)
{
if (BaseTypeAktuel.IsGenericType)
{
var BaseTypeGenericTypeDefinition = BaseTypeAktuel.GetGenericTypeDefinition();
if (BaseTypeGenericTypeDefinition.Equals(BaseGenericTypeDefinition))
{
return BaseTypeAktuel.GenericTypeArguments;
}
}
BaseTypeAktuel = BaseTypeAktuel.BaseType;
}
}
return null;
}
static public Type IEnumerableTypeArgumentExtrakt(
this Type TypeImplementingEnumerable)
{
var GenericTypeArguments =
ListeTypeArgumentZuBaseOderInterface(TypeImplementingEnumerable, typeof(IEnumerable<>));
if (null == GenericTypeArguments)
{
// does not implement IEnumerable<>
return null;
}
return GenericTypeArguments.FirstOrDefault();
}
public static String Join<T>(this IEnumerable<T> enumerable)
{
// ¡the typeof() has to refer to the class containing this Method!:
var SelfType = typeof(Extension);
var IEnumerableTypeArgument = IEnumerableTypeArgumentExtrakt(typeof(T));
if (null != IEnumerableTypeArgument)
{
System.Reflection.MethodInfo method = SelfType.GetMethod("Join");
System.Reflection.MethodInfo generic = method.MakeGenericMethod(IEnumerableTypeArgument);
return String.Join(",", enumerable.Select(e => generic.Invoke(null, new object[] { e })));
}
return String.Join(",", enumerable.Select(e => e.ToString()));
}
internal static class ExtensionMethods
{
public static String Join<T>(this IEnumerable<T> enumerable)
{
StringBuilder sb = new StringBuilder();
JoinInternal(enumerable, sb, true);
return sb.ToString();
}
private static bool JoinInternal(IEnumerable enumerable, StringBuilder sb, bool first)
{
foreach (var item in enumerable)
{
var castItem = item as IEnumerable;
if (castItem != null)
{
first = JoinInternal(castItem, sb, first);
}
else
{
if (!first)
{
sb.Append(",");
}
else
{
first = false;
}
sb.Append(item);
}
}
return first;
}
}
internal static class ExtensionMethods
{
public static IEnumerable<T> SelectManyRecusive<T>(this IEnumerable enumerable)
{
foreach (var item in enumerable)
{
var castEnumerable = item as IEnumerable;
if (castEnumerable != null
&& ((typeof(T) != typeof(string)) || !(castEnumerable is string))) //Don't split string to char if string is our target
{
foreach (var inner in SelectManyRecusive<T>(castEnumerable))
{
yield return inner;
}
}
else
{
if (item is T)
{
yield return (T)item;
}
}
}
}
}