好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

泛型接口的协变和逆变

泛型接口的协变和逆变

泛型接口中的泛型参数可以声明为协变参数或者逆变参数,首先介绍一下泛型接口的协变和逆变:

  协变:泛型参数定义的类型只能作为方法的返回类型,不能用作方法参数的类型,且该类型直接或间接地继承自接口方法的返回值类型,称为协变。可以使用关键字out,将泛型类型参数声明为协变参数。   逆变:泛型参数定义的类型只能作为方法参数的类型,不能用作返回类型,且该类型是接口方法的参数类型的基类型,称为逆变。可以使用in关键字,将泛型类型参数声明为逆变参数。

首先我们来定义一个协变接口,代码如下所示:

  具有协变参数的泛型接口
1 interface ISample1< out T>
2 {
3 T Function1();
4 }
5
6 class Sample1<T>:ISample1<T>
7 {
8 public T Function1()
9 {
10 Console.WriteLine( " Wroked...... " );
11 return default (T);
12 }
13 }

复制代码

上述代码中,ISample1<out T>是一个协变接口,其泛型参数T是一个协变参数,Sample1 <T>泛型类实现了该协变接口,如下代码体现了“协变”:

 1   class  BaseClass{}
2 class Child:BaseClass{}
3 ISample1<Child> sample1= new ISample<Child>(); <1>
4 ISample1<BaseClass> base1=sample1; <2>
5 ISample1< object > base2=base1; <3>

复制代码

在上述代码中,<1>中sample1的协变参数的类型为Child(此时是协变实参),也就是<1>中sample1的接口方法返回类型为Child,二<2>中的接口方法返回类型为BaseClass,CLR会自动完成Child到BaseClass的隐式类型转换,此谓“协变”。同理,<3>也是合法的。
    我们特别介绍一下第一段代码中的“default(T)”代码,在这里default关键字用以解决这样一个问题:在无法预参数化类型T是值类型还是引用类型的情况下,使用default关键字,对于引用类型会返回空引用(null),而对于值类型则会返回0。

 具有逆变参数的泛型接口
1 interface ISample2< in T>
2 {
3 void Function2(T arg);
4 }
5 class Sample2<T> : ISample2<T>
6 {
7 public void Function2(T arg)
8 {
9 Console.WriteLine(arg);
10 }
11 }

复制代码


上述代码定义了一个逆变接口,以及一个实现了该接口的类。接下来,首先使用BaseClass作为类型实参声明,并初始化一个变量sample2,然后再使用Child作为类型实参声明一个变量sample3,Child类是BaseClass类的派生类,可以基于逆变的规则将sample2赋给sample3变量

 1  ISample2<BaseClass> sample2 =  new  Sample2<BaseClass>();
2 ISample2<Child> sample3 =sample2;

复制代码

 

 

TFS团队项目操作 Posted on 2011-10-28 00:16  鬼火飘荡  阅读(1447)  评论(1)   编辑   收藏  

本项目主要的功能是添加和删除团队项目集合、添加和删除团队项目以及向团队项目中添加用户。

一、  添加和删除团队项目集合

微软提供了专门用于添加和删除团队项目集合的API,使得我们的工作事半功倍。

1、   创建团队项目集合

这是微软提供的创建团队项目集合的API:

  1  Namespace: Microsoft.TeamFoundation.Framework.Client
2 Assembly: Microsoft.TeamFoundation.Client (inMicrosoft.TeamFoundation.Client.dll)
3
4 ServicingJobDetailQueueCreateCollection(
5
6 string name,
7
8 string description,
9
10 bool isDefault,
11
12 string virtualDirectory,
13
14 TeamFoundationServiceHostStatusstate,
15
16 IDictionary< string , string >servicingTokens
17
18 )

复制代码



 示例代码:

  1   public   static   bool  CreateTeamprojectSet(stringprojectsetName, string  discription)
2
3 {
4
5 List<ADominModel> ads = ADominDAL.GetAdominModels();
6
7 ADominModelad = ads[ 0 ];
8
9
10
11 stringserverUrl = @" http:// " +ad.AdominIpAddress + " :8080/tfs " ;
12
13 stringcollectionName = projectsetName;
14
15 stringsqlConnectionString = " DataSource=SPMSERVER;Integrated Security=True; " ;
16
17 NetworkCredentialcredential = new NetworkCredential(ad.AdominSuperUser,ad.AdominPassWord, ad.AdominName);
18
19 TfsConfigurationServerTfs = new TfsConfigurationServer( new Uri(serverUrl),(ICredentials)credential);
20
21 ITeamProjectCollectionServicetpcService = Tfs.GetService<ITeamProjectCollectionService>();
22
23 Dictionary< string , string >servicingTokens = new Dictionary< string , string >();
24
25 servicingTokens.Add( " SharePointAction " , " None " ); // 不配置sharepoint
26
27 servicingTokens.Add( " ReportingAction " , " None " ); // 不设置报表服务
28
29 try
30
31 {
32
33 ServicingJobDetailtpcJob = tpcService.QueueCreateCollection(
34
35 collectionName, // 团队项目集合名称
36
37 discription, // 说明
38
39 false , // 是否为默认的团队项目集合
40
41 string .Format( " ~/{0}/ " ,collectionName), // 虚拟目录
42
43 TeamFoundationServiceHostStatus.Started, // 初始状态
44
45 servicingTokens, // 服务口令
46
47 sqlConnectionString, // 数据库连接字符窜 null, // 默认连接字符窜
48
49 null // 数据库分类connection strings
50
51 )
52
53 TeamProjectCollectiontpc = tpcService.WaitForCollectionServicingToComplete(tpcJob);
54
55 returntrue;
56
57 }
58
59 catch (CollectionPropertyException ex)
60
61 {
62
63 throwex ;
64
65 }
66
67 catch (Exception ex)
68
69 {
70
71 throwex;
72
73 }
74
75 }
76
77

复制代码



2、   删除团队项目集合

这是微软提供的删除团队项目集合的API:

Namespace: Microsoft.TeamFoundation.Framework.Client

Assembly: Microsoft.TeamFoundation.Client (inMicrosoft.TeamFoundation.Client.dll)

ServicingJobDetailQueueDetachCollection(

Guid collectionId,

IDictionary< string , string >servicingTokens,

string collectionStoppedMessage,

out string detachedConnectionString

)

复制代码



示例代码:

 public   static   bool  DeleteCollection(stringcollectionName)

{

List<ADominModel> ads = ADominDAL.GetAdominModels();

ADominModelad = ads[ 0 ];

stringserverUrl = @" http:// " +ad.AdominIpAddress + " :8080/tfs " ;

NetworkCredentialcredential = new NetworkCredential(ad.AdominSuperUser,ad.AdominPassWord, ad.AdominName);

TfsConfigurationServerTfs = new TfsConfigurationServer( new Uri(serverUrl),(ICredentials)credential);

ITeamProjectCollectionServicetpcService = Tfs.GetService<ITeamProjectCollectionService>();

Dictionary< string , string >servicingTokens = new Dictionary< string , string >();

servicingTokens.Add( " SharePointAction " , " None " ); // 不配置sharepoint

servicingTokens.Add( " ReportingAction " , " None " ); // 不设置报表服务

TeamProjectCollectiondelCollection = GetCollection(collectionName);

stringvirtualDirectory = delCollection.VirtualDirectory;

stringconnectString;



try

{

ServicingJobDetailtpcJob = tpcService.QueueDetachCollection(delCollection, servicingTokens, " stop " , outconnectString);

if (DeleteDatabase(collectionName))

return true ;

}

catch (Exception ex)

{

Console.WriteLine(ex);

}

connectString = "" ;

returnfalse;

}





复制代码



二、  添加和删除团队项目

由于微软未提供这方面的API,但是另外提供了一个tfpt

( 下载地址 )要创建和删除团队项目必须使用tfpt,考虑到每个客户端都要安装tfpt是不现实的,所以我们将tfpt安装在服务器上,通过webservice来调用tfpt从而实现其功能。这样设计还有一个好处:由于在Team Foundation Server(TFS)上只有系统管理员才能创建项目,故只要将部署Webservice的那台机器添加到TFS的系统管理员中就可以了,这样也方便各个平台的扩展。

下面先介绍tfpt这个工具,其基本的命令如下:

1.删除团队项目

通过使用 TFSDeleteProject,可以将团队项目从 Team Foundation Server 中删除,前提是不再需要项目。此外,如果不成功的团队项目创建后仍未删除的组件,可以使用TFSDeleteProject 删除它们。(注意:TFSDeleteProject会永久地破坏团队项目,之后无法恢复它,你应该在使用 TFSDeleteProject 之前备份所有重要项目数据)

         

可以在运行 团队资源管理器 的任何客户端计算机上的“<驱动器:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE”中找到TFSDeleteProject 命令行工具。

所需权限

若要使用 TFSDeleteProject 命令,您必须是“Team Foundation Administrators(Team Foundation 管理员)”安全组或“Project Administrators (项目管理员)”安全组的成员。有关更多信息,请参见 TeamFoundation Server 权限 和 Team Foundation Server 默认组、权限和角色 。

删除团队项目命令:  

TFSDeleteproject[/q] [/force] [/excludewss] /collection:URL TeamProjectName

删除项目示意图如下:

【参数列表】             

 

选项

   

说明

   

/q

   

可选。使用安静模式。不提示用户进行确认。

   

/force

   

可选。指定即使某些组件不能被删除,应继续删除过程。

   

/excludewss

   

可选。指定不删除与团队项目关联的 SharePoint 站点。指定此选项将维护现有的站点,以便其他团队项目可以继续使用它。

   

/collection:URL

   

必需。指定团队项目集合的 URI。必须对 URI 使用以下格式:http://ServerName:Port/VirtualDirectoryName/CollectionName

 

如果不指定虚拟目录,则必须使用以下格式的 URI:

 

http://ServerName:Port/CollectionName。

   

TeamProjectName

   

必需。项目的名称。如果名称包含空格,则将其置于引号中。

 

2.  创建团队项目

通过使用Createteamproject,在Team Foundation Server 中创建团队项目

 

创建团队项目命令:

tfpt createteamproject /collection:uri

                 /teamproject:"project name"

                 /processtemplate:"template name"

         [/sourcecontrol:New | None|Branch:branchpath]

                      [/log:"logfolder"]

                       [/validate]

                       [/verbose]

                       [/noreports]

                           [/noportal]

【参数列表】

                                       

 

选项

   

说明

   

/collection:URL

   

必需。指定团队项目集合的 URI。必须对 URI 使用以下格式:http://ServerName:Port/VirtualDirectoryName/CollectionName

 

如果不指定虚拟目录,则必须使用以下格式的 URI:

 

http://ServerName:Port/CollectionName。

   

/teamproject

   

必需。需要创建的团队项目的名称。

   

/processtemplate

   

必需。创建项目所需要的模版名称。

   

/sourcecontrol

   

指定源控制选项:”New”表示创建一个新的树型控件源码;“None”表示创建一个新的项目不需要源码控件的支持;“Branch:branchpath” branch from an existing path on the TFS  server. For the Branch option: if the branchpath does not exist on the TFS  server, a list of existing paths are printed.

   

/log

   

可选。指定日志文件存放的路径

   

/validate

   

可选。指定用户输入进行验证。如果是特别用户只会做验证并且没有端口会改变。

   

/verbose

   

可选。开关的详细模式

   

/noreports

   

可选。指定的报告是否会被配置为项目

   

/ noportal

   

可选。如果没有指定的项目门户网站或软件将被配置为项目。

 

我们为大家提供一个简单的service( http://10.2.11.127/CreateTfs.asmx ),有如下方法:

只有将部署service的服务器添加到部署TFS的服务器后,远程调用方法,才可能实现功能,远程调用webservice时会在服务器上创建一个cmd进程:

          

  1    Process p =  new  Process();
2
3 // Process类有一个StartInfo属性,这是ProcessStartInfo类,包括一些属性和方法
4
5 p.StartInfo.FileName = " cmd.exe " ;
6
7 /// /执行的命令及参数,cmdStr为Powershell脚本命令
8
9 p.StartInfo.Arguments = " /c " + cmdStr;
10
11 p.StartInfo.UseShellExecute = false ; // 关闭shell的使用
12
13 p.StartInfo.RedirectStandardInput = true ;
14
15 p.StartInfo.RedirectStandardOutput = true ;
16
17 p.StartInfo.RedirectStandardError= true ;
18
19 p.StartInfo.CreateNoWindow = true ; // 设置不显示窗口
20 p.Start(); // 启动进程

复制代码

在cmd里面执行powershell脚本,从而创建或者删除团队项目。

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于泛型接口的协变和逆变的详细内容...

  阅读:38次

上一篇: HTML5学习

下一篇:OEA体验 :常用功能1