C# attribute text from resource file?(资源文件中的 C# 属性文本?)
问题描述
我有一个属性,我想将文本从资源文件加载到属性中.
I have an attribute and i want to load text to the attribute from a resource file.
[IntegerValidation(1, 70, ErrorMessage = Data.Messages.Speed)]
private int i_Speed;
但我不断得到"属性参数必须是属性参数类型的常量表达式、typeof表达式或数组创建表达式"
But I keep getting "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"
如果我添加一个字符串而不是 Data.Messages.Text,它会完美运行,例如:
It works perfectly if i add a string instead of Data.Messages.Text, like:
[IntegerValidation(1, 70, ErrorMessage = "Invalid max speed")]
有什么想法吗?
推荐答案
编译时属性值被硬编码到程序集中.如果你想在执行时做任何事情,你需要使用一个常量作为key,然后将一些代码放入属性类本身来加载资源.
Attribute values are hard-coded into the assembly when you compile. If you want to do anything at execution time, you'll need to use a constant as the key, then put some code into the attribute class itself to load the resource.
这篇关于资源文件中的 C# 属性文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:资源文件中的 C# 属性文本?
基础教程推荐
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 如果条件可以为空 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 将数据集转换为列表 2022-01-01
