Why is this parameter not CLS-compliant?
Asked Answered
M

1

9

I am trying to eliminate all the CLS-compliant errors that we have throughout out solution. While I have managed to sort out several (e.g. public variables beginning with an underscore and using unsigned integers), there are a few which I can't seem to find the reason why they are failing.

I have pasted the all the code relevant to one of the problems, there is a fair amount of code, but it only consists of a form constructor, interface and class that implements the interface.

VB.Net form constructor (the warning is on the unitOfWork parameter and reads Type of parameter 'unitOfWork' is not CLS-compliant:

Public Sub New(ByRef unitOfWork As IUnitOfWork)

    MyBase.New()
    InitializeComponent()

    _unitOfWork = unitOfWork

End Sub

The interface IUnitOfWork is as follows:

public interface IUnitOfWork : IDisposable
{
    bool IsInTransaction { get; }

    bool IsDirty { get; }

    object BeginTransaction();

    object BeginTransaction(IsolationLevel isolationLevel);

    void Commit();

    void Rollback();

    object Session { get; }
}

And the one and only implementation of the interface (and therefore the an instance of which will be passed to the form constructor) is this:

public class UnitOfWork : IUnitOfWork
{
    private ISession _session;

    public UnitOfWork(SMSession session)
    {
        if (session == null)
        {
            throw new ArgumentNullException("Session wasn't supplied");
        }

        this._session = (ISession)session.OpenSession();
    }

    public bool IsInTransaction
    {
        get { return this._session.Transaction.IsActive; }
    }

    public bool IsDirty
    {
        get { return this._session.IsDirty(); }
    }

    public object Session
    {
        get { return this._session; }
    }

    public object BeginTransaction() 
    {
        return this._session.BeginTransaction(IsolationLevel.ReadCommitted);
    }

    public object BeginTransaction(IsolationLevel isolationLevel) 
    {
        return this._session.BeginTransaction(isolationLevel);
    }

    public void Commit()
    {
        if (this._session.Transaction == null)
        {
            throw new NullReferenceException
                ("The Transaction has not been initialised");
        }

        this._session.Transaction.Commit();
    }

    public void Rollback()
    {
        if (this._session.Transaction == null)
        {
            throw new NullReferenceException
                ("The Transaction has not been initialised");
        }

        this._session.Transaction.Rollback();
    }

    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                this._session.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        if (this._session != null)
        {
            this._session.Dispose();
        }

        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

I am either being blind to the issue (could be the case as I have been staring at this for ages!) or there is another CLS-compliance restriction that I am unaware of. Any suggestions?

Manley answered 9/10, 2013 at 16:0 Comment(7)
is the _unitOfWork variable private or is it another accessibility? When I had a similar issue in C# it was because I had a protected variable that started with _. The CLS does not allow leading underscores in names that are exposed to other classes.Trin
Brute-force approach: try commenting out methods in the interface to see if that makes the warning disappear?Amando
@MarcGravell Not sure, searching found me a question that implies ref parameters are compliant: #12598491Amando
I cannot repro the warning using the code you have postedImmoderate
CLS compliance has to be met by the whole object graph, right, so you need to check SMSession and ISession too?Lobectomy
@Lobectomy IUnitOfWork doesn't touch thoseImmoderate
Is the assembly that declares IUnitOfWork marked as CLS-compliant?Immoderate
S
14

My guess is that your C# library itself is not marked with CLSCompliant. I just duplicated the error, with the VB project marked CLSCompliant, and the C# library that it references is not marked CLSCompliant.

I marked the C# library CLSCompliant, and the warning went away.

Summersummerhouse answered 9/10, 2013 at 16:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.