如果没有实现IEnumerable< T>的类,编译时是否可以知道元素类型? [英] Is it possible to know the element type at compile time without a class implementing IEnumerable<T>?

查看:78
本文介绍了如果没有实现IEnumerable< T>的类,编译时是否可以知道元素类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结

忽略一个对象不能表示为数组的情况,是否可以定义一个扩展(static )方法:

  public static? ToArray< TSource>(此TSource来源); 

并返回一个对象的数组,如果它包含任何元素的序列?如果是,那么会是什么?

  • 解释



    我曾考虑过以下声明:

      public static TElement [] ToArray< TElement>(this IEnumerable< TElement> source); 
    公共静态? ToArray< TSource>(这个IEnumerable来源);

    但我不能假设一个未知类必须实现 IEnumerable< T> / code>或 IEnumerable 。我甚至不能在 IEnumerable 这个超出泛型定义的情况下定义

    我还考虑过 Array 类:

      public static Array ToArray< TSource>(this TSource source); 

    但是这意味着元素类型在编译时是未知的。

    所以我想知道是否可以在编译时知道元素类型,而不需要实现 IEnumerable< T>

  • $ c>早于泛型,所以它没有类型信息;只是一个 IEnumerator GetEnumerator()使得 String 不那么吸引人去尝试强制进入你想要的这个通用实现完成。



    自从完成C#之后已经很长时间了,但我想象:

      public static TElement ToArray< TSource>(this IEnumerable< TElement> source); 

    ...应该没问题:

      public static TElement [] ToArray< TElement>(this IEnumerable< TElement> source); 

    关于扩展方法的重要一点是它们只是语法糖。他们实际上并没有呆在周围,或者被注入到他们装饰的类型中。它们在编译时被替换为静态方法调用。使用扩展方法的里程不同。


    • summary

      Ignore the case that an object cannot represented as an array, is it possible to define an extension(static) method like:

      public static ? ToArray<TSource>(this TSource source); 
      

      and returns the array of an object if it consists of whatever a sequence of elements? And if yes, what would the ? be?

    • explanation

      I've thought about the following declarations:

      public static TElement[] ToArray<TElement>(this IEnumerable<TElement> source); 
      public static ? ToArray<TSource>(this IEnumerable source); 
      

      But I cannot assume an unknown class must have implemented IEnumerable<T> or IEnumerable. I cannot even define the ? in the case it is just IEnumerable which is out of generic definition.

      And I've also thought about the Array class:

      public static Array ToArray<TSource>(this TSource source); 
      

      But that means the element type was unknown at compile time.

      So I'm wondering is it possible to know the element type at compile time without a class implementing IEnumerable<T>?

    解决方案

    Unfortunately, IEnumerator predates generics so it carries with it no type information; just a IEnumerator GetEnumerator() making String not so appealing to try and force into this generic implementation you're trying to accomplish.

    It's been a long time since I've done C# but I imagine:

    public static TElement ToArray<TSource>(this IEnumerable<TElement> source);
    

    ... should be fine as:

    public static TElement[] ToArray<TElement>(this IEnumerable<TElement> source);
    

    The important thing to keep in mind about extension methods is they are just syntax sugar. They don't actually stick around or become injected into the types they are decorating. They are replaced at compile time to static method invocations. Your mileage with extension methods will vary.

    这篇关于如果没有实现IEnumerable&lt; T&gt;的类,编译时是否可以知道元素类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆