资源文件中的 C# 属性文本? [英] C# attribute text from resource file?

查看:12
本文介绍了资源文件中的 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# 属性文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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