资源文件中的 C# 属性文本?

0

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

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14