简体   繁体   English

该类型必须是引用类型,才能在通用类型或方法中将其用作参数“ T”

[英]The type must be a reference type in order to use it as parameter 'T' in the generic type or method

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)
        {

        }
    }
}

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): 我无法复制,但是我怀疑您的实际代码中存在一个T : class的约束-例如,您需要传播该约束以使编译器满意(很难说没有复制实例):

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

如果将T限制为一class则会出现此错误

如果将约束放在通用类或方法上,则使用它的所有其他通用类或方法都必须“至少”具有这些约束。

暂无
暂无

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

相关问题 为了在通用类型或方法“ QueryAPI.Query”中将其用作参数“ T”,类型“ T”必须是引用类型。 <T> () - The type 'T' must be a reference type in order to use it as parameter 'T' in the generic type or method 'QueryAPI.Query<T>() TOut类型必须是引用类型,才能在通用方法类型中用作参数T - The Type TOut must be a reference type in order to use it as a parameter T in the generic type of method 为了在通用类型或方法中将其用作参数“ T”,类型“字符串”必须为非空值类型 - The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 类型“T”必须是可转换的,以便在泛型类型或方法中将其用作参数“T” - The type 'T' must be convertible in order to use it as parameter 'T' in the generic type or method T必须是具有公共无参数构造函数的非抽象类型,以便在泛型类型或方法中将其用作参数“TModel” - T must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TModel' in the generic type or method 该类型(我的类)必须为非空类型,以便在通用方法中用作参数“ T” - the type (my class) must be non-nullable type in order to use as a parameter 'T' in a generic method 类型“ T1”必须是不可为空的值类型,以便在通用类型或方法“ System.Nullable”中将其用作参数“ T” <T> &#39; - The type 'T1' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>' 类型&#39;T&#39;必须是非可空值类型,以便在泛型类型或方法&#39;System.Nullable中将其用作参数&#39;T&#39; <T> “ - The type 'T' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>' 类型&#39;MyObject&#39;必须是非可空值类型才能在泛型类型或方法&#39;Nullable中将其用作参数&#39;T&#39; <T> “ - The type 'MyObject' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable<T>' 类型“字符串”必须是不可为空的类型,以便将其用作泛型类型或方法“System.Nullable”中的参数 T<T> &#39; - The type 'string' must be a non-nullable type in order to use it as parameter T in the generic type or method 'System.Nullable<T>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM