Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Cross Language Interoperability

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.23k
    Comment on it

    .Net Framework contains a large library and support for cross language interoperability (i.e., the ability through which each and every language can use code written in other languages) across several programming languages. It also support the feature of code reusability, because of cross language interoperability code written in any of .net compliant language can be used by other .net compliant language. e.g., code written in C#.net can be used with the modules developed in vb.net,f#.net etc.

    When we write a program in C#.net and compile that C# program it will generate IL (Intermediate Language). Now, this code generated on compilation can be used by any other language that is supported by .Net Framework. The target language does not care about in which language your code was initially written ,all it want is your code should contain a class supported by .Net Framework.

    Now, Let us discuss a practical example of Cross Language Interoperability. We will a create a class Library(.dll) in c# that will be used in a vb.net code(.exe).

    1. Create a Class Library Project in c#.

      Create a class and declare it to be public.

      Add code that can be used in other modules like functions to calculate area, sum or something else.

      Compile the code written in c# to generate a IL (Intermediate Language) code ie., It will create a .dll in bin or debug folder.

    Code Snippet for class1.cs is as follows:

    Using System;

    namespace ClassLibrary

    {

      Public class class1
          {
                Public  int Sum (int l, int b)
                {return l*b;}
                Public  int sum (int l, int b)
               {return l*b;}
    
           }
    

    }

    In the above code, We have written code in C# i.e., a case sensitive Language. So,we have declared two functions for calculating sum, where name of functions is same but that differ in cases of letters in name of functions. So, being a case sensitive language C# treat both as different functions. But Vb.net is a case in- sensitive language so it will treat both as same functions. To avoid any hassle in consuming this .dll we can rename the functions and give them unique name so that it can work fine with case sensitive as well as case in- sensitive languages. Also we need to make this Library CLS complaint for this on the top of namespace ClassLibrary, add the statement [assembly:CLSCompliant(true)]. Now, if something is not CLS compliant in your code that will get highlighted and you can fix that issue.

    Using System;

    [assembly:CLSCompliant(true)]

    namespace ClassLibrary

    {

      Public class class1
          {
               Public  int Sum(int l, int b)
               {return l*b;}
    
              Public  int sum1(int l, int b)
               {return l*b;}
    
         }
    

    }

    Now, let's consume this C# .dll in a VB .NET application.

    1. Create a VB .NET Console Application.

    2. Add Reference of ClassLibrary.dll to the application.

    3. Imports ClassLibrary.Class1 on the top of vb.net code file

    4. Create object of class library(.dll) to access functions of the class Library. strong text

    Code Snippet for Module1.vb class of vb.net console Application is as follows:

    Imports ClassLibrary.Class1

    Module Module1

    Dim obj As New ClassLibrary.Class1

         Sub Main()
             Obj.sum(1,2)
             Obj.sum1(3,4)
         End Sub
    

    End Module

    VB .NET got access to C# functions after making C# assembly. CLS Complaint.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: