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# 属性文本?


基础教程推荐
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01