'ErrorMessageResourceType'未找到指定的属性。关于 XmlSerialise

发布于 2024-09-04 23:13:47 字数 751 浏览 3 评论 0原文

在我的 ASP.Net MVC 应用程序中,我有一个模型层,它在业务对象上使用本地化验证注释。

代码如下所示:

[XmlRoot("Item")]
public class ItemBo : BusinessObjectBase
{
    [Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof(StringResource))]
    [HelpPrompt("ItemNumber")]
    public long ItemNumber { get; set; }

这效果很好。

当我想将对象序列化为 xml 时,出现错误:

“未找到指定的‘ErrorMessageResourceType’属性”(尽管它在其他错误下丢失,但它是我正在尝试处理的内部异常。

因此问题是使用相关的资源文件位于另一个程序集中,并被标记为“公共”,正如我所说,在我进行序列化之前,一切都正常。

在我的 nunit 测试和目标类中引用了相关的 DataAnnotations 类等。

顺便说一句,HelpPrompt 是我在其他地方定义的另一个数据注释,并且不会导致问题。

此外,如果我将必需属性更改为标准格式,序列化工作正常,

        [Required(ErrorMessage="Error")]

有人可以帮助我吗?

In my ASP.Net MVC app I have a Model layer which uses localised validation annotations on business objects.

The code looks like this:

[XmlRoot("Item")]
public class ItemBo : BusinessObjectBase
{
    [Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof(StringResource))]
    [HelpPrompt("ItemNumber")]
    public long ItemNumber { get; set; }

This works well.

When I want to serialise the object to xml I get the error:

"'ErrorMessageResourceType' property specified was not found" (although it is lost beneath other errors, it is the innerexception I am trying to work on.

The problem therefore is the use of the DataAnnotations attributes. The relevant resource files are in another assembly and are marked as 'public' and as I said everything works well until I get to serialisation.

I have references to the relevant DataAnnotations class etc in my nunit tests and target class.

By the way, the HelpPrompt is another data annotation I have defined elsewhere and is not causing the problem.

Furthermore if I change the Required attribute to the standard format as follows, the serialisation works ok.

        [Required(ErrorMessage="Error")]

Can anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

今天小雨转甜 2024-09-11 23:13:47

啊哈,答案比我想象的要容易。简而言之,StringResource 程序集中不存在“RequiredField”公共静态属性。

问题是找到错误。序列化对象时,我必须在尝试实例化序列化程序时捕获异常

serial = new XmlSerializer(doc.GetType());

,然后通过 InnerExceptions 层次结构来分析产生的 InvalidOperationException 并获取准确的错误消息,告诉我出了什么问题:

资源类型“StringResource”没有名为“RequiredField”的公开可见静态属性。

现在工作正常

Aha, the answer was easier than I expected. In short the "RequiredField" public static property did not exist in the StringResource assembly.

The issue was finding the error. When serialising the object I had to catch the exception on the attempt to instantiate the serialiser

serial = new XmlSerializer(doc.GetType());

and then work my way dfown through a hierarchy of InnerExceptions to analyse the InvalidOperationException that resulted and get the precise error message which told me what was wrong:

The resource type 'StringResource' does not have a publicly visible static property named 'RequiredField'.

Works ok now

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文