You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

动态读取资源文件属性失败,LocalizedDescription中property始终为null

枚举本地化描述获取失败问题

为了获取枚举的本地化描述,我做了以下操作:

  • 创建独立的不变式resx文件,将访问修饰符设置为Public,自动生成对应的.cs文件;同时创建了未启用代码生成的本地化资源文件。
  • 由于资源文件不会创建附属程序集,无法直接使用ResourceManager类。生成的CustomResource类代码如下:
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class CustomResource {
    
    private static global::System.Resources.ResourceManager resourceMan;
    
    private static global::System.Globalization.CultureInfo resourceCulture;
    
    [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
    internal CustomResource() {
    }

    [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
    public static global::System.Resources.ResourceManager ResourceManager {
        get {
            if (object.ReferenceEquals(resourceMan, null)) {
                global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("MyApp.Resources.CustomResource", typeof(CustomResource).Assembly);
                resourceMan = temp;
            }
            return resourceMan;
        }
    }
    
    [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
    public static global::System.Globalization.CultureInfo Culture {
        get {
            return resourceCulture;
        }
        set {
            resourceCulture = value;
        }
    }
    
    public static string Default {
        get {
            return ResourceManager.GetString("Default", resourceCulture);
        }
    }
}

接着我创建了自定义特性类LocalizedDescriptionAttribute:

public class LocalizedDescriptionAttribute : DescriptionAttribute
{
    private readonly string resourceKey;
    private readonly Type resourceType;

    public LocalizedDescriptionAttribute(string resourceKey, Type resourceType)
    {
        this.resourceKey = resourceKey;
        this.resourceType = resourceType;
    }

    public override string Description
    {
        get
        {
            //Get the property
            var property = resourceType.GetProperty(resourceKey, 
                BindingFlags.Public | BindingFlags.NonPublic | 
                BindingFlags.Static | BindingFlags.DeclaredOnly);

            //Must have getter method
            if (property != null)
            {
                var propertyGetter = property.GetMethod;
                if (propertyGetter == null || (!propertyGetter.IsAssembly && !propertyGetter.IsPublic))
                {
                    property = null;
                }
            }

            return (string)property.GetValue(null, null)!;
        }
    }
}

之后用这个特性修饰枚举:

public enum MyEnum
{
    [LocalizedDescription("Default", typeof(CustomResource))]
    Default = 0,

    [LocalizedDescription("Other", typeof(CustomResource))]
    Other = 1
}

现在遇到的问题是:LocalizedDescriptionAttribute类的Description属性中,property变量始终为null。请问我遗漏了什么?


内容的提问来源于stack exchange,提问作者Ivan Debono

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 19:05:02