zoukankan      html  css  js  c++  java
  • C# 泛型

    (一)泛型类

    image

    代码

    我的本意是要将一个实体参数转换为泛型对象T返回,所以初次代码就写成下面这样:

    复制代码

    public static T GetObj<T>(Employee model)
            {
                T result = default(T);
    if (model is T)
                {
                    result = (T)model; //或者  result = model as T;
                }
    return result;
            }

    复制代码

    可是,编译器提示无法将类型转换为T,之前竟然没碰到过这个问题。查了一下资料,原来,要这么写:

    复制代码

    public class GenericTest
        {
    //public static T GetObj<T>(Employee model)
    //{
    //    T result = default(T);
    //    if (model is T)
    //    {
    //        result = (T)model; //或者  result = model as T;
    //    }
    //    return result;
    //}
    public static T GetObj<T>(Employee model)
            {
                T result = default(T);
    if (model is T)
                {
                    result = (T)(object)model; //或 (T)((object)model);
                }
    return result;
            }
        }

    复制代码

    天杀的ms。

  • 相关阅读:
    time模块
    Spring注入方式及注解配置
    Spring注入值得2种方式:属性注入和构造注入
    MySQL命令行登陆,远程登陆MySQL
    通过XMLHttpRequest和jQuery两种方式实现ajax
    Linux常用命令
    ASP.NET 打包下载文件
    从用户浏览器输入url到用户看到页面结果的过程,发生了什么事情?
    冒泡排序的三种实现
    字符串编码C#
  • 原文地址:https://www.cnblogs.com/lihuali/p/8900394.html
Copyright © 2011-2022 走看看