Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
57
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

Chapter 146: CLSCompliantAttribute

Constructor Parameter

CLSCompliantAttribute(Boolean) Initializes an instance of the CLSCompliantAttribute class with a Boolean value indicating whether the indicated program element is CLS-compliant.

Section 146.1: Access Modifier to which CLS rules apply

using System;

[assembly:CLSCompliant(true)] namespace CLSDoc

{

public class Cat

{

internal UInt16 _age = 0;

private UInt16 _daysTillVacination = 0;

//Warning CS3003 Type of 'Cat.DaysTillVacination' is not CLS-compliant protected UInt16 DaysTillVacination

{

get { return _daysTillVacination; }

}

 

 

//Warning

CS3003

Type of 'Cat.Age' is not CLS-compliant

public UInt16 Age

{ get { return _age; } }

//valid behaviour by CLS-compliant rules public int IncreaseAge()

{

int increasedAge = (int)_age + 1;

return increasedAge;

}

}

}

The rules for CLS compliance apply only to a public/protected components.

Section 146.2: Violation of CLS rule: Unsigned types / sbyte

using System;

[assembly:CLSCompliant(true)] namespace CLSDoc

{

public class Car

{

internal UInt16 _yearOfCreation = 0;

//Warning CS3008 Identifier '_numberOfDoors' is not CLS-compliant //Warning CS3003 Type of 'Car._numberOfDoors' is not CLS-compliant public UInt32 _numberOfDoors = 0;

GoalKicker.com – C# Notes for Professionals

703

//Warning CS3003 Type of 'Car.YearOfCreation' is not CLS-compliant public UInt16 YearOfCreation

{

get { return _yearOfCreation; }

}

//Warning CS3002 Return type of 'Car.CalculateDistance()' is not CLS-compliant public UInt64 CalculateDistance()

{

return 0;

}

//Warning CS3002 Return type of 'Car.TestDummyUnsignedPointerMethod()' is not CLS-compliant public UIntPtr TestDummyUnsignedPointerMethod()

{

int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; UIntPtr ptr = (UIntPtr)arr[0];

return ptr;

}

//Warning CS3003 Type of 'Car.age' is not CLS-compliant public sbyte age = 120;

}

}

Section 146.3: Violation of CLS rule: Same naming

using System;

[assembly:CLSCompliant(true)] namespace CLSDoc

{

public class Car

 

 

{

 

 

//Warning

CS3005

Identifier 'Car.CALCULATEAge()' differing only in case is not CLS-

compliant

public int CalculateAge()

{

return 0;

}

public int CALCULATEAge()

{

return 0;

}

}

}

Visual Basic is not case sensitive

GoalKicker.com – C# Notes for Professionals

704

Section 146.4: Violation of CLS rule: Identifier _

using System;

[assembly:CLSCompliant(true)] namespace CLSDoc

{

public class Car

{

//Warning CS3008 Identifier '_age' is not CLS-complian public int _age = 0;

}

}

You can not start variable with _

Section 146.5: Violation of CLS rule: Inherit from non CLSComplaint class

using System;

[assembly:CLSCompliant(true)] namespace CLSDoc

{

[CLSCompliant(false)] public class Animal

{

public int age = 0;

}

 

 

//Warning

CS3009

'Dog': base type 'Animal' is not CLS-compliant

public class Dog : Animal

{

}

}

GoalKicker.com – C# Notes for Professionals

705