Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 访问自定义属性中的资源键_C#_Enums - Fatal编程技术网

C# 访问自定义属性中的资源键

C# 访问自定义属性中的资源键,c#,enums,C#,Enums,我已经编写了一个自定义属性来支持枚举描述符的本地化 public class LocalizedDescriptionAttribute : DescriptionAttribute { ResourceManager _resourceManager; string _resourceKey; public LocalizedDescriptionAttribute(string resourceKey, Type resourceType) {

我已经编写了一个自定义属性来支持枚举描述符的本地化

 public class LocalizedDescriptionAttribute : DescriptionAttribute
{
    ResourceManager _resourceManager;
    string _resourceKey;

    public LocalizedDescriptionAttribute(string resourceKey, Type resourceType)
    {
        _resourceManager = new ResourceManager(resourceType);
        _resourceKey = resourceKey;
    }

    public override string Description
    {
        get
        {
            string description = _resourceManager.GetString(_resourceKey);
            return string.IsNullOrWhiteSpace(description) ? string.Format("[[{0}]]", _resourceKey) : description;
        }
    }
}
我支持这样的本地化

 public enum Shape
{

    [LocalizedDescription("STR_SQUARE", typeof(MyResources))]
    SQUARE

}
但在这里,如果从资源文件中删除资源字符串,则不会出现编译时错误。我希望以这样的方式编写属性:如果从资源文件中删除了资源字符串,我的代码将给出编译错误

我不知道为什么我不能做到这一点,我如何才能实现下面提到的自定义属性

 public enum Shape
{

    [LocalizedDescription(MyResources.STR_SQUARE, typeof(MyResources))]
    SQUARE

}

您的资源位于资源文件中这一事实不允许您获得编译时错误。资源文件仅在运行时读取。您可能需要将资源列表移动到代码中,或者将验证作为单元/集成测试的一部分进行处理。资源文件中包含资源这一事实不允许您出现编译时错误。资源文件仅在运行时读取。您可能需要将资源列表移动到代码中,或者将验证作为单元/集成测试的一部分进行处理。