簡體   English   中英

使用反射從資源中獲取價值

[英]Get value from resource using Reflection

您好,我正在嘗試使用反射從資源文件中的Key中獲取值。 但是我正在努力。 我建立了名稱空間和區域性,但值始終為空。

    public static string GetResource(string language)
    {
        var culture = (language == "en-US") ? string.Empty : $".{language}";

        var resource = Assembly
            .GetExecutingAssembly()
            .GetTypes()
            .Where(x => x.Name.Contains("Test"))
            .Single(x => x.Namespace == $"ConsoleApp1.Resources{culture}");
        resource.GetProperty("Hello");//this is always null

        return null;
    }

我不確定為什么會這樣,但是我知道我正在獲取正確的資源,並且看到其中的屬性。 有人有任何建議嗎?

您好屬性:

    internal static string Hello {
        get {
            return ResourceManager.GetString("Hello", resourceCulture);
        }
    }

由於“ Hello”屬性是internalstatic ,因此我們應指定它:

resource.GetProperty("Hello", BindingFlags.NonPublic | BindingFlags.Static);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM