Why is my class not CLS-compliant?

瘦欲@ 提交于 2019-12-30 18:26:06

问题


This really baffles me. I've tried removing the readonly, changing names.. What am I doing wrong here?

public abstract class CatalogBase<T> where T : class
{
    protected readonly String DataPath;
    protected readonly XmlSerializer Serializer;
    private readonly XmlSerializerNamespaces _namespaces;

    protected CatalogBase(String dataPath)
    {
        DataPath = dataPath;
        Serializer = new XmlSerializer(typeof (T));
        _namespaces = new XmlSerializerNamespaces();
        _namespaces.Add(String.Empty, String.Empty);
    }

    public virtual void Write(T obj)
    {
        var streamWriter = new StreamWriter(DataPath);

        Serializer.Serialize(streamWriter, obj, _namespaces);
        streamWriter.Close();
    }

    public abstract IDictionary<String, T> Read();
}

Edit:

The warning:

Warning 1 'Ar.ViewModel.Workspaces.MaterialCatalogBase': base type 'Or.Files.CatalogBase' is not CLS-compliant C:_Center_Work_Programming_Cs\Ar\Ar\ViewModel\Workspaces\MaterialCatalogBase.cs 9 18 Ar

Edit2:

Even if I change the class as below I still get the error:

public abstract class CatalogBase<T> where T : class
{
    protected readonly String DataPath;
    protected readonly XmlSerializer Serializer;
    private readonly XmlSerializerNamespaces namespaces;

    protected CatalogBase(String dataPath)
    {
        DataPath = dataPath;
        Serializer = new XmlSerializer(typeof (T));
        namespaces = new XmlSerializerNamespaces();
        namespaces.Add(String.Empty, String.Empty);
    }

    public virtual void Write(T obj)
    {
        var streamWriter = new StreamWriter(DataPath);

        Serializer.Serialize(streamWriter, obj, namespaces);
        streamWriter.Close();
    }

    public abstract IDictionary<String, T> Read();
}

Also, I've forgotten to mention that I get two (exactly the same errors) for some reason.. ?


回答1:


Looks like you have the following:

  • Assembly A declares CatalogBase<T>. Assembly A is not marked as CLSCompliant
  • Assembly B references assembly A. Assembly B declares MaterialCatalogBase : CatalogBase<T>. Assembly B is marked as CLSCompliant

If it is your case - then assembly in which your CatalogBase<T> class located should be marked with CLSCompliant attribute:

[assembly: CLSCompliant(true)]


来源:https://stackoverflow.com/questions/15739511/why-is-my-class-not-cls-compliant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!