One of the most important features of. NET is cross-language programming, and DLLs written in C # can be called in vb.net, for example:
A class written in C #, compiled into a DLL, and then called in vb.net:
Using System;
Namespace Clssample
{
public class Clstest
{
Public Clstest ()
{
}
public void ABC ()
{
Console.WriteLine ("ABC");
}
}
}
Called in vb.net:
Dim C as Clssample.clstest = New clssample.clstest
C.ABC ()
Now add a function to the Clstest class in the DLL:
public void ABC ()
{
Console.WriteLine ("ABC");
}
First compile C # write this DLL, and then compile vb.net project, compile problems, the following information:
Overload resolution failed because there is no accessible "ABC" that is best suited to these parameters:
' Public Sub ABC () ': not most suitable.
' Public Sub ABC () ': not most suitable.
The reason is simple because C # is case-sensitive, but vb.net is not differentiated.
The real reason for this is that the class written in C # is not CLS-compliant (Common language specification).
Now add a sentence in front of the namespace to make sure that you don't include content that is not available in other languages at compile time:
[Assembly:clscompliant (True)]
Namespace Clssample
{
......
public void ABC ()
{
Console.WriteLine ("ABC");
}
public void ABC ()
{
Console.WriteLine ("ABC");
}
}
At this time to compile, there will be errors, prompting information:
Identifiers that differ only in case "CLSSAMPLE.CLSTEST.ABC ()" are not CLS-compliant
To compile the pass, precede the function ABC with:
[CLSCompliantAttribute (False)]
Indicates that the ABC function is not CLS-compliant
The following is a partial description of the CLS in MSDN:
The CLS is designed to be large enough to include language constructs that are often needed by developers, but also small enough to
Most languages can support it. In addition, any language that is not likely to quickly verify code type security
Constructs are excluded from the CLS so that all CLS-compliant languages can generate verifiable code
(if they choose to do so).
This means that the developed class library must comply with the CLS in order to be better used by other languages. Otherwise, it's like
In the above case, the dynamic link library developed in C # has problems in vb.net, especially to develop some
Common class libraries, it is even more important to note this.
Look at some of the results of Int32 decompile in the. NET Framework (with reflector):
[CLSCompliant (False)]
UINT System.IConvertible.ToUInt32 (IFormatProvider provider);
This is because some languages do not support UInt32 types.
Then look at the results of the UInt32 decompile, UInt32 's declaration:
[Serializable, StructLayout (layoutkind.sequential), CLSCompliant (false)]
public struct uint32:icomparable, IFormattable, iconvertible
{
}
What is a CLS?
"CLS is a contract between a programming language designer and a class library designer"
CLSCompliant (cross-language invocation) of the CLS (Common Language Specification)