对于一个定义泛型类型为参数的函数,如果调用时传入的对象为T对象或者为T的子类,在函数体内部如果需要使用T的属性的方法时,我们可以给这个泛型增加约束;

  类的定义 

   public class ProductEntryInfo
{
[Description("商品编号")]
public int ProductSysNo { get; set; }
//more
} public class ProductEntryInfoEx : ProductEntryInfo
{ [Description("成份")]
public string Component { get; set; }
//more
}

方法

private static string CreateFile<T>(List<T> list)
where T:ProductEntryInfo
{
int productSysNo =list[0].ProductSysNo
 } 

调用 

List<ProductEntryInfo> peList = new List<ProductEntryInfo>();
string fileName = CreateFile( peList); List<ProductEntryInfoEx> peList = new List<ProductEntryInfoEx>();
string fileNameEx = CreateFile(checkListAll);

  这样就可以实现上边的CreateFile方法了

  这样类型参数约束,.NET支持的类型参数约束有以下五种:

    where T : struct | T必须是一个结构类型
    where T : class T必须是一个类(class)类型
    where T : new() | T必须要有一个无参构造函数
    where T : NameOfBaseClass | T必须继承名为NameOfBaseClass的类
    where T : NameOfInterface | T必须实现名为NameOfInterface的接口

分别提供不同类型的泛型约束

  可以提供类似

class MyClass<T, U>
where T : class
where U : struct
{ }

在MSDN详细说明:http://msdn.microsoft.com/zh-cn/library/bb384067.aspx

where T:class 泛型类型约束的更多相关文章

  1. C#高级编程之泛型二(泛型类型约束、类型、反射与泛型)

    泛型类型约束 简言之:对泛型类型进行约束,细化,限定. MSDN的定义:泛型定义中的 where 子句指定对用作泛型类型.方法.委托或本地函数中类型参数的参数类型的约束,意思就是可以有泛型类.泛型方法 ...

  2. C# where(泛型类型约束)

    /*在泛型类型定义中,where 子句用于指定对下列类型的约束:这些类型可用作泛型声明中定义的类型参数的实参. 例如,可以声明一个泛型类 MyGenericClass,这样,类型参数 T 就可以实现 ...

  3. where(泛型类型约束)

    .NET支持的类型参数约束有以下五种: where T : struct T必须是一个结构类型 where T : class T必须是一个类(class)类型,不能是结构(structure)类型 ...

  4. C# where 泛型类型约束

    泛型定义中的 where 子句指定对用作泛型类型.方法.委托或本地函数中类型参数的参数类型的约束. 约束可指定接口.基类或要求泛型类型为引用.值或非托管类型. 它们声明类型参数必须具备的功能. 作为约 ...

  5. where T : class泛型类型约束

    类型参数约束,.NET支持的类型参数约束有以下五种: where T : struct | T必须是一个结构类型where T : class T必须是一个类(class)类型where T : ne ...

  6. 类的 where T : class 泛型类型约束

    where T : struct | T必须是一个结构类型where T : class T必须是一个类(class)类型where T : new() | T必须要有一个无参构造函数where T ...

  7. C#学习(1):类型约束

    where T : class泛型类型约束 类型参数约束,.NET支持的类型参数约束有以下五种: where T : struct | T必须是一个结构类型 where T : class T必须是一 ...

  8. C#知识点记录

    用于记录C#知识要点. 参考:CLR via C#.C#并发编程.MSDN.百度 记录方式:读每本书,先看一遍,然后第二遍的时候,写笔记. CLR:公共语言运行时(Common Language Ru ...

  9. 模板(Template)

    最近阅读google chromium base container stack_container代码,深刻感觉到基础知识不扎实. // Casts the buffer in its right ...

随机推荐

  1. [Angular 2] Adding a data model

    Instead of add todo as a string, we create a data model: export class TodoModel{ constructor( public ...

  2. linux 下 apt命令集详解

    apt命令用法 packagename指代为软件包的名称 apt-get update 在修改/etc/apt/sources.list或/etc/apt/preferences之後运行该命令.此外您 ...

  3. codevs 1281 Xn数列 (矩阵乘法)

    /* 再来个题练练手 scanf longlong 有bug....... */ #include<cstdio> #include<iostream> #include< ...

  4. 纯css+js水平时间轴

    自定义,并自动加载时间节点 当前时间节点居中,突出显示 时间动态无痕添加 效果图: 初始状态 时间左走到一定2016.1月后 html: <!-- 水平时间轴 --> <div id ...

  5. 简单图片banner轮播

    /**************[css]****************/   <style type="text/css">        *{margin:0px; ...

  6. jQuery的选择器中的通配符[id^='code']或[name^='code']

    这两天在做一个专题的时候遇到了一个通配符的问题 //弹层操作$(function(){ //视频播放 $("a[href^='#video']").each(function(in ...

  7. MVC跳转

    //RedirectToAction(view?参数,控制器); return RedirectToAction("MyjoinEvent?id=" + eventid + &qu ...

  8. WPF DataGrid 绑定DataSet数据 自动生成行号

    1.绑定数据:dataGrid1.ItemsSource = dataSet.Tables[0].DefaultView; 注意:在创建DataGrid 时可以通过AutoGenerateColumn ...

  9. 认识<img>标签,为网页插入图片

    在网页的制作中为使网页炫丽美观,肯定是缺少不了图片,可以使用<img>标签来插入图片. 语法: <img src="图片地址" alt="下载失败时的替 ...

  10. Swift - 09 - Optionals

    //: Playground - noun: a place where people can play import UIKit // swift中没有被赋值的变量是不能被使用的 //var str ...