对于泛型类型参数,不进行装箱或类型参数转换 [英] No boxing or type parameter conversion for generic Type parameter

查看:523
本文介绍了对于泛型类型参数,不进行装箱或类型参数转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下辅助方法:

  public static T CreateRequest< T>()
其中T: Request,new()
{
T request = new T();
// ...
//分配默认值等。
// ...
返回请求;
}

我想在另一个方法中使用这个方法:

  public T Map< F,T>(F value,T toValue)
其中T:new b $ b其中F:new()
{
if(typeof(T).BaseType.FullName ==MyNamespace.Request)
{
toValue = MyExtensions.CreateRequest ; T>();
}
else
{
toValue = new T();
}
}

但是我得到错误:


类型'T'不能用作泛型类型或方法'MyExtensions.CreateRequest()'中的类型参数'T'。没有从T到MyNamespace.Request的装箱转换或类型参数转换。


类型T,这样CreateRequest会使用它没有问题?



EDIT:



我知道我可以做两件事:




  • 松开对CreateRequest或
  • 的限制
  • 地图中的约束。



但是我不能做第一个,因为在CreateRequest I的Request类的用户属性, ,因为我使用其他类型(不继承Request)与Map函数。

解决方案

需要放松 CreateRequest 的一般限制。

  public static T CreateRequest< T()
其中T:new()
{
if(!typeof(Request).IsAssignableFrom(typeof(T)))
throw new ArgumentException

var result = new T();
请求请求=(请求)(对象)结果;
// ...
//分配默认值等。
// ...
return result;
}

这可能会很痛苦,因为您失去了对此参数的编译时验证。 p>

或者如果您想在其他地方使用 CreateRequest 方法,那么只对这种情况创建非通用重载。

  public static object CreateRequest(type requestType)
{
if(!typeof(Request).IsAssignableFrom(requestType))
throw new ArgumentException();

var result = Activator.CreateInstance(requestType);
请求请求=(请求)结果;
// ...
//分配默认值等。
// ...
return result;
}


I have the following helper method:

public static T CreateRequest<T>()
    where T : Request, new()
{
    T request = new T();
    // ...
    // Assign default values, etc.
    // ...
    return request;
}

I want to use this method from the inside of another method in another helper:

public T Map<F, T>(F value, T toValue)
    where T : new()
    where F : new()
{
    if (typeof(T).BaseType.FullName == "MyNamespace.Request")
    {
        toValue = MyExtensions.CreateRequest<T>();
    }
    else
    {
        toValue = new T();
    }
}

But then I get the error:

The type 'T' cannot be used as type parameter 'T' in the generic type or method 'MyExtensions.CreateRequest()'. There is no boxing conversion or type parameter conversion from 'T' to 'MyNamespace.Request'.

Is there a way to cast the type "T", so that CreateRequest would use it without problems?

EDIT:

I know I can do two things:

  • loosen constraints on CreateRequest or
  • tighten contraints in Map.

But I can't do the first, because in CreateRequest I user properties of Request class, and I can't do the second, because I use other types (that don't inherit from Request) with Map function.

解决方案

For this scenario you'll need to loosen generic restrictions of CreateRequest.

public static T CreateRequest<T>()
    where T : new()
{
    if(!typeof(Request).IsAssignableFrom(typeof(T)))
        throw new ArgumentException();

    var result = new T();
    Request request = (Request)(object)result;
   // ...
   // Assign default values, etc.
   // ...
   return result ;
}

It might be painful because you lose compile time verification of this parameter.

Or if you want to use CreateRequest method elsewhere then create non-generic overload for this scenario only.

public static object CreateRequest(Type requestType)
 {
    if(!typeof(Request).IsAssignableFrom(requestType))
        throw new ArgumentException();

    var result = Activator.CreateInstance(requestType);
    Request request = (Request)result;
   // ...
   // Assign default values, etc.
   // ...
   return result ;
}

这篇关于对于泛型类型参数,不进行装箱或类型参数转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆