【问题标题】:calledType.InvokeMember don't work. How to get value of resource?称为Type.InvokeMember 不起作用。如何获得资源的价值?
【发布时间】:2019-03-15 09:43:38
【问题描述】:

我尝试获取 ressource.resx 的值,但我不能。

我愿意:

foreach (string certif in ContactCertifications)
{
    Type calledType = Type.GetType("TestNamespace.Resources");            

    String s = (String)calledType.InvokeMember(certif,BindingFlags.InvokeMethod 

    | BindingFlags.Public |BindingFlags.Static,null,null,null);                                       }

certif = "PRG_CARTV"

调用类型为:{Name = "Resources" FullName = "TestNamespace.Resources"} 当我在“String s = (String) calledType”行中时出现错误:“找不到方法'TestNamespace.Resources.PRG_CARTV'。”

当我有 String s = TestNamespace.Resources.PRG_CARTV; 时,它可以工作,所以我不理解..

当我做简单的事情时:

var myManager = new ResourceManager(typeof(Resources));
var myString = myManager.GetString("PRG_CARTV");

它不起作用,我有一个错误:“找不到指定文化或中性文化的适当资源。确保 \"TestNamespace.Ressources.resources \" 已正确嵌入或链接到程序集中.. 。”

【问题讨论】:

    标签: c# resources resx invokemember


    【解决方案1】:

    你有两个问题:

    1)通过反射获取资源的价值,我尝试了这个并且工作了:

    String s = (String) calledType.InvokeMember(certif, BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Static, null, null, null);
    

    注意更改的绑定标志:BindingFlags.GetProperty 代替 BindingFlags.InvokeMethodBindingFlags.NonPublic 代替 BindingFlags.Public

    2) 资源管理器的问题。我自己会尝试再次重新创建 Resources.resx。如果您想进一步调查 StackOverflow 中的类似问题,例如:Could not find any resources appropriate for the specified culture or the neutral culture

    【讨论】:

    • 您好,感谢您的回复,但此解决方案对我也不起作用。 :/ 我有 : calledType = {Name = "Resources" FullName = "TestNamespace.Resources"} 然后是一个异常:"Method 'TestNamespace.Resources.PRG_CARTV' not found" 但我会看看你发给我的链接。跨度>
    • O,我找到了如何使用 RessourceManager 获取此值,谢谢! var myManager = new ResourceManager(typeof(ConsoleApp1.Properties.Ressources)); var myString = myManager.GetString("PRG_CARTV");
    猜你喜欢
    • 1970-01-01
    • 2011-03-25
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多