哪种泛型参数约束组合将使 C# 编译器为 GenericParamConstraint 元数据表生成行?

发布于 2024-10-31 02:25:54 字数 959 浏览 6 评论 0原文

根据 CLR GenericParamConstraint 表的 Partition II 元数据规范,

GenericParamConstraint 表 记录每个的约束 通用参数。每个通用 参数可以被约束来导出 从零个或一个班级开始。每个通用 参数可以限制为 实现零个或多个接口。

问题是我似乎无法找到正确的 C# 代码片段来让 C# 编译器在 GenericParamConstraint 表中生成一行。我尝试过使用

    public interface IFoo
    {
    }

    public interface IBaz
    {
    }

    public interface IBar
    {
    }
    public class Foo
    {

    }
    public class SampleClassWithGenericParamConstraint<T>
        where T : IFoo, IBaz, new()
    {
        public void DoSomething<U>(U arg1)
            where U : struct
        {
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
        }
    }

.. 但问题是 C# 编译器只为 GenericParam 表生成行,而不是为 GenericParamConstraint 表添加行。所以这是我的问题 - 是否有任何用 C# 编写的代码示例会强制 C# 编译器向 GenericParamConstraint 表添加元数据行,以便我可以使用示例二进制文件?如果这是 C# 编译器方面的某种限制,是否有等效的 ILASM 代码片段可以替代这项工作?

According to the Partition II metadata specification for the GenericParamConstraint table for the CLR,

The GenericParamConstraint table
records the constraints for each
generic parameter. Each generic
parameter can be constrained to derive
from zero or one class. Each generic
parameter can be constrained to
implement zero or more interfaces.

The problem is that I can't seem to find the right C# code snippet to have the C# compiler generate a single row in the GenericParamConstraint table. I have tried using

    public interface IFoo
    {
    }

    public interface IBaz
    {
    }

    public interface IBar
    {
    }
    public class Foo
    {

    }
    public class SampleClassWithGenericParamConstraint<T>
        where T : IFoo, IBaz, new()
    {
        public void DoSomething<U>(U arg1)
            where U : struct
        {
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
        }
    }

..but the problem is that the C# compiler only generates rows for the GenericParam table instead of adding rows for the GenericParamConstraint table. So here's my question--is there any code sample written in C# that would force the C# compiler to add a metadata row to the GenericParamConstraint table so that I could use the sample binary? And if this is some sort of limitation on the C# compiler's part, is there an equivalent ILASM snippet that would do the job instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一江春梦 2024-11-07 02:25:54

您一定很困惑,您发布的使用 csc 编译的确切代码会在 GenericParamConstraint 表中产生三行。

在此处输入图像描述

在此上下文中,GenericParam U 的令牌为 2a000001,并且是受 System.ValueType TypeRef 约束。 T (2a00000a) 受 IFoo 和 IBaz TypeDef 约束。

You must be confused, the exact code you posted, compiled with csc, results in three rows in the GenericParamConstraint table.

enter image description here

In this context, the GenericParam U has a token of 2a000001, and is constrained on the System.ValueType TypeRef. T (2a00000a) is constrained on the IFoo and IBaz TypeDefs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文