# CLS ルールが適用されるアクセス修飾子
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;
}
}
}
CLS 準拠の規則は、公開/保護されたコンポーネントにのみ適用されます。
# CLS 規則違反:符号なし型 / 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;
//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;
}
}
# CLS ルール違反:同じ命名
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 では大文字と小文字が区別されません
# CLS ルールの違反:識別子 _
using System;
[assembly:CLSCompliant(true)]
namespace CLSDoc
{
public class Car
{
//Warning CS3008 Identifier '_age' is not CLS-complian
public int _age = 0;
}
}
_で変数を開始することはできません
# CLS ルール違反:非 CLSComplaint クラスから継承
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
{
}
}
# 構文
<オール># パラメータ
コンストラクタ | パラメータ |
---|---|
CLSCompliantAttribute(ブール値) | 指定されたプログラム要素が CLS に準拠しているかどうかを示すブール値を使用して、CLSCompliantAttribute クラスのインスタンスを初期化します。 |
# コメント
共通言語仕様 (CLS) は、他の CLS 準拠言語と相互運用するために、CLI を対象とする言語 (共通言語インフラストラクチャ仕様を確認する言語) が確認する必要がある基本ルールのセットです。
CLI 言語のリスト
ライブラリを配布するときは、ほとんどの場合、アセンブリを CLSCompliant としてマークする必要があります。この属性により、コードがすべての CLS 準拠言語で使用できることが保証されます。これは、CLR (共通言語ランタイム) でコンパイルおよび実行できる任意の言語でコードを使用できることを意味します
アセンブリが CLSCompliantAttribute
でマークされている場合 、コンパイラはコードがCLSルールに違反しているかどうかをチェックし、警告を返します