本文介绍了为什么我的课程不符合 CLS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

警告:

警告 1 'Ar.ViewModel.Workspaces.MaterialCatalogBase':基本类型'Or.Files.CatalogBase' 不是符合 CLS 的 C:_Center_Work_Programming_CsArArViewModelWorkspacesMaterialCatalogBase.cs 9 18 Ar

编辑 2:

即使我按如下方式更改类,我仍然收到错误:

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.. ?

推荐答案

看起来您有以下内容:

  • 程序集 A 声明 CatalogBase.程序集 A 标记为 CLSCompliant
  • 程序集 B 引用程序集 A.程序集 B 声明 MaterialCatalogBase : CatalogBase.程序集 B 标记为 CLSCompliant
  • 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

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

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

[assembly: CLSCompliant(true)]

这篇关于为什么我的课程不符合 CLS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 07:10