为什么我的班级不符合CLS?

时间:2013-04-01 07:36:02

标签: c# .net-4.5 cls-compliant

这真让我感到困惑。我已经尝试删除只读,更改名称..我在这里做错了什么?

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 \ Ar \ Ar \ ViewModel \ Workspaces \ MaterialCatalogBase.cs 9 18 Ar

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

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

1 个答案:

答案 0 :(得分:4)

看起来你有以下内容:

  • 程序集A声明CatalogBase<T>。程序集A 标记为CLSCompliant
  • 程序集B引用程序集A.程序集B声明MaterialCatalogBase : CatalogBase<T>。程序集B 标记为CLSCompliant

如果是你的情况 - 那么你的CatalogBase<T>类所在的程序集应该用CLSCompliant属性标记:

[assembly: CLSCompliant(true)]