泛型接口的协变和逆变
2012-04-08 23:36
471 查看
泛型接口中的泛型参数可以声明为协变参数或者逆变参数,首先介绍一下泛型接口的协变和逆变:
协变:泛型参数定义的类型只能作为方法的返回类型,不能用作方法参数的类型,且该类型直接或间接地继承自接口方法的返回值类型,称为协变。可以使用关键字out,将泛型类型参数声明为协变参数。
逆变:泛型参数定义的类型只能作为方法参数的类型,不能用作返回类型,且该类型是接口方法的参数类型的基类型,称为逆变。可以使用in关键字,将泛型类型参数声明为逆变参数。
首先我们来定义一个协变接口,代码如下所示:
复制代码
上述代码中,ISample1<out T>是一个协变接口,其泛型参数T是一个协变参数,Sample1 <T>泛型类实现了该协变接口,如下代码体现了“协变”:
复制代码
在上述代码中,<1>中sample1的协变参数的类型为Child(此时是协变实参),也就是<1>中sample1的接口方法返回类型为Child,二<2>中的接口方法返回类型为BaseClass,CLR会自动完成Child到BaseClass的隐式类型转换,此谓“协变”。同理,<3>也是合法的。
我们特别介绍一下第一段代码中的“default(T)”代码,在这里default关键字用以解决这样一个问题:在无法预参数化类型T是值类型还是引用类型的情况下,使用default关键字,对于引用类型会返回空引用(null),而对于值类型则会返回0。
复制代码
上述代码定义了一个逆变接口,以及一个实现了该接口的类。接下来,首先使用BaseClass作为类型实参声明,并初始化一个变量sample2,然后再使用Child作为类型实参声明一个变量sample3,Child类是BaseClass类的派生类,可以基于逆变的规则将sample2赋给sample3变量
复制代码
本项目主要的功能是添加和删除团队项目集合、添加和删除团队项目以及向团队项目中添加用户。
一、 添加和删除团队项目集合
微软提供了专门用于添加和删除团队项目集合的API,使得我们的工作事半功倍。
1、 创建团队项目集合
这是微软提供的创建团队项目集合的API:
复制代码
示例代码:
复制代码
2、 删除团队项目集合
这是微软提供的删除团队项目集合的API:
复制代码
示例代码:
复制代码
二、 添加和删除团队项目
由于微软未提供这方面的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
删除项目示意图如下:
【参数列表】
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]
【参数列表】
我们为大家提供一个简单的service(http://10.2.11.127/CreateTfs.asmx),有如下方法:
只有将部署service的服务器添加到部署TFS的服务器后,远程调用方法,才可能实现功能,远程调用webservice时会在服务器上创建一个cmd进程:
复制代码
在cmd里面执行powershell脚本,从而创建或者删除团队项目。
协变:泛型参数定义的类型只能作为方法的返回类型,不能用作方法参数的类型,且该类型直接或间接地继承自接口方法的返回值类型,称为协变。可以使用关键字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 | 必需。项目的名称。如果名称包含空格,则将其置于引号中。 |
通过使用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的服务器添加到部署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脚本,从而创建或者删除团队项目。
相关文章推荐
- c#4.0泛型接口和泛型委托的协变和逆变
- C#里泛型接口支持协变、逆变和不支持协变、逆变的对比?
- C#中泛型方法与泛型接口 C#泛型接口 List<IAll> arssr = new List<IAll>(); interface IPerson<T> c# List<接口>小技巧 泛型接口协变逆变的几个问题
- c#打包文件解压缩 C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法 一个简单例子理解C#的协变和逆变 对于过长字符串的大小比对
- 理解 C# 泛型接口中的协变与逆变(抗变)
- <转>C# 4.0 为泛型编程引入了 协变 和 逆变 支持,这是个不错的福利,能省掉以往的一些麻烦。不过当前(Beta2)仅支持泛型接口和泛型委托。
- <转>C# 4.0 为泛型编程引入了 协变 和 逆变 支持,这是个不错的福利,能省掉以往的一些麻烦。不过当前(Beta2)仅支持泛型接口和泛型委托。
- [译]委托和接口泛型参数类型的协变和逆变
- 泛型在接口中的协变、逆变练习
- 第五节:泛型(泛型类、接口、方法、委托、泛型约束、泛型缓存、逆变和协变)
- 泛型接口协变逆变的几个问题
- 泛型的协变,逆变
- Kotlin 泛型的协变和逆变
- C# 协变逆变(泛型修饰符in和out)
- 建议43:让接口中的泛型参数支持协变
- Java用通配符 获得泛型的协变和逆变
- .NET 4.0中的泛型的协变和逆变
- 转载.NET 4.0中的泛型的协变和逆变
- 协变、逆变与不变:数组、泛型、与返回类型
- C# 泛型的协变和逆变 (转载)