.Resx 属性上的 .NET GetProperty
基于 http://www.thinkingguy.net /2010/01/localizing-labelfor-in-aspnet-mvc-2.html
我正在尝试使用反射来获取 resx 文件中的字符串属性
var propertyInfo = _resourceType.GetProperty(resourcePropertyName, BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
我的 MVC2 项目中有一个资源文件夹,其中包含自动生成属性的资源文件
public static string Dagrapport_Datum {
get {
return ResourceManager.GetString("Dagrapport_Datum", resourceCulture);
}
}
无论我传递给 GetProperty 它都保持为空...... 有什么线索可以解释为什么会这样吗?
Based on http://www.thinkingguy.net/2010/01/localizing-labelfor-in-aspnet-mvc-2.html
I'm trying to use reflection to get to a string property in a resx file
var propertyInfo = _resourceType.GetProperty(resourcePropertyName, BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
I have a resources folder in my MVC2 project with a resource file that autogenerated a Property
public static string Dagrapport_Datum {
get {
return ResourceManager.GetString("Dagrapport_Datum", resourceCulture);
}
}
Whatever I pass to GetProperty It just stays null....
Any clues as to why this could be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 BindingFlags 与属性的签名不匹配。
您需要 BindingFlags.Static | BindingFlags.Public 或许还有 BindingFlags.GetProperty。
编辑:最好也设置 BindingFlags.NonPublic 。
因此 GetProperty() 搜索所有静态、公共或非公共(内部、私有、受保护)属性。
Your BindingFlags doesn't match with the signature of the property.
You need BindingFlags.Static | BindingFlags.Public and maybe BindingFlags.GetProperty.
Edit: Its better to set BindingFlags.NonPublic too.
So GetProperty() searches for all Static, Public or NonPublic (internal, private, protected) properties.
如果您将访问修饰符设置为“内部”,则此操作应该有效。
如果您将访问修饰符设置为“公共”,则可以将
NonPublic
更改为Public
。这也有效:
在此示例中,我有一个名为
TestResource
的资源,其属性为SomeProperty
。This should work if you have your Acces Modifier set to "Internal"
If you set the Acces Modifier to "Public" you can change
NonPublic
toPublic
instead.This also works:
In this example I've got a Resource called
TestResource
with a propertySomeProperty
.