以下代码会出问题:

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. Android 隐藏虚拟按键,并且全屏

    /** * 隐藏虚拟按键,并且全屏 */protected void hideBottomUIMenu() { //隐藏虚拟按键,并且全屏 if (Build.VERSION.SDK_INT > ...

  2. salesforce

    salesforce&apex salesforce开发的小心得 salesforce零基础学习(七十七)队列的实现以及应用 摘要: 队列和栈简单的区别为栈是后进先出,队列是先进先出.队列也是 ...

  3. 主动学习——active learning

    阅读目录 1. 写在前面 2. 什么是active learning? 3. active learning的基本思想 4. active learning与半监督学习的不同 5. 参考文献   1. ...

  4. P1280 尼克的任务 线性DP

    题目描述 尼克每天上班之前都连接上英特网,接收他的上司发来的邮件,这些邮件包含了尼克主管的部门当天要完成的全部任务,每个任务由一个开始时刻与一个持续时间构成. 尼克的一个工作日为N分钟,从第一分钟开始 ...

  5. 【LeetCode】170. Two Sum III – Data structure design

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...

  6. IO知识点整理(文件File类的使用)

    一: 1.API 2.构造函数的程序 注意这集中构造函数的特点. 同时,字段separator的使用. import java.io.File; public class Test101 { publ ...

  7. Djang1.8+Python2.0迁移到Django2.0+Python3.6注意事项(转)

    Djang1.8+Python2.0迁移到Django2.0+Python3.6注意事项 参考:https://blog.csdn.net/weixin_40475396/article/detail ...

  8. Plant Simulation常用命令

    Plant Simulation 是面向对象的三维离散事件仿真软件,使您能够快速.直观地构建逼真的物流模型. 您还可以使用高级统计工具执行复杂的生产分析.以下介绍该软件的一些常用命令. 1. clea ...

  9. GRYZ 模 拟 赛 系 列 之 迷 宫(不就是个洪水)

    - 迷 宫 (maze.cpp/c/pas) Description Karles 和朋友到迷宫玩耍,没想到遇上了 10000000 年一次的大洪水,好在 Karles 是一个喜 欢思考的人,他发现迷 ...

  10. 优美的爆搜?KDtree学习

    如果给你平面内一些点,让你求距离某一个指定点最近的点,应该怎么办呢? O(n)遍历! 但是,在遍历的过程中,我们发现有一些点是永远无法更新答案的. 如果我们把这些点按照一定顺序整理起来,省略对不必要点 ...