草庐IT

可选性

全部标签

c# - 我可以为 ASP.NET SOAP Web 服务设置一个可选参数吗

我想用这个签名构建一个网络服务,如果param2留空,它不会抛出异常。这可能吗?[WebMethod]publicstringHelloWorld(stringparam1,boolparam2){}异常是在尝试将空字符串转换为bool值时抛出的System.ArgumentException。到目前为止还没有奏效的想法:网络服务不允许方法重载,例如publicstringHelloWorld(stringparam1){returnHelloWorld(param1,false);}按照建议here:使bool可为空bool?。同样的异常(exception)。操纵WSDL,参见th

c# - 为 WCF 实现接口(interface)时不能使用可选参数

在我的界面中我已经声明了这一点。[OperationContract][WebGet]StringGetStuff(Stringbeep,Stringboop="toolazytotype");我是这样实现的。StringGetStuff(Stringbeep,Stringboop="toolazytotype"){...}它编译并上传为我的WCF服务。但是,当我将它用作Web引用并尝试执行下面的代码时,编译器提示说没有带有单个参数签名的方法。最后一行是问题所在。我怎么能懒得打字默认?ServiceClientclient=newServiceClient();client.GetSt

c# - Entity Framework 6 可选的单向关系

我有两个表:Client------------------------Id(string)现在我想创建一个从Departmant到Client的可选关系(使用ClientNumber)。我在Department类(Client)中创建了一个虚拟属性,现在我需要使用EntityTypeConfiguration配置关系。数据库中没有配置外键,我无法更改数据库。我也无法更改实体(类)客户端。所以我需要告诉EntityFrameworkDepartment类中的ClientNumber与Client类中的Number属性相关(可选)。但我不知道如何告诉EF部门的ClientNumber与客

c# - C# 中的可选委托(delegate)

这个问题在这里已经有了答案:Defaultvalueongenericpredicateasargument(3个答案)关闭9年前。这是两个扩展方法重载的简单示例publicstaticclassExtended{publicstaticIEnumerableEven(thisListnumbers){returnnumbers.Where(num=>num%2==0);}publicstaticIEnumerableEven(thisListnumbers,Predicatepredicate){returnnumbers.Where(num=>num%2==0&&predicate

c# - MSDeploy - 允许参数在 parameters.xml 中是可选的/为空的

我正在使用msdeploy部署asp.net-mvcWeb应用程序通过teamcity.我正在使用paramaters.xml文件来操纵我的应用程序的web.config,具体应用settings部分。我有一些设置,其中只有对特定环境具有值才有效,其余时间该值应为空白(即,Property应仅在Production上具有值)。但是,当我不指定一个值时,MSDeploy给了我这个异常:Microsoft.Web.Deployment.DeploymentException:The'facebookUserToken'argumentcannotbenullorempty.atMicros

C# 4.0 - 如何处理可选的字符串参数

此代码无效:privatevoidFoo(stringoptionalString=string.Empty){//dofoo.}但是这段代码是:privatevoidFoo(stringoptionalString=""){//dofoo.}为什么?因为string.Empty是只读字段,不是常量,可选参数的默认值必须是编译时常量。所以,关于我的问题......(好吧,关注)这是我必须做的:privateconststringemptyString="";privatevoidFoo(stringoptionalString=emptyString){//dofoo.if(!stri

c# - 使用 System.Drawing.Color 类型的可选参数

我开始利用.Net4.0中的可选参数我遇到的问题是当我尝试声明System.Drawing.Color的可选参数时:publicmyObject(intfoo,stringbar,Colorrgb=Color.Transparent){//....}我希望Color.Transparent成为rgb参数的默认值。问题是,我一直收到这个编译错误:Defaultparametervaluefor'rgb'mustbeacompile-timeconstant如果我只能将原始类型用于可选参数,那真的会扼杀我的计划。 最佳答案 在这种情况下

c# - 关键字 'event' 在 C# 中是可选的吗?

eventOne(带关键字“event”)和eventTwo(不带关键字)有什么区别?classProgram{publiceventEventHandlereventOne;publicEventHandlereventTwo;publicvoidRaiseOne(){if(eventOne!=null)eventOne(this,EventArgs.Empty);}publicvoidRaiseTwo(){if(eventTwo!=null)eventTwo(this,EventArgs.Empty);}staticvoidMain(string[]args){varp=newPr

c# - 你能用可选参数解释一下 c# 的这种奇怪行为吗?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:C#optionalparametersonoverriddenmethods这是以下代码的输出:Peter:-1Peter:0Fred:1Fred:1你能解释一下为什么Peterp.TellYourAge()和p.DoSomething()的调用不一样吗?这里是您自己尝试的代码(VS2010和FW4):staticvoidMain(string[]args){Peterp=newPeter();p.TellYourAge();//expected-1,result:-1p.DoSomething();//e

c# - 在属性构造函数中使用可选参数时,属性参数必须是常量错误

谁能解释为什么这段代码有效:publicclassAdministratorSettingValidationAttribute:Attribute{publicAdministratorSettingValidationAttribute(AdministratorSettingDataTypeadministratorSettingDataType){DataType=administratorSettingDataType;}publicAdministratorSettingValidationAttribute(AdministratorSettingDataTypeadmi