C# 4.0: Improved COM Interop

The question often arises what is so special in C# 4.0 about COM Interop. To me it is a lot. I will try to explain why..

Assume you are trying to get the list of proccess running locally in your machine and creating them using Excel chart and copying that chart to Word.

I have given the working code at the end of this article. Here I want to highlight some features of C# 4.0 which helps us to write better code,

Named Parameter

============

In the code at

chart.ChartWizard(range.CurrentRegion,

        _missingValue,

        _missingValue,

        _missingValue,

        _missingValue,

        _missingValue,

        _missingValue,

        "Memory Usage in " + Environment.MachineName,

        _missingValue,

        _missingValue);

There are some unnecessay code which can be removed as below,

chart.ChartWizard(Source: range.CurrentRegion,

    Title: "Memory Usage in " + Environment.MachineName);

Like that you can get rid of many such codes

Improved COM Interop

===============

Now another cool thing about COM Interop Improvement is, no need to pass ref _missingValue.

So the code

word.Documents.Add(ref _missingValue, ref _missingValue,

    ref _missingValue, ref _missingValue);

can be written as

word.Documents.Add();

Dynamic Language Runtime

===================

Another noticable part is any method’s tool tip shows like,

void ChartWizard(dynamic Source = null, dynamic Gallery = null, dynamic Format = null, dynamic PlotBy = null, dynamic CategoryLabels = null, dynamic SeriesLabels = null, dynamic HasLegend = null, dynamic Title = null, dynamic CategoryTitle = null, dynamic ValueTitle = null, dynamic ExtraTitle = null);

So it fully dynamic.

So the below code would work fine,

using Excel = Microsoft.Office.Interop.Excel;

using Word = Microsoft.Office.Interop.Word;

static object _missingValue = Missing.Value;

static void Main(string[] args)

{

    GenerateChart(copyToWord: true);

}

static void GenerateChart(bool copyToWord = false)

{

    var excel = new Microsoft.Office.Interop.Excel.Application();

    excel.Visible = true;

    excel.Workbooks.Add(_missingValue);

    excel.get_Range("A1").Value2 = "Process Name";

    excel.get_Range("B1").Value2 = "Memory Usage";

    var processes = (from p in Process.GetProcesses()

                    orderby p.WorkingSet64 descending

                    select p).Take(10);

    int i = 2;

    foreach (var p in processes)

    {

        excel.get_Range("A" + i).Value2 = p.ProcessName;

        excel.get_Range("B" + i).Value2 = p.WorkingSet64;

        i += 1;

    }

    Excel.Range range = excel.get_Range("A1");

    Excel.Chart chart = (Excel.Chart)excel.ActiveWorkbook.

        Charts.Add(

            _missingValue,

    excel.ActiveSheet,

            _missingValue,

            _missingValue);

    chart.ChartWizard(range.CurrentRegion,

        _missingValue,

        _missingValue,

        _missingValue,

        _missingValue,

        _missingValue,

        _missingValue,

        "Memory Usage in " + Environment.MachineName,

        _missingValue,

        _missingValue);

    chart.ChartStyle = 45;

    chart.CopyPicture(Excel.XlPictureAppearance.xlScreen,

        Excel.XlCopyPictureFormat.xlBitmap,

        Excel.XlPictureAppearance.xlScreen);

    //Copy the chart to Word

    if (copyToWord)

    {

        var word = new Word.Application();

        word.Visible = true;

        word.Documents.Add(ref _missingValue,

            ref _missingValue,

            ref _missingValue,

            ref _missingValue);

        word.Selection.Paste();

    }

}

Namoskar!!!