以下代码会出问题:

public static T Protect<T>(Func<T> func, UserLevel pageRole) where T : ActionResult, new()
{
    try
    {
        return func();
    }
    catch (Exception err)
    {
        if (typeof(T) is JsonResult)
        {
            return (T)new JsonResult() ;

}
        else if (typeof(T) is ActionResult)
        {
            return (T)new ContentResult();
        }

throw err;
    }
}

得到 Cannot convert type 'JsonResult'  to 'T',应该修正为:

return (T)(object)new JsonResult();

Well, you can always placate the compiler by adding an object cast in the middle:

geoConfigs = (T)(object)GetGeoConfigurationNumericFromDB(items, maMDBEntities);

which will defer the type-check until runtime. However! There is no compiler way to do this otherwise, as it is never going to be happy with string tests like "MamConfiguration". Also, generics work well when the code is ... generic - i.e. does the same thing which each type. The code shown is the opposite of generic. It is non-generic code exposed through a generic API. It is always going to be messy inside. Personally I would try to avoid this usage in the first place.

或者:

You should be able to just use Convert.ChangeType() instead of your custom code:

public T Get<T>(Stats type) where T : IConvertible
{
return (T) Convert.ChangeType(PlayerStats[type], typeof(T));
}

Cannot convert type SomeClass to 'T'的更多相关文章

  1. Python Theano TypeError: Cannot convert Type TensorType(float64, vector) (of Variable Subtensor{int64:int64:}.0) into Type TensorType(float64, matrix)

    参考: https://groups.google.com/forum/#!topic/theano-users/teA-07wOFpE 这个问题出现的原因是,我在读文件的时候,应该Train_X读成 ...

  2. C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别

    转自:http://www.cnblogs.com/leolis/p/3968943.html 在编程过程中,数据转换是经常要用到的,C#中数据转换的方法很多,拿将目标对象转换为 整型(int)来讲, ...

  3. C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别 <转>

    作者:Statmoon 出处:http://leolis.cnblogs.com/   在编程过程中,数据转换是经常要用到的,C#中数据转换的方法很多,拿将目标对象转换为整型(int)来讲,有四种方法 ...

  4. (int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别

    C#中(int).int.Parse().int.TryParse()和Convert.ToInt32()的区别   原文链接:http://www.cnblogs.com/leolis/p/3968 ...

  5. [C++] Type Conversion(类型转换)

    Type Conversion(类型转换) Two kinds of type conversion explict type conversion(显式类型转换) impict type conve ...

  6. Swift - what's the difference between metatype .Type and .self?

    Declaration typealias AnyClass = AnyObject.Type .Type The metatype of a class, structure, or enumera ...

  7. Java中的static(1)【持续更新】——关于Eclipse的No enclosing instance of type ... 错误的理解和改正

    No enclosing instance of type SomeClass is accessible. Must qualify the allocation with an enclosing ...

  8. 从NullObject谈C#6.0改进

    前言 本文来聊一聊我们经常会做的空值检查问题,从一个简单的空值检查Any Where,到设计模式的NullObjectPattern,再到C#6.0“可能”会提供的语法,让我们体验一次语言开发上的“持 ...

  9. C#高级编程9-第7章 运算符和类型强制转换

    运算符和类型强制转换 1.运算符 运算符的简化操作 条件运算符: if-else的简化操作,也称三元运算符.如果条件为真,返回一个值,为假返回另外一个值. condition?true_value:f ...

随机推荐

  1. node.js之nodemon 代码热更新 修改代码后服务器自动重启

    1.安装nodemon: npm install -g nodemon //全局安装 npm install nodemon --save //局部安装 2.在项目根目录下创建 nodemon.jso ...

  2. 模拟app端上传图片

    使用插件模块管理模式: jsp页面: <sys:fileUpload fieldName="picList" contentId="true" value ...

  3. 004.Zabbix3.x-Server服务端安装

    一 环境基础 1.1 部署基础环境 部署Zabbix需要LAMP或LANP环境,数据库可以为MySQL或者MariaDB.硬件及存储条件按需配置. 1.2 常见依赖列表 Web前端需要支持的软件环境如 ...

  4. 004.KVM日常管理1

    一 常用命令 1.1 查看虚机列表及状态 [root@kvm-host ~]# virsh list --all 1.2 连接虚机 [root@kvm-host ~]# virsh console v ...

  5. Redis 的线程模型

    redis 内部使用文件事件处理器 file event handler,这个文件事件处理器是单线程的,所以 redis 才叫做单线程的模型.它采用 IO 多路复用机制同时监听多个 socket,根据 ...

  6. hdu 3360 最小点覆盖 **

    题意:给你一个图,图中有宝物和保安两种元素.每个宝物需要周围的某些位置同时安放保安(如果那些位置有宝物,可以把宝物替换成保安)问你最少需要再安置多少保安,可以使所有宝物满足要求. 题意有点难懂 链接: ...

  7. Maven入门指南② :Maven 常用命令,手动创建第一个 Maven 项目

    1.根据 Maven 的约定,我们在D盘根目录手动创建如下目录及文件结构: 2.打开pom.xml文件,添加如下内容: <project xmlns="http://maven.apa ...

  8. RubyMine8 安装

    在win7下面安装RubyMine8 中文 汉化 1.需要下载 RubyMine8的程序包 2.先安装好 RubyMine8  很简单  点击下一步就可以  选择安装目录这应该就不需要说了 3.汉化 ...

  9. Entity framework 增加默认执行时间

    public partial class ProductionSupportEntities : DbContext { public ProductionSupportEntities() : ba ...

  10. 《大话设计模式》C#/C++版pdf/源码下载

    大话设计模式(带目录完整版)[中文PDF+源代码].zip 下载地址:http://pan.baidu.com/s/1giQP4大话设计模式C++.pdf下载地址:http://pan.baidu.c ...