设置一个C#可选参数的默认值 [英] Setting the default value of a C# Optional Parameter

查看:738
本文介绍了设置一个C#可选参数的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试设置的可选参数在资源文件什么的默认值,我得到一个编译时错误

Whenever I attempt to set the default value of an optional parameter to something in a resource file, I get a compile-time error of

有关信息默认参数值必须是一个编译时间常数。

Default parameter value for 'message' must be a compile-time constant.

有什么办法,我可以改变资源文件是如何工作的,使这一切成为可能?

Is there any way that I can change how the resource files work to make this possible?

public void ValidationError(string fieldName, 
                            string message = ValidationMessages.ContactNotFound)

在此, ValidationMessages 是一个资源文件。

In this, ValidationMessages is a resource file.

推荐答案

没有,你将不能够直接使资源工作的默认值。你需要做的是设置默认值,像空,然后做资源查找,当参数在方法体内的默认值。

No, you will not be able to make the resource work directly in the default. What you need to do is set the default value to something like null and then do the resource lookup when the parameter has the default value in the body of the method.

这篇关于设置一个C#可选参数的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆