简体   繁体   English

如何将字段指定为函数C#的参数?

[英]How to specify fields as params for function C#?

I have one [Operation Contract] where described function which registers a new user in WCF Service: 我有一个[Operation Contract] ,其中描述了在WCF服务中注册新用户的功能:

[OperationContract]
boolean Register(string name, string password);

This function accepts two parameters name and password of user. 该函数接受用户的两个参数namepassword How I can extend these parameters in the future, for example If I want to add second name for register function or more parameters? 我将来如何扩展这些参数,例如,如果我想为寄存器功能添加第二个名字或更多参数?

I can do the following: 我可以执行以下操作:

boolean Register(string name, string password, string secondName);

But if params are more 20? 但是,如果参数多于20?

If you don't mind breaking the contract for the operation that you want to change, you could combine some of the (more than 20) parameters into a class. 如果您不介意为要更改的操作打破合同,可以将一些(超过20个)参数组合到一个类中。

This is generally good practice when writing methods that have a lot of parameters (see also What's the best way to refactor a method that has too many (6+) parameters? ). 在编写具有很多参数的方法时,这通常是一种好习惯(另请参见重构具有过多(6+)个参数的方法的最佳方法是什么? )。

You may probably need to add a [DataContract] attribute to that class then, though, if you want to use it in WCF calls. 但是,如果要在WCF调用中使用它,则可能需要向该类添加[DataContract]属性。

By using the params keyword, you can specify a method parameter that takes a variable number of arguments. 通过使用params关键字,您可以指定一个采用可变数量参数的方法参数。 like- 喜欢-

boolean Register(params string[] list);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM