COM Interoperability in .NET Part 2

The managed code

To illustrate COM type communication with managed code, Let us see an example
in which I create a C# class library which has a class named Calculatorwhich supports three methods named Add(),Subtract()and Hello(). Notice that we define another interface named Imuldiv.

namespace Simpleclasslib
{
  using System;
  using System.Runtime.InteropServices;

  public interface Imuldiv
  {
    int Multiply(int x,int y);
    int Division(int x,int y);
  }
  
  public class Calculator:Imuldiv
  {
    public Calculator(){}
    public int Add(int x,int y) {
      return x+y;
    }
    public int Subtract(int x,int y)
    {
      return x-y;
    }
    int Imuldiv.Multiply(int x,int y)
    {
      return x*y;
    }
    int Imuldiv.Division(int x,int y)
    {
      return x/y;
    }
    public string Hello(string strName)
    {
      string str ;
      str = "Hello " + strName ;
      return str ;
    }
  }
}

Once the managed code is written, the compilation process is the same as it would be for any other piece of managed code.

csc /out:Server.dll /target:library Calculator.cs

Now let us see the Server.dll in the ILDasm.exe.In that you can see the calculator class members and Imuldiv Interface members.

You might also like...

Comments

About the author

G.Gnana Arun Ganesh

G.Gnana Arun Ganesh India

G.Gnana Arun Ganesh is the Administrator and the Founder of ARUN MICRO SYSTEMS (www.arunmicrosystems.netfirms.com). He has been programming in C++, Visual Basic, COM, Java and Microsoft Technolo...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“You can stand on the shoulders of giants OR a big enough pile of dwarfs, works either way.”