开发者

What combination of generic parameter constraints will make the C# compiler generate rows for the GenericParamConstraint metadata table?

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 Progra开发者_开发技巧m
    {
        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?


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

What combination of generic parameter constraints will make the C# compiler generate rows for the GenericParamConstraint metadata table?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜