DisplayAttribute:没有很好的在App_GlobalResources文件资源 [英] DisplayAttribute: No good for resources in App_GlobalResources

查看:211
本文介绍了DisplayAttribute:没有很好的在App_GlobalResources文件资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题适用于所有项目真的,但是我展示的环境是一个MVC3应用程序,因此该标签。

This question applies to all projects really, however my demonstrating environment is an MVC3 app, hence that tag.

我写了 DisplayNameAttribute 的派生类为我们的MVC2项目,像许多人都有,所以我可以定位在MVC编辑和显示模板显示名称。我抬起用于错误信息,使得它在一个标准的方式表现了 ValidationAttribute 定位code。

I wrote a DisplayNameAttribute-derived class for our MVC2 projects, like so many people have, so I could localise display names in MVC editors and display templates. I lifted the ValidationAttribute localisation code used for the error message so that it behaved in a 'standard' way.

然后,我注意到,在MVC3我们有 DisplayAttribute ,所以我尽职尽责地废弃了我的属性类在.NET 4的版本我MVC扩展的框架,改变了模特属性在我目前的项目中使用此,例如:

Then I noticed that in MVC3 we have the DisplayAttribute, so I dutifully obsoleted my attribute class in the .Net 4 version of my MVC extension framework and changed the model properties in my current project to use this, e.g:

[Display(ResourceType = typeof(Resources.MyResources), Name = "ResourceName")]
public string ModelProperty { get; set; }

然后我运行Web应用程序,我得到类似这样的错误:

Then I run the web app and I get an error similar to this:

System.InvalidOperationException:无法检索属性'名称',因为定位失败。键入[资源类型在这里]不公开或不包含与名称的公共静态字符串属性[资源名称这里]。

System.InvalidOperationException: Cannot retrieve property 'Name' because localization failed. Type '[Resource Type Here]' is not public or does not contain a public static string property with the name '[Resource Name Here]'.

我当然各个地方使用的定位,并在类库这些资源将被通常被标记为公开。在Web应用程序,但是,我会使用 App_GlobalResources文件文件夹,因为,这就是它了!

I use localisation in various places of course, and in class libraries such resources will be typically be marked public. In web applications, however, I will use the App_GlobalResources folder because, well, that's what it's for!

这里的问题是,这个文件夹中生成的RESX文件,资源访问是总是产生的内部访问,没有办法改变它。

The problem here is that the resource accessors that are generated for resx files in this folder are always generated as internal access, with no way of changing it.

所以,既然 DisplayAttribute 似乎只是为了寻找公共成员(而本地化code,我从 ValidationAttribute 并应用到自己的 DisplayNameAttribute -deriving类型简单地查找静态成员公共或内部可见性),它会出现 DisplayAttribute 是禁地到打算使用任何MVC的Web应用程序 App_GlobalResources文件的本地化字符串?

So, since DisplayAttribute seems only to look for public members (whereas the localisation code I copied from ValidationAttribute and applied to my own DisplayNameAttribute-deriving type simply looks for static members with either public OR internal visibility), it would appear that DisplayAttribute is off-limits to any MVC web apps that intend to use App_GlobalResources for the localised strings?

除了事实,这是的也许的在 DisplayAttribute 中的错误,有其他人遇到这个问题?你是如何解决这个问题呢?我应履行 DisplayAttribute 的使用和更改所有我的资源是正确的嵌入在类库项目,并将它们作为公众?或者我应该沟DisplayAttribute支持我,我知道作品等类的!?

Apart from the fact that this is probably a bug in DisplayAttribute, has anybody else hit this problem? What did you do to solve it? Should I honour the usage of DisplayAttribute and change all my resources to be 'properly' embedded as in the class library projects, and set them all as public? Or should I ditch DisplayAttribute in favour of my other class that I know works!?

推荐答案

<打击>您应该可以改变访问修饰符。在Solution Explorer中,双击.resx文件,然后在附近的托管资源编辑器的右上方,改变的访问修饰符的距离内部公开

You should be able to change the access modifier. In Solution Explorer, double-click your .resx file, then near the top-right of the managed resource editor, change the Access Modifier drop-down from Internal to Public.

更新

这可能工作:使全球资源的公共

这篇关于DisplayAttribute:没有很好的在App_GlobalResources文件资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆