Why is my code not compatible with CLS?

I had errors while creating my project:

Warning as error: type "OtherAddresses.AddresseTypeParameter" is not compatible with CLS. C: ... \ Units \ OthersAddresses.ascx.cs

public Address.AddressTypeEnum AddressTypeParameter { get { return _addressTypeParameter; } set { _addressTypeParameter = value; } } 

and this one:

Warning as error: Type 'Global.UserInSession' does not match CLS C: ... \ Global.asax.cs

 public static User UserInSession { get { return (HttpContext.Current.Session["CurrentUser"] == null) ? null : HttpContext.Current.Session["CurrentUser"] as User; } set { HttpContext.Current.Session["CurrentUser"] = value; } } 

I added the [CLSCompliant(false)] UserInSession to UserInSession and AddresseTypeParameter , and it works, but I would like to understand why it is not compatible with CLS.

Additional information about classes and enumerations:

Class User (User.cs)

 public class User { private string _uniqueIdentifier; private string _password = string.Empty; private string _email = string.Empty; private string _passwordQuestion = string.Empty; private string _passwordAnswer = string.Empty; private string _id_directions_db = string.Empty; private string _id_gesab = string.Empty; private string _zipCode = string.Empty; private string _fonction_id = string.Empty; private string _fonction = string.Empty; private string _structure_id = string.Empty; private string _structure = string.Empty; private string _firstName = string.Empty; private string _lastName = string.Empty; private string _company = string.Empty; private string _avatarPath = string.Empty; private Role _role = new Role(); private List<Address> _addressList = new List<Address>(); private string _otherInformation = string.Empty; private MembershipUser _membershipUserAssociated = null; ... public enum GenderEnum { Empty = 0, Monsieur, Madame } 

and

enum AddressTypeEnum (Address.cs)

 public class Address { private AddressTypeEnum _addressType; private string _firstName = string.Empty; private string _lastName =string.Empty; private string _structure = string.Empty; private string _structureComplementary = string.Empty; private string _addressStreet = string.Empty; private string _addressComplementary = string.Empty; private string _bp = string.Empty; private string _zipCode = string.Empty; private string _country = string.Empty; private string _countryId = string.Empty; private string _city = string.Empty; private string _phone = string.Empty; private string _fax = string.Empty; private string _email = string.Empty; public enum AddressTypeEnum { Empty = 0, Personal = 1, Billing = 2, Delivery = 3 } 
+8
c # cls-compliant
source share
2 answers

You need to use CLSCompliantAttribute :

If CLSCompliantAttribute is not applied to a program element, then by default:

  • The assembly is not CLS compliant.
  • A type is CLS-compatible only if its closing type or assembly is CLS-compatible.
  • An element of type CLS-compatible only if the type is CLS-compatible.

In addition, you need to make sure that your build is truly CLS compatible .

+8
source share

This lets you know that the "Type" OtherAdresses.AdresseTypeParameter is "not CLS compliant," and not that the properties are required. Take a look at types, not properties, and you will probably find what triggers the warning.

+1
source share

All Articles