C# - 全局内部常量?
我正在尝试执行以下操作:
[FooAttribute(Value = String.Format("{0} - {1}", myReources.BaseString, "Bar"))]
public int FooBar { get; set; }
但是编译器会抱怨...那么当我将 BaseString
放在一个位置时,正确的方法是什么?我的代码中散布着库内属性的属性,因此“全局”内部 const 听起来像是解决方案,因为我无法使用资源。
I'm trying to do the following:
[FooAttribute(Value = String.Format("{0} - {1}", myReources.BaseString, "Bar"))]
public int FooBar { get; set; }
The compiler complains though... so what is the correct way to do it where I have my BaseString
in one location? My code is littered with attributes on the properties inside my library, so "global" internal const sound like the solution since I can't use resources.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在属性中使用 string.Format 之类的表达式...但以下内容应该有效:
You can't have expressions like string.Format in an attribute...but the following should work:
如果删除 String.Format 并使用基本字符串连接,编译器不会抱怨。由于 String.Format 在运行时而不是编译时解析,因此您不能在属性中使用它。编译器将识别 myResources.BaseString 和“Bar”都是常量值,因此这样做是合法的。
<代码>
[FooAttribute(Value = myReources.BaseString + "Bar")]
If you remove the String.Format and use basic string concatenation, the compiler will not complain. Since String.Format is resolved at runtime and not compile time, you can't use it in attributes. The compiler will recognize that both the myResources.BaseString and "Bar" are constant values so it's legal to do this.
[FooAttribute(Value = myReources.BaseString + "Bar")]