为什么我的代码不符合CLS?

时间:2012-01-04 14:28:53

标签: c# .net cls-compliant

我在构建项目时遇到错误:

  

警告为错误:'OthersAddresses.AddresseTypeParameter'的类型不符合CLS C:... \ Units \ OthersAddresses.ascx.cs

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

和这一个:

  

警告为错误:'Global.UserInSession'的类型不符合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;
    }
}

我在[CLSCompliant(false)]UserInSession之前添加了属性AddresseTypeParameter并且它有效,但我想了解为什么它不符合CLS。

有关类和枚举的更多信息:

班级用户(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
        }

枚举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
    }

2 个答案:

答案 0 :(得分:8)

您需要使用CLSCompliantAttribute

  

如果没有CLSCompliantAttribute应用于程序元素,则默认情况下:

     
      
  • 装配不符合CLS。
  •   
  • 仅当其封闭类型或程序集符合CLS时,该类型才符合CLS。
  •   
  • 仅当类型符合CLS时,类型的成员才符合CLS。
  •   

除此之外,你需要确保你的装配确实是CLS compliant

答案 1 :(得分:1)

这是为了让您知道“其他类型的AddAdresses.AdresseTypeParameter'不符合CLS”,而不是属性必然。看一下 types ,而不是属性,你可能会发现导致警告的原因。