简体   繁体   English

作为通用值的结构数组

[英]Array of structs as Generic value

I am trying to use an array of structs as a generic parameter as follows: 我试图使用结构体数组作为通用参数,如下所示:

new Test<S[,]>()

This is easy, except for my class needs to create an instance of T as follows: 这很容易,除了我的班级需要创建T的实例如下:

T s = new T();

This is giving me various compile-time errors. 这给了我各种编译时错误。 I have tried a few ways of getting around these errors, but without success. 我尝试了几种解决这些错误的方法,但是没有成功。

Here is some sample code to illustrate the errors and what I have tried: 这是一些示例代码来说明错误和我尝试过的操作:

class Program
{
    static void Main(string[] args)
    {
        var t1 = new Test<S[,]>();

        // ERROR: 'Program.S[*,*]' must be a non-abstract type
        // with a public parameterless constructor in order to
        // use it as parameter 'T' in the generic type or method
        // 'Program.Test2<T>'
        var t2 = new Test2<S[,]>();

        // ERROR: 'Program.S[*,*]' must be a non-nullable value
        // type in order to use it as parameter 'T' in the generic
        // type or method 'Program.Test3<T>'
        var t3 = new Test3<S[,]>();

        // ERROR: 'Program.S[*,*]' must be a non-nullable value
        // type in order to use it as parameter 'T' in the generic
        // type or method 'Program.Test3<T>'
        var t4 = new Test4<S[,]>();
    }

    struct S
    {
    }

    class Test<T>
    {
        // ERROR: Cannot create an instance of the variable type 'T'
        // because it does not have the new() constraint
        T s = new T();
    }

    class Test2<T> where T: new()
    {
        T s = new T();
    }

    class Test3<T> where T: struct
    {
        T s = new T();
    }

    // ERROR: The 'new()' constraint cannot be used with the 'struct' constraint
    class Test4<T> where T : struct, new()
    {
        T s = new T();
    }
}

Is there a simple way around this issue? 有没有解决此问题的简单方法?

Your error gives you the answer. 您的错误为您提供了答案。 new() constraint can not be used for structure(both new() and struct constraint can not be used together). new()约束不能用于结构(new()和struct约束不能一起使用)。 So unless it is imperative to have S as structure, I would change S to class and get rid of problem. 因此,除非必须将S作为结构,否则我将把S更改为类并摆脱问题。 But you must have S as structure then workaround I suggest is to create a wraper class. 但是您必须具有S作为结构,然后解决方法我建议创建一个包装器类。

public class Wraper
{
    Wrapper(){}
    Wrapper(S value){
        this.Value = value;
    }

    public S Value {get; set;}

    public static implicit operator S(Wrapper wrapper){
        return wrapper. Value;
    }

    public static implicit operator Wraper(S s){
        return new Wrapper(S);
    }
}

Now you can define your Test as the following: 现在,您可以将测试定义如下:

class Test<T> where T : Wrapper, new()
{
    T value;

    //as you have provided implicit conversion from S to Wrapper and vice versa
    //something like this should work
    public S Value{
        get{ 
            //implicit conversion from Wrapper to S;
            return this.Value;
        }

        set{
            //implicit conversion from S to Wrapper
            this.value = value;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM