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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With