在此上下文中不能使用[XYZ]类型

时间:2016-02-03 09:05:55

标签: c# xml-serialization

我收到一个System.InvalidOperationException,并显示消息“类型WindowsFormsApplication1.Category可能不会在此上下文中使用。”

[Serializable]
public enum EAction
{
    defaultAction,
}

public class Configuration : List<Product>
{
}

public class ConfigurationItem
{
    [XmlAttribute("name")]
    public string Name { get; set; }


    [XmlAttribute("action")]
    public EAction Action { get; set; }
}

public class ConfigurationItemCollection : ConfigurationItem, IEnumerable<ConfigurationItem>
{
    [XmlIgnore]
    public IEnumerable<ConfigurationItem> Children
    {
        get
        {
            return _items;
        }
    }

    private List<ConfigurationItem> _items = new List<ConfigurationItem>();

    public virtual void Add(object item)
    {
        ConfigurationItem cfgItem = item as ConfigurationItem;
        if (cfgItem != null)
            _items.Add(cfgItem);
    }

    public IEnumerator<ConfigurationItem> GetEnumerator()
    {
        return _items.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return _items.GetEnumerator();
    }
}

[XmlInclude(typeof(Category))]
public class Product : ConfigurationItemCollection
{
}

[XmlInclude(typeof(Addon))]
public class Category : ConfigurationItemCollection
{
}

public class Addon : ConfigurationItem
{
}

用法:             配置cfg = new Configuration();

        Product product = new Product();
        product.Name = "Product";
        product.Action = EAction.defaultAction;
        cfg.Add(product);

        Category category = new Category();
        category.Name = "Category";
        category.Action = EAction.defaultAction;

        Addon addon = new Addon();
        addon.Name = "AddOn";
        addon.Action = EAction.defaultAction;

        product.Add(category);
        product.Add(addon);

        Addon addon2 = new Addon();
        addon2.Name = "AddOn 2";
        addon2.Action = EAction.defaultAction;
        category.Add(addon2);

        Addon addon3 = new Addon();
        addon3.Name = "AddOn 3";
        addon3.Action = EAction.defaultAction;
        category.Add(addon3);

        XmlSerializer x = new XmlSerializer(typeof(Configuration));
        TextWriter writer = new StreamWriter(@"d:\temp\cfg.xml");
        x.Serialize(writer, cfg);

例外的原因是什么?

更新了代码。现在可以重现异常。

1 个答案:

答案 0 :(得分:0)

好吧,我已经重新修改了我的课程,并以其他方式得到了我需要的结果:

[Serializable]
public enum EAction
{
    defaultAction,
}

[XmlRoot("Configuration")]
public class Configuration
{
    public Configuration()
    {
        Products = new List<Product>();
    }

    [XmlElement("Product", Type = typeof(Product))]
    public List<Product> Products { get; set; }
}

public class ConfigurationItem
{
    [XmlAttribute("name")]
    public string Name { get; set; }


    [XmlAttribute("action")]
    public EAction Action { get; set; }
}

public class Product : ConfigurationItem
{
    public Product()
    {
        Items = new List<ConfigurationItem>();
    }

    [XmlElement("Category", Type = typeof(Category))]
    [XmlElement("Addon", Type = typeof(Addon))]
    public List<ConfigurationItem> Items { get; set; }
}

public class Category : ConfigurationItem
{
    public Category()
    {
        Items = new List<ConfigurationItem>();
    }

    [XmlElement("Category", Type = typeof(Category))]
    [XmlElement("Addon", Type = typeof(Addon))]
    public List<ConfigurationItem> Items { get; set; }
}

public class Addon : ConfigurationItem
{
}