类型必须是引用类型,以便在泛型类型或方法中将其用作参数'T'

时间:2021-12-19 00:41:54

I'm getting deeper into generics and now have a situation I need help with. I get a compile error on the 'Derived' class below as shown in the subject title. I see many other posts similar to this one but I'm not seeing the relationship. Can someone tell me how to resolve this?

我正在深入研究泛型,现在有一个我需要帮助的情况。我在下面的“派生”类上得到一个编译错误,如标题所示。我看到过很多类似的帖子,但是我没有看到它们之间的关系。有人能告诉我如何解决这个问题吗?

using System;
using System.Collections.Generic;


namespace Example
{
    public class ViewContext
    {
        ViewContext() { }
    }

    public interface IModel
    {
    }

    public interface IView<T> where T : IModel 
    {
        ViewContext ViewContext { get; set; }
    }

    public class SomeModel : IModel
    {
        public SomeModel() { }
        public int ID { get; set; }
    }

    public class Base<T> where T : IModel
    {

        public Base(IView<T> view)
        {
        }
    }

    public class Derived<SomeModel> : Base<SomeModel> where SomeModel : IModel
    {

        public Derived(IView<SomeModel> view)
            : base(view)
        {
            SomeModel m = (SomeModel)Activator.CreateInstance(typeof(SomeModel));
            Service<SomeModel> s = new Service<SomeModel>();
            s.Work(m);
        }
    }

    public class Service<SomeModel> where SomeModel : IModel
    {
        public Service()
        {
        }

        public void Work(SomeModel m)
        {

        }
    }
}

3 个解决方案

#1


336  

I can't repro, but I suspect that in your actual code there is a constraint somewhere that T : class - you need to propagate that to make the compiler happy, for example (hard to say for sure without a repro example):

我不能支持repro,但我怀疑在您的实际代码中,在T: class中有一个约束——您需要传播它以使编译器满意,例如(如果没有repro示例,很难肯定):

public class Derived<SomeModel> : Base<SomeModel> where SomeModel : class, IModel
                                                                    ^^^^^
                                                                 see this bit

#2


36  

You get this error if you have constrained T to being a class

如果你把T限制为一个类,就会得到这个错误。

#3


6  

If you put constrains on a generic class or method, every other generic class or method that is using it need to have "at least" those constrains.

如果您对泛型类或方法施加约束,则使用泛型类或方法的所有其他泛型类或方法都需要“至少”具有这些约束。

#1


336  

I can't repro, but I suspect that in your actual code there is a constraint somewhere that T : class - you need to propagate that to make the compiler happy, for example (hard to say for sure without a repro example):

我不能支持repro,但我怀疑在您的实际代码中,在T: class中有一个约束——您需要传播它以使编译器满意,例如(如果没有repro示例,很难肯定):

public class Derived<SomeModel> : Base<SomeModel> where SomeModel : class, IModel
                                                                    ^^^^^
                                                                 see this bit

#2


36  

You get this error if you have constrained T to being a class

如果你把T限制为一个类,就会得到这个错误。

#3


6  

If you put constrains on a generic class or method, every other generic class or method that is using it need to have "at least" those constrains.

如果您对泛型类或方法施加约束,则使用泛型类或方法的所有其他泛型类或方法都需要“至少”具有这些约束。