开发者

.NET GetProperty on .Resx Property

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 w开发者_开发百科ith 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?


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.


This should work if you have your Acces Modifier set to "Internal"

var resource = typeof(TestResource).GetProperties(BindingFlags.Static | BindingFlags.NonPublic);
var property = resource.First(x => x.Name == "SomeProperty");

If you set the Acces Modifier to "Public" you can change NonPublic to Public instead.

This also works:

var someProperty = typeof (TestResource).GetProperty("SomeProperty", BindingFlags.Static | BindingFlags.NonPublic);

In this example I've got a Resource called TestResource with a property SomeProperty.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜