A very important feature of. NET is cross-language programming. dll written in C # can be called in VB.net, for example:
A class written in C # is compiled into the DLL and then called in VB.net:
Using system;
Namespace clssample
{
Public class clstest
{
Public clstest ()
{
}
Public void ABC ()
{
Console. writeline ("ABC ");
}
}
}
Call in VB.net:
Dim C as clssample. clstest = new clssample. clstest
C. ABC ()
Add a function to the clstest class in the DLL:
Public void ABC ()
{
Console. writeline ("ABC ");
}
Compile the DLL written by C # first, and then compile the VB.net project. If the compilation fails, the following message is displayed:
The overload decision fails because no accessible "ABC" is most suitable for these parameters:
'Public sub ABC () ': not the most suitable one.
'Public sub ABC () ': not the most suitable one.
The reason is simple, because C # Is case sensitive, but VB.net is not.
The real reason is that the class written in C # does not comply with Cls (public language specifications.
Add a sentence before the namespace to indicate that the content cannot be used by other languages during compilation:
[Assembly: clscompliant (true)]
Namespace clssample
{
......
Public void ABC ()
{
Console. writeline ("ABC ");
}
Public void ABC ()
{
Console. writeline ("ABC ");
}
}
At this time, compile again, and an error will occur, prompting the message:
Only the "clssample. clstest. ABC ()" identifier with different case sensitivity does not match CLS
To be compiled, add the following before function ABC:
[Clscompliantattribute (false)]
It is pointed out that ABC functions do not conform to Cls.
The following section describes CLS in msdn:
CLS is large enough in design, including the language structures that developers often need. It is also small enough,
Most languages support it. In addition, any language that cannot quickly verify code type security
Constructor is excluded from CLS so that all Cls-compliant languages can generate verifiable code.
(If they do ).
That is to say, the developed class library must comply with Cls to be better used by other languages. Otherwise, it is like
In the above situation, the dynamic link library developed with C # has encountered problems in VB.net, especially the development of some
Pay more attention to this point when using common class libraries.
Check the partial decompilation result of int32 in. NET Framework (reflector is used ):
[Clscompliant (false)]
Uint system. iconvertible. touint32 (iformatprovider provider );
This is because some languages do not support the uint32 type.
Let's look at the uint32 decompilation result and uint32 declaration:
[Serializable, structlayout (layoutkind. Sequential), clscompliant (false)]
Public struct uint32: icomparable, iformattable, iconvertible
{
}
What is CLS?
"CLS is an agreement between Programming Language designers and class library designers"