点击或拖拽改变大小

AttributeMissException 类

自定义属性未找到异常。
继承层次
SystemObject
  SystemException
    XPatchLibAttributeMissException

命名空间:  XPatchLib
程序集:  XPatchLib (在 XPatchLib.dll 中) 版本:4.1.0.4
语法
C#
[SerializableAttribute]
public class AttributeMissException : Exception

AttributeMissException 类型公开以下成员。

构造函数
  名称说明
公共方法AttributeMissException
初始化 AttributeMissException 类的新实例。
公共方法AttributeMissException(String)
使用指定的错误信息初始化 AttributeMissException 类的新实例。
公共方法AttributeMissException(String, Exception)
使用指定错误消息和对作为此异常原因的内部异常的引用来初始化 AttributeMissException 类的新实例。
公共方法AttributeMissException(Type, String)
使用指定的自定义属性名称初始化 AttributeMissException 类的新实例。
Top
属性
  名称说明
公共属性AttributeName
获取当前未找到的自定义特性的名称。
公共属性ErrorType
获取当前未找到特性的类型。
公共属性Message
获取描述当前异常的消息。
(覆写 ExceptionMessage.)
Top
方法
示例

当集合元素类型没有定义主键时,在序列化/反序列化 集合对象时会抛出 AttributeMissException 异常。

C#
using System;
using System.IO;
using System.Text;
using System.Xml;

namespace XPatchLib.Example
{
    public class ThrowAttributeMissException
    {
        public static void Main()
        {
            var t = new ThrowAttributeMissException();

            t.Divide();
        }

        private void Divide()
        {
            var order = CreatePuchaseOrder();
            var newOrder = CreatePuchaseOrder();

            newOrder.OrderedItems[0].ItemName = "Widget B";

            var serializer = new Serializer(typeof(PurchaseOrder));
            using (var fs = new MemoryStream())
            {
                using (var writer = new XmlTextWriter(fs, new UTF8Encoding(false)))
                {
                    try
                    {
                        //由于 OrderedItem 类型没有定义主键,所以在序列化/反序列化 集合对象时会抛出 AttributeMissException 异常。
                        serializer.Divide(writer, order, newOrder);
                    }
                    catch (AttributeMissException ex)
                    {
                        Console.WriteLine(ex.Message);
                        Console.WriteLine(ex.AttributeName);
                        Console.WriteLine(ex.ErrorType);
                    }
                }
            }
        }

        private PurchaseOrder CreatePuchaseOrder()
        {
            var result = new PurchaseOrder();

            var i1 = new OrderedItem();
            i1.ItemName = "Widget S";

            OrderedItem[] items = {i1};
            result.OrderedItems = items;

            return result;
        }

        public class OrderedItem
        {
            public string ItemName { get; set; }
        }

        public class PurchaseOrder
        {
            public OrderedItem[] OrderedItems;
        }
    }
}
版本信息

.NET Framework

支持版本:4.5, 4.0, 3.5, 2.0

.NET Standard

支持版本:2.0, 1.3, 1.0
参见