.NET 泛型术语 - 打开/关闭、未绑定(bind)/构造

标签 .net generics

.NET 泛型术语有点含糊。更糟糕的是——它似乎在不同的来源中被模棱两可地使用。基本上不清楚的是这四个术语之间的关系(与“类型”有关):

  • 打开
  • 关闭
  • 未绑定(bind)
  • build

  • 我了解 List<T>已打开,List<int>已经关闭。但是,相对于开放/封闭类型,真正的“构造”和“未绑定(bind)”是什么?

    最佳答案

    来自 language specification :

    4.4 Constructed types

    A generic type declaration, by itself, denotes an unbound generic type that is used as a “blueprint” to form many different types, by way of applying type arguments. The type arguments are written within angle brackets (< and> ) immediately following the name of the generic type. A type that includes at least one type argument is called a constructed type. A constructed type can be used in most places in the language in which a type name can appear. An unbound generic type can only be used within a typeof-expression (§7.6.11). [...]

    4.4.2 Open and closed types

    All types can be classified as either open types or closed types. An open type is a type that involves type parameters. More specifically:

    • A type parameter defines an open type.

    • An array type is an open type if and only if its element type is an open type.

    • A constructed type is an open type if and only if one or more of its type arguments is an open type. A constructed nested type is an open type if and only if one or more of its type arguments or the type arguments of its containing type(s) is an open type.

    A closed type is a type that is not an open type. [...]

    4.4.3 Bound and unbound types

    The term unbound type refers to a non-generic type or an unbound generic type. The term bound type refers to a non-generic type or a constructed type. An unbound type refers to the entity declared by a type declaration. An unbound generic type is not itself a type, and cannot be used as the type of a variable, argument or return value, or as a base type. The only construct in which an unbound generic type can be referenced is the typeof expression (§7.6.11).



    这是我想到的一个例子:
    // Foo<T> is an unbound generic type.
    class Foo<T> { .. } 
    
    // Bar<K> is an unbound generic type.
    // Its base-class Foo<K> is a constructed, open generic type.
    class Bar<K> : Foo<K> { .. } 
    
    // IntFoo is not a generic type.
    // Its base-class Foo<int> is a constructed, closed generic type.
    class IntFoo : Foo<int> { .. } 
    

    这里尝试使用相关属性将其与反射 API 联系起来:IsGenericType , IsGenericTypeDefinitionContainsGenericParameters(根据语言规范,这些测试并不是 100% 预测每种“种类”)。
    +----------+---------------------+-----------+--------------+-------------------+
    |   Name   |        Kind         | IsGenType | IsGenTypeDef | ContainsGenParams |
    +----------+---------------------+-----------+--------------+-------------------+
    | Foo<>    | Unbound             | TRUE      | TRUE         | TRUE              |
    | Foo<>*   | Constructed, open   | TRUE      | FALSE        | TRUE              |
    | Foo<int> | Constructed, closed | TRUE      | FALSE        | FALSE             |
    | IntFoo   | Not generic         | FALSE     | FALSE        | FALSE             |
    +----------+---------------------+-----------+--------------+-------------------+
    * = Bar<>'s base type.
    

    关于.NET 泛型术语 - 打开/关闭、未绑定(bind)/构造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4876622/

    相关文章:

    c# - 二维数组索引位置 "touching"

    c# - 我们如何在 VC++ 中使用 .NET dll?

    entity-framework - 使用动态构建的表达式过滤非通用 DbSet

    c# - Func<T> 的性能和继承

    generics - 是否可以在 ‘T is constrained to be a tuple of type ' T2*'T3 处编写一个 genericFunction<'T>?

    .net - 无法加载文件或程序集 'System.IO.Pipelines'

    c# - 以图形方式显示正在执行的代码? 。网

    c# - ImageMagick .net c# mp4/webm 视频在没有伪影的情况下获得 N 帧?

    java - 包装 jasper 报告导出器时出现问题

    Java 泛型与 ArrayList <?扩展 A> 添加元素