简体   繁体   English

为了在通用类型或方法中将其用作参数“ 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

I followed the suggestion around this web site to made the following method: 我按照该网站上的建议进行了以下操作:

public static T? GetElementValue<T>(this XElement xElement, string s) 
    where T : struct, IComparable
{
     var result = new Nullable<T>();

     try
     {
         if (string.IsNullOrEmpty(s) || s.Trim().Length > 0 || xElement.IsEmpty)
            return result;

         var element = xElement.Element(s);

         if (element == null || element.IsEmpty)
             return result;

         var conv = TypeDescriptor.GetConverter(typeof(T));
         result = (T)conv.ConvertFrom(element.Value);
     }
     catch (Exception ex)
     {
          ex.Message.WriteAsError();
     }

     return result;
}

but when I try to use it with a string I still get the following error: The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 但是当我尝试将其与string一起使用时,仍然出现以下错误:类型“ string”必须为不可为空的值类型,才能在通用类型或方法中将其用作参数“ T”

Someone can tell me why? 有人可以告诉我为什么吗? Thx 谢谢

You have restricted your T parameter by 您已将T参数限制为

where T : struct

The struct restriction means that only value types can be supplied for T . struct限制意味着只能为T提供值类型。 string (more precisely, System.String ) however, is a class, a reference type. string (更确切地说是System.String )是一个类,一种引用类型。

Removing that restriction will not be quite trivial, as you use T as T? 取消该限制并不是一件容易的事,因为您将T用作T? , ie a nullable T . 即可空T Nullable<T> in turn only accepts value types. Nullable<T>依次仅接受值类型。

You will have to decide on whether you want to allow reference types - if you want to restrict T to reference types, use the class constraint instead of struct (and drop the ? from T? , as reference types are always nullable on their own). 您必须对您是否允许引用类型决定-如果你想限制T引用类型,使用class约束,而不是struct (并删除?T? ,引用类型总是为空的自己) 。 If you want to allow both value and reference types, you cannot make a nullable version of T . 如果要同时允许值和引用类型,则不能创建T的可空版本。 If you need all of these, please extend your description of the circumstances so we can help you find a better solution. 如果您需要所有这些,请扩展对情况的描述,以便我们帮助您找到更好的解决方案。

因为您将T限制为值类型- where T : struct, IComparable ,而string是引用类型。

a string is not a value type, it is an immutable reference type so it violates the generic constraint you have where T must be a struct . 字符串不是值类型,它是不可变的引用类型,因此它违反了T必须为struct的通用约束。

Take a look at the string type documentation for more details about string . 请参阅string类型文档,以获取有关string更多详细信息。

暂无
暂无

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

相关问题 类型'string'必须是不可为空的值类型,以便在泛型类型或方法'System.Nullable <T>'中将其用作参数'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 'System.Nullable<T>' 类型“ 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>' 类型“字符串”必须是不可为空的类型,以便将其用作泛型类型或方法“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>' 该类型必须是不可为空的值类型,以便在通用类型或方法“ System.Nullable”中将其用作参数“ T” <T> “ - The type 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>' 该类型(我的类)必须为非空类型,以便在通用方法中用作参数“ T” - the type (my class) must be non-nullable type in order to use as a parameter 'T' in a generic method 使用 Struct 作为通用参数并转换到基接口会导致错误:必须是不可为空的值类型 - Using Struct as Generic Parameter and casting to the base interface cause the error: must be a non-nullable value type 类型必须是不可为空的值 - The type must be a non-nullable value 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM