[英]C# attribute text from resource file?
我有一個屬性,我想從資源文件加載文本到屬性。
[IntegerValidation(1, 70, ErrorMessage = Data.Messages.Speed)]
private int i_Speed;
但我不斷得到“屬性參數必須是屬性參數類型的常量表達式,typeof表達式或數組創建表達式”
如果我添加一個字符串而不是Data.Messages.Text,它可以很好地工作,如:
[IntegerValidation(1, 70, ErrorMessage = "Invalid max speed")]
有任何想法嗎?
這是我的解決方案。 我已經將resourceName和resourceType屬性添加到屬性,就像微軟在DataAnnotations中所做的那樣。
public class CustomAttribute : Attribute
{
public CustomAttribute(Type resourceType, string resourceName)
{
Message = ResourceHelper.GetResourceLookup(resourceType, resourceName);
}
public string Message { get; set; }
}
public class ResourceHelper
{
public static string GetResourceLookup(Type resourceType, string resourceName)
{
if ((resourceType != null) && (resourceName != null))
{
PropertyInfo property = resourceType.GetProperty(resourceName, BindingFlags.Public | BindingFlags.Static);
if (property == null)
{
throw new InvalidOperationException(string.Format("Resource Type Does Not Have Property"));
}
if (property.PropertyType != typeof(string))
{
throw new InvalidOperationException(string.Format("Resource Property is Not String Type"));
}
return (string)property.GetValue(null, null);
}
return null;
}
}
編譯時,屬性值被硬編碼到程序集中。 如果要在執行時執行任何操作,則需要使用常量作為鍵 ,然后將一些代碼放入屬性類本身以加載資源。
這是我放在一起的修改版本:
[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple = false)]
public class ProviderIconAttribute : Attribute
{
public Image ProviderIcon { get; protected set; }
public ProviderIconAttribute(Type resourceType, string resourceName)
{
var value = ResourceHelper.GetResourceLookup<Image>(resourceType, resourceName);
this.ProviderIcon = value;
}
}
//From http://stackoverflow.com/questions/1150874/c-sharp-attribute-text-from-resource-file
//Only thing I changed was adding NonPublic to binding flags since our images come from other dll's
// and making it generic, as the original only supports strings
public class ResourceHelper
{
public static T GetResourceLookup<T>(Type resourceType, string resourceName)
{
if ((resourceType != null) && (resourceName != null))
{
PropertyInfo property = resourceType.GetProperty(resourceName, BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic);
if (property == null)
{
return default(T);
}
return (T)property.GetValue(null, null);
}
return default(T);
}
}
我用屬性的顯示名稱遇到了這個問題,我做了以下更改:
對於我們的資源文件,我將自定義工具屬性更改為PublicResXFileCodeGenerator
然后將其添加到屬性:
[Display(Name = "MyResourceName", ResourceType = typeof(Resources.MyResources))]
使用字符串作為資源的名稱。 .NET使用一些內部屬性來做到這一點。
屬性的性質使得您在屬性屬性中放置的數據必須是常量。 這些值將存儲在程序集中,但永遠不會導致執行的已編譯代碼。 因此,您不能擁有依賴於執行的屬性值來計算結果。
這是我寫的東西,因為我找不到任何其他的東西:
輸入
在項目A中編寫一個常量字符串類。
[GenerateResource]
public static class ResourceFileName
{
public static class ThisSupports
{
public static class NestedClasses
{
[Comment("Comment value")]
public const string ResourceKey = "Resource Value";
}
}
}
產量
並且將在包含常量類的項目中生成資源。
您需要做的就是在某個地方使用此代碼:
資源
public class CommentAttribute : Attribute
{
public CommentAttribute(string comment)
{
this.Comment = comment;
}
public string Comment { get; set; }
}
public class GenerateResourceAttribute : Attribute
{
public string FileName { get; set; }
}
public class ResourceGenerator
{
public ResourceGenerator(IEnumerable<Assembly> assemblies)
{
// Loop over the provided assemblies.
foreach (var assembly in assemblies)
{
// Loop over each type in the assembly.
foreach (var type in assembly.GetTypes())
{
// See if the type has the GenerateResource attribute.
var attribute = type.GetCustomAttribute<GenerateResourceAttribute>(false);
if (attribute != null)
{
// If so determine the output directory. First assume it's the current directory.
var outputDirectory = Directory.GetCurrentDirectory();
// Is this assembly part of the output directory?
var index = outputDirectory.LastIndexOf(typeof(ResourceGenerator).Assembly.GetName().Name);
if (index >= 0)
{
// If so remove it and anything after it.
outputDirectory = outputDirectory.Substring(0, index);
// Is the concatenation of the output directory and the target assembly name not a directory?
outputDirectory = Path.Combine(outputDirectory, type.Assembly.GetName().Name);
if (!Directory.Exists(outputDirectory))
{
// If that is the case make it the current directory.
outputDirectory = Directory.GetCurrentDirectory();
}
}
// Use the default file name (Type + "Resources") if one was not provided.
var fileName = attribute.FileName;
if (fileName == null)
{
fileName = type.Name + "Resources";
}
// Add .resx to the end of the file name.
fileName = Path.Combine(outputDirectory, fileName);
if (!fileName.EndsWith(".resx", StringComparison.InvariantCultureIgnoreCase))
{
fileName += ".resx";
}
using (var resx = new ResXResourceWriter(fileName))
{
var tuples = this.GetTuplesRecursive("", type).OrderBy(t => t.Item1);
foreach (var tuple in tuples)
{
var key = tuple.Item1 + tuple.Item2.Name;
var value = tuple.Item2.GetValue(null);
string comment = null;
var commentAttribute = tuple.Item2.GetCustomAttribute<CommentAttribute>();
if (commentAttribute != null)
{
comment = commentAttribute.Comment;
}
resx.AddResource(new ResXDataNode(key, value) { Comment = comment });
}
}
}
}
}
}
private IEnumerable<Tuple<string, FieldInfo>> GetTuplesRecursive(string prefix, Type type)
{
// Get the properties for the current type.
foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static))
{
yield return new Tuple<string, FieldInfo>(prefix, field);
}
// Get the properties for each child type.
foreach (var nestedType in type.GetNestedTypes())
{
foreach (var tuple in this.GetTuplesRecursive(prefix + nestedType.Name, nestedType))
{
yield return tuple;
}
}
}
}
然后使用[GenerateResource]
一個引用所有程序集的小項目
public class Program
{
static void Main(string[] args)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
string path = Directory.GetCurrentDirectory();
foreach (string dll in Directory.GetFiles(path, "*.dll"))
{
assemblies.Add(Assembly.LoadFile(dll));
}
assemblies = assemblies.Distinct().ToList();
new ResourceGenerator(assemblies);
}
}
然后,您的屬性可以使用靜態類ResourceFileName.ThisSupports.NestedClasses.ResourceKey
而其他代碼可以使用資源文件。
您可能需要根據您的特定需求進行定制。
我有一個類似的情況,我需要將資源字符串放入屬性。 在C#6中,我們有nameof()
功能,這似乎可以解決問題。
在我的情況下,我可以使用[SomeAttribute(nameof(Resources.SomeResourceKey))]
並且它編譯得很好。 然后我只需要在另一端做一些工作就可以使用該值從Resources文件中獲取正確的字符串。
在您的情況下,您可以嘗試:
[IntegerValidation(1, 70, ErrorMessageResourceKey = nameof(Data.Messages.Speed))]
private int i_Speed;
然后你可以按照(偽代碼)的方式做一些事情:
Properties.Resources.ResourceManager.GetString(attribute.ErrorMessageResourceKey);
如果您使用的是.NET 3.5或更高版本,則可以使用ErrorMessageResourceName
和ErrorMessageResourceType
參數。
例如[Required(ErrorMessageResourceName ="attribute_name" , ErrorMessageResourceType = typeof(resource_file_type))]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.