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

标签 c# .net cls-compliant

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

Warning as Error: Type of 'OthersAddresses.AddresseTypeParameter' is not CLS-compliant C:...\Units\OthersAddresses.ascx.cs

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

还有这个:

Warning as Error: Type of 'Global.UserInSession' is not CLS-compliant 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;
    }
}

我在 UserInSessionAddresseTypeParameter 之前添加了属性 [CLSCompliant(false)] 并且它有效,但我想了解为什么它不是符合 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
    }

最佳答案

您需要使用 CLSCompliantAttribute :

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

  • The assembly is not CLS-compliant.
  • The type is CLS-compliant only if its enclosing type or assembly is CLS-compliant.
  • The member of a type is CLS-compliant only if the type is CLS-compliant.

除此之外,您还需要确保您的程序集确实是 CLS compliant .

关于c# - 为什么我的代码不符合 CLS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8728468/

相关文章:

c# - "Cloning" Entity Framework 中的 EntityConnections 和 ObjectContext

c# - 比较两个函数的负载/性能

c# - 相同的GetHashCode()用于不同的对象

.net - .NET 中的 CIL、CLS 和 CTS

c# - 为什么我的标识符被标记为不符合 CLS?

c# - 用C#制作星形金字塔

c# - TemplateBuilder 和 SideWaffle。项目模板。更改默认根命名空间

c# - 非零索引数组

c# - 可视化编程语言控制

.net - CLSCompliant(true) 拖入未使用的引用