以下代码会出问题:

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. AIM Tech Round 4 (Div. 1) C - Upgrading Tree 构造 + 树的重心

    C - Upgrading Tree 我发现我构造题好弱啊啊啊. 很明显能想到先找到重心, 然后我们的目标就是把所有点接到重心的儿子上,让重心的儿子子树变成菊花图, 这个先把重心到儿子的边连到 i , ...

  2. 【Java】 大话数据结构(8) 串的模式匹配算法(朴素、KMP、改进算法)

    本文根据<大话数据结构>一书,实现了Java版的串的朴素模式匹配算法.KMP模式匹配算法.KMP模式匹配算法的改进算法. 1.朴素的模式匹配算法 为主串和子串分别定义指针i,j. (1)当 ...

  3. 007 @CookieValue绑定请求中的cookie

    1.介绍 2.使用的cookie 3.index.jsp <%@ page language="java" contentType="text/html; char ...

  4. C#并行编程(5):需要知道的异步

    异步与并行的联系 大家知道"并行"是利用CPU的多个核心或者多个CPU同时执行不同的任务,我们不关心这些任务之间的依赖关系. 但是在我们实际的业务中,很多任务之间是相互影响的,比如 ...

  5. UVALive 6911 Double Swords 树状数组

    Double Swords 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8 ...

  6. spring-boot parent变更为依赖方式

    原parent继承方式 <parent> <groupId>org.springframework.boot</groupId> <artifactId> ...

  7. 再谈JavaScript中的闭包

    一.什么是闭包 闭包就是有权访问另一个函数作用域中变量的函数,因此,闭包的本质是一个函数.当一个内部函数被保存到外部时,就会生成闭包. 二.闭包的作用 1.实现公有变量,即通过局部变量实现全局变量的效 ...

  8. 使用 IntraWeb (15) - 基本控件之 TIWEdit、TIWMemo、TIWText

    TIWEdit //单行文本框, 通过 PasswordPrompt 属性可以作为密码框 TIWMemo //多行文本框 TIWText //相当于多行的 TIWLabel 或不能编辑的 TIWMem ...

  9. How Visual Studio 2012 Avoids Prompts for Source

    [原文地址]:http://blogs.msdn.com/b/heaths/archive/2012/07/26/how-visual-studio-2012-avoids-prompts-for-s ...

  10. JTAG - Debug Cable Driver/Receiver