简体   繁体   English

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

I have the generic method to parse the json response but when I tries to call it in my method I'm getting an error " The Type TOut must be a reference type in order to use it as a parameter T in the generic type of method". 我具有解析json响应的通用方法,但是当我尝试在我的方法中调用它时,出现错误“ Type TOut必须是引用类型,才能将其用作方法的通用类型中的参数T ”。 Please help and Let me know how to call the generic method. 请帮忙,让我知道如何调用通用方法。

private static T TryParse<T>(string input, T defaultVal = default(T)) where T : class
    {
        try
        {
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(input);
            return result;
        }
        catch (Exception)
        {
            return defaultVal;
        }
    }

public async Task<Tuple<bool, string, TOut>> GetAsync<TOut>(
        Func<string, bool> successTest = null,
        Dictionary<string, string> parameters = null)
    {
        var result = await GetAsync(parameters);
        var response = await GetResponseContentAsync(result.Content);
        var responseObj = TryParse<TOut>(response);


        return Tuple.Create(true, response, responseObj);
    }

Your TryParse<T> method says that T must be a class ( where T : class ). 您的TryParse<T>方法说T必须是一个类( where T : class )。 Your GetAsync<TOut> method lets T be anything - there's no restriction on it. 您的GetAsync<TOut>方法允许T为任意值-对此没有限制。

However, TOut is used as T when calling TryParse . 但是,在调用TryParse时,将TOut用作T Here's the problem - TOut can be anything, but T can only be a class. 这就是问题TOut可以是任何东西,但是T只能是一个类。

Either remove the where T : class restriction from TryParse , or add it to GetAsync ( where TOut : class ). TryParse删除where T : class限制,或将其添加到GetAsyncwhere TOut : class )。 Which you do depends on your requirements. 您要做什么取决于您的要求。

暂无
暂无

声明:本站的技术帖子网页,遵循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>() 该类型必须是引用类型,才能在通用类型或方法中将其用作参数“ T” - The type must be a reference type in order to use it as parameter 'T' in the generic type or 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