为什么我的课不符合CLS?(Why is my class not CLS-compliant?)

2019-08-18 12:03发布

这我百思不得其解。 我试着去掉只读,更改名称..我在做什么错在这里?

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();
}

编辑:

警告:

警告1 'Ar.ViewModel.Workspaces.MaterialCatalogBase':基类型'Or.Files.CatalogBase'是不符合CLS C:_Center_Work_Programming_Cs \氩\氩\视图模型\工作区\ MaterialCatalogBase.cs 9 18氩

EDIT2:

即使我改变类下面我仍然得到错误:

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();
}

另外,我忘了提,我得到两个(完全相同的错误),由于某种原因..?

Answer 1:

看起来你具备以下条件:

  • 组件A声明CatalogBase<T> 组件A没有标记为CLSCompliant
  • 组件B的引用组件A组件B宣称MaterialCatalogBase : CatalogBase<T> 组件B 标记为CLSCompliant

如果它是你的情况下-然后组件,其中,您的CatalogBase<T>位于应标明CLSCompliant属性类:

[assembly: CLSCompliant(true)]


文章来源: Why is my class not CLS-compliant?