开发者

What is relationship between CTS and CLS?

I just cant seem to understand these pil开发者_Go百科lars of .NET.


CTS (Common Type System) So you can say CTS describes how types are declared, used and managed in the runtime and facilitates cross-language integration, type safety, and high performance code execution.

CLS (Common Language Specification) The Common Language Specification (CLS) is an agreement among language designers and class library designers to use a common subset of basic language features that all languages have to follow.

As you can see in the image CLS is a subset of CTS

alt text http://www.microsoft.com/taiwan/msdn/columns/DoNet/images/NET_CTS1.gif


Common Type System

CTS is a formal specification describing type properties:

  • How types should be laid out in IL.
  • Each type can consist of 0 or more following members: property, field, method and event.
  • Access modifiers (and C# equivalents): private (private), family (protected), family and assembly (N/A), assembly (no modifier/internal), family or assembly (protected internal), public (public)
  • Type inheritance, virtual methods
  • Each type must inherit from System.Object
  • Each language implements only a subset of CTS features.

Common Language Specification

CLS facilitates interoperability between .NET languages at the IL level:

  • It is a compiler specification that determines what IL the compilers must emit in order for the code to be interoperable among .NET languages.
  • CLS describes a common set of functionalities that the program is allowed to use and still be interoperable with programs written in other .NET languages.
  • CLS features are a subset of CTS features.
  • CLS is of importance only if you wish to write your programs in multiple languages.
  • An important thing to note is that CLS must be adhered to only by the pieces of code marked as public, since these are the only ones that are available to other programs.
  • If we annotate the code with [assembly: CLSCompliant(true)] attribute, then the compiler will check if the code is CLS compliant.

For example:

  • CLS states that member names cannot be differentiated by case: thus Foo() and foo() are the same thing.
  • unsigned int cannot be used as it is not implemented by all .NET languages


As already mentioned, CLS is subset of CTS. But there is much more in the actual definition of these terms.

I suggest reading CLR via C#.


Some examples would certainly help here.

One of the key things that is not CLS-compliant is unsigned numbers (e.g., uint). Another is function pointers (delegates). If it's not going to make sense on both ends of the line, then it's not "common"; CLS defines a set of common types that work not only within the CLR, but also within certain common and well-specificed interop scenarios.

If you want to enforce CLS compliance, you can add the "CLSCompliant" attribute to an assembly by adding it to the assemblyinfo.cs file in a project.

[assembly: CLSCompliant(true)]

You can also add it as an attribute on a class.

[CLSCompliant(true)]
public class HospitalLocationEntity : EntityBase
{
    ...
}

Doing these things will cause the C# compiler (or VB, with appropriate VB syntax on the attributes) to raise compile errors for CLS compliance violations.

Also, adding the [ScriptService] and [ScriptMethod] attributes to web services (.asmx) will cause the service to generate JSON service output, and will require that the data used for service responses be marked as CLSCompliant at class and assembly levels.

<System.Web.Services.WebService()> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
<ScriptService()> _
Public Class HospitalLocationService
    Inherits System.Web.Services.WebService
    <WebMethod()> _
    <ScriptMethod()> _
    Public Function GetAll() As List(Of HospitalLocationEntity)
        Return (New HospitalLocation()).GetAll().Data
    End Function
End Class


CLS

CLS stands for Common Language Specification and it is a subset of CTS. It defines a set of rules and restrictions that every language must follow which runs under the .NET framework. The languages which follow these set of rules are said to be CLS Compliant. In simple words, CLS enables cross-language integration or Interoperability.

For Example

if we talk about C# and VB.NET then, in C# every statement must have to end with a semicolon. it is also called a statement Terminator, but in VB.NET each statement should not end with a semicolon(;). Explanation of the above Example

So these syntax rules which you have to follow from language to language differ but CLR can understand all the language Syntax because in .NET each language is converted into MSIL code after compilation and the MSIL code is language specification of CLR.

CTS

Common Type System (CTS) describes the datatypes that can be used by managed code. CTS defines how these types are declared, used and managed in the runtime. It facilitates cross-language integration, type safety, and high-performance code execution. The rules defined in CTS can be used to define your own classes and values.

OR we can also understand like,

CTS deals with the data type. So here we have several languages and each and every language has its own data type and one language data type cannot be understandable by other languages but .NET Framework language can understand all the data types.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜