I have the following code in a c# class library...
public static class foo
{
public enum bar
{
bsNone = -1,
bsSolid = 0,
bsDash = 1
}
}
And in a VB.Net Winforms application I reference the enum as the return type of a property:
Private _LeftBorderStyle As foo.bar
Public Property LeftBorderStyle() As foo.bar
Get
Return _LeftBorderStyle
End Get
Set(ByVal value As foo.bar)
_LeftBorderStyle = value
End Set
End Property
When I build the VB.Net project I get the following warning:
Return type of function 'LeftBorderStyle' is not CLS-compliant.
Can you tell me why the enum is non-CLS compliant?
This is happening because you are publicly exposing from an assembly marked as CLS-Compliant a type that is from an assembly that is not CLS-Compliant.
Note that you are allowed to consume a type that is not CLS-Compliant in a CLS-Compliant assembly; but you are not allowed to expose such a type.
For example, assume you have this class in a non-CLS-Compliant assembly:
Now assume you have the following class in a CLS-Compliant assembly that references the non-CLS-Compliant assembly:
The compiler will NOT warn you about
MyTest1()'suse of the typeMyEnumfrom a non-Compliant assembly, because it is only being used internally.But it WILL warn you about exposing it as the public return type of
MyTest2().If you make the non-CLS-Compliant assembly compliant by adding
[assembly: CLSCompliant(true)]toAssemblyInfo.cs, then the code will all compile without a warning.To reiterate: If you use a type defined in a non-compliant assembly, that type is automatically non-compliant, even if it is just something basic like an enum.
From the Microsoft documentation for CLSCompliantAttribute: