"Reguli CLS" presentation @ RONUA Bucharest

Post on 22-Jan-2018

80 views 1 download

transcript

Reguli CLS

Adrian Floreahttp://blogs.ugidotnet.org/adrian/

La inceput: Project 7 Un proiect de cercetare, rod al colaborarii

intre MS Research si cateva universitatzi din afara SUA, in vederea implementarii de compilatoare pentru CLR pentru diferite limbaje de programare folosite cu precadere in mediul academic si explorarii posibilitatzilor de interoperabilitate intre aceste limbaje. Experientza castigata de echipele participante in acest proiect a putut da un valoros feedback arhitectilor .NET si a dus la cresterea popularitatzii limbajelor academice.

Regula 1 “CLS rules apply only to those parts of a

type that are accessible or visible outside of the defining assembly”.

Public

FamilyFamily-Or-Assembly

Private

AssemblyFamily-And-Assembly

Compiler-ControlledCLS

Regula 2 “Members of non-CLS compliant types shall

not be marked CLS-compliant”. In Visual C#: error CS3003: Type of ‘...' is not

CLS-compliant In Visual Basic .NET compileaza fara erori In Visual J#: warning VJS1466: The attribute

System.CLSCompliantAttribute is being attached but the compiler will not verify that the assembly is CLS compliant

Tipuri neconforme cu CLS System.SByte System.UIntPtr System.TypedReference (Regula 14) System.UInt16 System.UInt32 System.UInt64

Tipuri neconforme cu CLS “We include Byte rather than SByte in

the CLS because it is much more common to view bytes as unsigned 0..255 values than –128..127 values”.

Brad Abrams, SLAR p. 78

Regula 4 “Assemblies shall follow Annex 7 of Technical

Report 15 of the Unicode Standard 3.0. Identifiers shall be in the canonical format defined by Unicode Normalization Form C. For CLS purposes, two identifiers are the same if their lowercase mappings are the same. However, in order to override an inherited definition the CLI requires the precise encoding of the original declaration be used”.

Regula 4 In Visual C#: error CS3008: Identifier

'...' is not CLS-compliant error CS3005: Identifier ‘...' differing

only in case is not CLS-compliant In Visual Basic .NET: error BC30260:

'...' is already declared as '...' in this class

In Visual J# compileaza fara erori

Regula 6 “Fields and nested types shall be

distinct by identifier comparison alone, even though the CTS allows distinct signatures to be distinguished. Methods, properties, and events that have the same name (by identifier comparison) shall differ by more than just the return type, except as specified in CLS Rule 39”.

Regula 6 In Visual C#: error CS0102: The class

‘...' already contains a definition for '...‘ In Visual Basic .NET: error BC31073:

'...' conflicts with a function by the same name declared in '...'

Regula 10 “Accessibility shall not be changed

when overriding inherited methods, except when overriding a method inherited from a different assembly with accessibility Family-or-Assembly. In this case the override shall have accessibility family.”

Regula 10Assembly 1 Assembly 2

Public Public

Assembly 1 Assembly 2

Family Family

Assembly 1 Assembly 2

Family-Or-Assembly Family

Regula 11 “All types appearing in a signature shall

be CLS-compliant”.

Conforme al CLS

Type

Member

Signature

1

Regula 12 “The visibility and accessibility of types

and members shall be such that types in the signature of any member shall be visible and accessible whenever the member itself is visible and accessible. For example, a public method that is visible outside its assembly shall not have an argument whose type is visible only within the assembly”.

Regula 18 “CLS-compliant interfaces shall not require

the definition of non-CLS compliant methods in order to implement them”.

In Visual C# obtinem: error CS3002: Return type of ‘...' is not CLS-

compliant error CS3001: Argument type ‘...' is not CLS-

compliant In Visual Basic .NET nu obtinem eroare

Regula 19 “CLS-compliant interfaces shall not

define static methods, nor shall they define fields”.

In Visual C# obtinem: error CS0106: The modifier 'static' is not

valid for this item error CS0525: Interfaces cannot contain

fields

Regula 19 In Visual Basic .NET obtinem: error BC30270: 'Shared' is not valid on

an interface method declaration. error BC30602: Interface members

must be methods, properties, events, or type definitions.

Regula 20 “CLS-compliant classes, value types, and

interfaces shall not require the implementation of non-CLS-compliant interfaces”.

In Visual C#: error CS3003: Type of ‘...' is not CLS-compliant

Implementarea unei interfete marcator (goale) neconforme cu CLS nu da insa eroare

Regula 21 “An object constructor shall call some

class constructor of its base class before any access occurs to inherited instance data. This does not apply to value types, which need not have constructors”.

Regula 21

Regula 23 “System.Object is CLS-compliant. Any

other CLS-compliant class shall inherit from a CLS compliant class”.

In Visual C#: error CS3009: ‘...': base type ‘...' is not CLS-compliant

Regula 24 “The methods that implement the getter

and setter methods of a property shall be marked SpecialName in the metadata”.

Regula 25 “The accessibility of a property and of

its accessors shall be identical”. In Visual Basic .NET si Visual C#,

regula e impusa de sintaxa In Visual J#: error VJS1805: Both

accessor methods of a property or event must have the same modifiers

Regula 26 “A property and its accessors shall all

be static, all be virtual, or all be instance”.

In Visual Basic .NET si Visual C#, regula e impusa de sintaxa

In Visual J#, aceeasi eroare ca la regula 25 (VJS1805)

Regula 27 “The type of a property shall be the return

type of the getter and the type of the last argument of the setter. The types of the parameters of the property shall be the types of the parameters to the getter and the types of all but the final parameter of the setter. All of these types shall be CLScompliant, and shall not be managed pointers (i.e. shall not be passed by reference)”.

Regula 27 Incercand sa incalcam aceasta regula in

Visual Basic .NET, se obtin erorile: BC31064: 'Set' parameter must have the

same type as the containing property. BC30651: Property parameters cannot be

declared 'ByRef‘. BC31065: 'Set' parameter cannot be declared

'ByRef‘. BC31063: 'Set' method cannot have more

than one parameter.

Regula 28 “Properties shall adhere to a specific

naming pattern. The SpecialName attribute referred to in CLS rule 24 shall be ignored in appropriate name comparisons and shall adhere to identifier rules”.

Visual J# nu dispune de o sintaxa pentru accesarea proprietatilor

Regula 28

Regula 28

Regula 29 "The methods that implement an event shall

be marked SpecialName in the metadata"

Regula 29

Regula 30 "The accessibility of an event and of its

accessors shall be identical" In Visual Basic .NET nu exista event

accessors In Visual C# regula e impusa de sintaxa In Visual J#: error VJS1805: Both

accessor methods of a property or event must have the same modifiers

Regula 30

Regula 31 “The add and remove methods for an event

shall both either be present or absent” In Visual C#: error CS0065: event property

must have both add and remove accessors In Visual Basic .NET amandoua metodele

add si remove sunt absente In Visual J#: error VJS1806: The event '...'

must have both add and remove accessors

Regula 32 “The add and remove methods for an event

shall each take one parameter whose type defines the type of the event and that shall be derived from System.Delegate”.

In Visual J#: error VJS1810: Event accessor must be of type 'void add_name(type)' or 'void remove_name(type)' where 'type' derives from System.Delegate

Regula 33 “Events shall adhere to a specific

naming pattern. The SpecialName attribute referred to in CLS rule 31 shall be ignored in appropriate name comparisons and shall adhere to identifier rules”.

Regula 33

Regula 36 “Global static fields and methods are

not CLS-compliant”.

Regula 37 “Only properties and methods may be

overloaded”. Deci nu si campurile sau evenimentele In Visual C#: error CS0102: The class

‘...' already contains a definition for ‘...‘ In Visual Basic .NET: error BC30260:

'...' is already declared as '...' in this class

Regula 38 “Properties, instance methods, and

virtual methods may be overloaded based only on the number and types of their parameters, except the conversion operators named op_Implicit and op_Explicit which may also be overloaded based on their return type”.

Regula 38 Overloading CTS Overloading CLS Overloading by demangling (la nivel

compilator, vezi Eiffel#)

Regula 39 “If either op_Implicit or op_Explicit is

provided, an alternate means of providing the coercion shall be provided”.

ToXxx FromXxx

Regula 40 “Objects that are thrown shall be of type

System.Exception or inherit from it. Nonetheless, CLS compliant methods are not required to block the propagation of other types of exceptions”.

Regula 40 Programatorii Visual J# au la dispozitie

doua ierarhii de exceptii: exceptii Java (care deriva din java.lang.Throwable) si exceptii .NET (care deriva din System.Exception). Cele doua ierarhii sunt mapate partial de catre metoda java.lang.Throwable.__mapCorException iar java.lang.Throwable deriva din System.Exception

Regula 40

Regula 40 In Visual C#: error CS0155: The type caught

or thrown must be derived from System.Exception

In Visual Basic .NET: error BC30665: 'Throw' operand must derive from 'System.Exception‘

In Visual J#: error VJS1362: Cannot throw ‘...' that does not inherit from java.lang.Throwable or System.Exception

Regula 40 In Lagoona .NET, exceptiile deriva din

System.Object si nu din System.Exception

In Nemerle si S# nu exista restrictii

Regula 41 “Attributes shall be of type System.Attribute,

or inherit from it”. In Visual C#: error CS0616: ‘...': is not an

attribute class In Visual Basic .NET: error BC31504: ‘...'

cannot be used as an attribute because it does not inherit from 'System.Attribute‘

In Visual J#: error VJS1397: Attribute ‘...' doesn't inherit from 'System.Attribute'