Language Interoperability
Introduction
The common language runtime provides built insupport for language interoperability. The common language runtimeprovides the necessary foundation for language interoperability byspecifying and enforcing a common type system and by providingmetadata. Metadata enables language interoperability by defining auniform mechanism for storing and retrieving information about types.Every type and member defined, attributes defined and referenced in amodule or assembly is described within metadata. Compilers store typeinformation as metadata, and the common language runtime uses thisinformation to provide services during execution. The runtime canmanage the execution of multilanguage applications because all typeinformation is stored and retrieved in the same way, regardless of thelanguage the code was written in. To ensure that your managed code isaccessible to developers using any programming language, the .NETFramework provides the Common Language Specification(CLS). The CLSdescribes a fundamental set of language features and defines rules forhow those features are to be used.
DLL in C#
A dynamic-link library (DLL) is an executablefile that acts as a shared library of functions. C# also includesnative support for component object model and windows basedapplications. One need not have to implement the interface unknown andits member functions queryinterface, addref or release. The simpleclass program can be compiler to create a library. Once the library isgenerated the same can be referred in any other program using compileroptions.
//program #: 1
//Compile using csc /t:library /out:industry.dll industry.cs
using System;
namespace industrial
{
public class greaternoida
{
public string display()
{
return ("Provides plot for industrial development.");
}
}
}
This generated the industry.dll. The /t optionspecifies that the output is a dll. The /out option indicates that theoutput will be named as mentioned.
//program #: 2
//Compile: csc /r:industry.dll program2.cs
using System;
using industrial;
class noida:greaternoida
{
public new string display()
{
return ("Noida also provides plots for industrial and residential purposes.");
}
}
class Test
{
public static void Main()
{
greaternoida gnoida=new greaternoida();
noida nda=new noida();
Console.WriteLine(gnoida.display());
Console.WriteLine(nda.display());
}
}
output:
Provides plot for industrial development.
Noida also provides plots for industrial and residential purposes.
This program uses the class greaternoida classas developed in the previous program. Then displays the output of thedisplay function. Another class noida is derived from greaternoidaclass and it also uses a new display function to display it?sinformation.
Managed C++ Class
Managed Extensions for C++ are a set oflanguage extensions to C++. Managed Extensions allow you to intermixtraditional unmanaged and managed C++ code within the same application.With Managed Extensions one can directly create, and call, a .NETFramework class from C++ code.
The Managed Extensions provide built-insupport for metadata, and enable declaration of new types andcategories of metadata. The limitations of type libraries andrestrictive automation types are removed with managed classes.
The general programming in Manages extensionsfor C++ has been supplemented with new codes to take care of garbagecollected heap and to include the .Net Framework platform. The keyword__gc on a class or struct indicates that it is garbage-collected, andits lifetime is managed by the common language runtime. The #usingdeclaration is used to import the metadata contained in the specifiedfile into the program. In Managed Extensions for C++ program, the filemscorlib.dll must be imported via the #using directive. As mentionedpreviously, the namespace statement is used to create a new namespaceto encapsulate the classes that will be created. The cl compiler isused to compile the managed code. The /clr compiler option enables theuse of Managed Extensions for C++ and creates an output file thatrequires the Common Language Runtime (CLR). The /LD option passes the/DLL option to the linker and creates a dll.The compiler generatesMicrosoft Intermediate Language (MSIL) code which is a CPU-independentset of instructions that can be efficiently converted to native code.The Just in time compiler(JIT) converts the MSIL to CPU specific codejust before the execution of the code.
//program#: 3
//compile: cl /CLR /LD first1.cpp
#using
using namespace System;
namespace gan {
__gc public class testcpp
{
public:
int display()
{
Console::WriteLine("Hello, World! from display function of managed C++");
return 0;
}
};
};
This produces a dll with name first1.dll. Thisdll contains the necessary assembly for using in any CLR complaintlanguage. The next program demonstrates how to use this dll in C#programme. C++ Class inside C#
//program # :4
//compile: csc /r:first1.dll program4.cs
using System;
class Test{
public static void Main()
{
gan.testcpp d1;
d1=new gan.testcpp();
d1.display();
}
}
output:
Hello, World! from display function of managed C++
This example demonstrates the use of a C++class inside a C# programme. The testcpp class belongs to thefirst1.cpp programme. An instance of the class testcpp is cretaed inthe C# is d1. The display function is invoked to display the message.
//prgramme #: 5
//compile:cl /clr /LD program5.cpp
#using
using namespace System;
namespace industrial {
__gc public class greaternoida {
public:
int virtual display()
{
Console::WriteLine("Provides plot for industrial and residential purpose");
return 0;}
};
};
This program creates an dll with name program5.dll
//programm #: 6
//compile: csc /r:program5.dll program6.cs
using System;
using industrial;
class noida:greaternoida
{
public override int display()
{
Console.WriteLine("Noida also provides plot for industrial and residential development");
return 0;
}
}
class society:greaternoida{
public override int display()
{
Console.WriteLine("Society only provides plot for residential development");
return 0;
}
}
class Test{<
br />
/>public static void Main()
{
greaternoida gnoida;
gnoida=new greaternoida();
gnoida.display();
noida nda=new noida();
nda.display();
society sct=new society();
sct.display();
}
}
output:
Provides plot for industrial and residential purpose
Noida also provides plot for industrial and residential development
Society only provides plot for residential development
This program has demonstrated how the virtualfunction display was overriden using the same display function in theinherited class.
Conclusion
This language interoperability is one of the highest features of the common language run time (CLR).












No comments yet... Be the first to leave a reply!