calledType.InvokeMember 不工作。如何获取资源的价值?

calledType.InvokeMember don't work. How to get value of resource?

我尝试获取 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);                                       }

证书 = "PRG_CARTV"

calledType 是:{Name = "Resources" FullName = "TestNamespace.Resources"} 当我排队时 "String s = (String)calledType" 我有一个错误:"Method 'TestNamespace.Resources.PRG_CARTV' not found."

当我有 String s = TestNamespace.Resources.PRG_CARTV; 它的工作,所以我不明白..

当我简单地做时:

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

它不起作用,我有一个错误:"Can not find appropriate resources for the specified culture or neutral culture. Make sure \ "TestNamespace.Ressources.resources\"已正确嵌入或链接到程序集中..."

你这里有 2 个问题:

1) 通过反射获取资源的值,我尝试了这个并成功了:

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

注意已更改的绑定标志:BindingFlags.GetProperty 而不是 BindingFlags.InvokeMethodBindingFlags.NonPublic 而不是 BindingFlags.Public

2) ResourceManager 问题。我自己会尝试再次重新创建 Resources.resx。如果您想进一步调查,请在 Whosebug 中查看类似问题,例如:Could not find any resources appropriate for the specified culture or the neutral culture