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.
Comments