Why is my class not CLS-compliant?
Asked Answered
K

1

8

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

Klotz answered 1/4, 2013 at 7:36 Comment(12)
What is the exact warning message you're receiving?Disconcerted
We're not compilers, so please show where you get the non-CLS-compilance warning.Sanctitude
I don't actually get a warning when I compile this, everything looks fine.Disconcerted
Same here - no warnings (.Net4 on VS2012)Illmannered
@DanielImms - See the example : msdn.microsoft.com/en-us/library/bhc3fa7f.aspxProbationer
msdn.microsoft.com/en-us/library/x8ak87y5%28v=vs.90%29.aspxAcetone
It says private is okay for the leading underscore.Antiphrasis
The CLS compliance police will come after you.Champagne
I wish it did, maybe it could coherently tell me what is wrong instead of giving me a cryptic warning msg.Klotz
Are your classes, MaterialCatalogBase and CatalogBase, in different assemblies?Aroid
They are, actually. MaterialCatalogBase is in a WPF Application project and CatalogBase is in a Class Library. Is that a problem?Klotz
@Pavel: Then perhaps the other assembly isn't marked as CLS Compliant? (or the class in it isn't).Illmannered
F
7

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)]
Fearfully answered 1/4, 2013 at 8:41 Comment(2)
Thank you. The assembly containing my container class wasn't marked, but the consuming assemblies are. Why the previous devs felt the need to ensure that this fully-contained webapp had to be CLS-compliant, IDK, but there we are.Petrology
@ScottPeterson, if there is no need anymore for it to be CLS-compliant, you can also end the compliance.Oldie

© 2022 - 2024 — McMap. All rights reserved.