包含ValueTuple的结构如何满足非托管约束,但ValueTuple本身不能满足?

编程入门 行业动态 更新时间:2024-10-27 04:28:15
本文介绍了包含ValueTuple的结构如何满足非托管约束,但ValueTuple本身不能满足?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请考虑以下类型:

  • (int, int)→受管理.
  • struct MyStruct { public (int,int) Value; }→不受管理!
  • (int, int) → managed.
  • struct MyStruct { public (int,int) Value; } → unmanaged!

问题:具有托管成员(int,int)的非通用结构MyStruct已被评估为托管类型.

Problem: A non-generic structure MyStruct, which has a managed member (int,int) has been evaluated as managed type.

预期的行为:包含托管成员的结构应被视为托管,与struct MyStruct { int? Value; }被视为托管的方式相同.

Expected Behavior: A structure which contains a managed member, should be considered as managed, the same way the struct MyStruct { int? Value; } are considered as managed.

这两种类型似乎都违反了文档"> [1] 和 [2] .

It seems both types are behaving against the documentations [1] and [2].

示例1-不受约束的约束

class Program { static void DoSomething<T>() where T : unmanaged { } struct MyStruct { public (int, int) Value; } static void Main(string[] args) { DoSomething<MyStruct>(); // → OK DoSomething<(int, int)>(); // → Shows compile-time error } }

错误CS8377类型'(int,int)'必须是不可为空的值类型, 以及处于任何嵌套级别的所有字段,以便将其用作 通用类型或方法"Program.DoSomething()"中的参数"T"

Error CS8377 The type '(int, int)' must be a non-nullable value type, along with all fields at any level of nesting, in order to use it as parameter 'T' in the generic type or method 'Program.DoSomething()'

示例2-指针或sizeof

使用上述结构,指针或 sizeof 操作符:

Using above structure, the behavior is the same for pointers or sizeof operator:

unsafe { (int, int)* p1; // → Compile-time error, MyStruct* p2; // → Compiles }

错误CS0208无法获取其地址,获取其大小或声明一个 指向托管类型的指针('(int,int)')

Error CS0208 Cannot take the address of, get the size of, or declare a pointer to a managed type('(int, int)')

问题

  • 包含ValueTuple的结构如何被视为unmanaged,并且在ValueTuple被视为托管的情况下如何满足unmanaged约束?

  • How do a struct containing ValueTuple is considered as unmanaged and can satisfy unmanaged constraint while the ValueTuple is considered as managed?

    具有ValueTupple<T1, T2>的结构和包含Nullable<T>的结构如何区别对待?

    How a struct having ValueTupple<T1, T2> and a struct containing Nullable<T> are treated differently?

    注1: IMO,该问题不同于建议:非托管构造类型(在注释中由DavidG解决),因为MyStruct不是通用的,而同时int?和(int,int)都是托管的,而struct MyStruct { int? Value; }和struct MyStruct { (int, int) Value; }评估结果不同.

    Note 1: IMO the issue is different from the Proposal: Unmanaged constructed types (addressed by DavidG in comments), because MyStruct is not generic, on the other hand while int? and (int,int) both are managed, but struct MyStruct { int? Value; } and struct MyStruct { (int, int) Value; } evaluated differently.

    推荐答案

    感谢您的举报.这只是编译器中的一个错误.元组用作字段时应注册为通用类型,因此在unmanaged type中无效.它似乎被评估为草木,但缺少此检查.

    Thanks for reporting. This is just a bug in the compiler. The tuple when used as a field should be registering as a generic type and hence invalid in an unmanaged type. It appears to be evaluating as a tulpe instead and missing this check.

    好消息是,在C#8.0中,此限制将消失.类型(int, int)是有效的unmanaged type.

    Good news is that in C# 8.0 this restriction will be going away. The type (int, int) is a valid unmanaged type.

  • 更多推荐

    包含ValueTuple的结构如何满足非托管约束,但ValueTuple本身不能满足?

    本文发布于:2023-11-05 19:25:50,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1561648.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:结构   ValueTuple

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!