CreateObject equivalent for C#

In current project, I had to use some 3rd party APIs, which is exposed via COM Interop. I found some VB.Net code to consume, but I couldn’t find in C# implementation for the same. Here is the code snippet which is equivalent VB.Net CreateObject method.

var txt = "HelloWorld";
var type = Type.GetTypeFromProgID("vbscript.regexp");
dynamic vbScriptRegEx = Activator.CreateInstance(type);
vbScriptRegEx.Pattern = "l";
Console.WriteLine(vbScriptRegEx.Replace(txt, "##"));

You may get some other errors, if your platform / platform target is different from the COM object compiled platform.

OpenXML and opening a file in Read only mode

In one of my project, I am using OpenXML SDK for opening Excel files. Recently I got an issue like SpreadsheetDocument.Open() method was throwing an IOException, if the Excel file is opened by MS Excel, even if I set the isEditable parameter false.

IO Exception - Open XML opening file in Read Only mode

IO Exception – Open XML opening file in Read Only mode

I fixed this problem by passing a stream instead of string (file path). And I used the File stream class to open the Excel file. Here is the code snippet.

using (var fileStream = new FileStream(fileName, FileMode.Open, 
    FileAccess.Read, FileShare.ReadWrite))
{
    using (var spreadSheetDocument = SpreadsheetDocument.Open(fileStream, false))
    {
        //Implementation
    }
}

Happy Coding :)

Generate HTML file from XML using XSLT

Recently I had to work on FxCop, for static code analysis. For those who don’t know what is FxCop, check this link. But one issue I faced was, the report can be saved / exported from FxCop is only in XML format. I searched for tool which help me to convert this XML to any other formats like HTML or doc, but I couldn’t find anything. :( Then I found FxCop comes with some XSL file, which help you to convert the FxCop generated XML to HTML. You can find these XSL files under – “C:\Program Files (x86)\Microsoft Fxcop 10.0\Xml” folder. And here is the code which helps to generate HTML using XML and XSLT.

public static string TransformXMLToHTML(string xmlFile, string xsltFile)
{
    var transform = new XslCompiledTransform();
    using (var reader = XmlReader.Create(File.OpenRead(xsltFile)))
    {
        transform.Load(reader);
    }

    var results = new StringWriter();
    using (var reader = XmlReader.Create(File.OpenRead(xmlFile)))
    {
        transform.Transform(reader, null, results);
    }

    return results.ToString();
}

Happy Programming :)

How to pass parameters to Work Flow

You can create arguments to workflow and can pass the values from outside. For creating arguments, you can use the Arguments Tab in the WorkFlow designer.

WF Arguments Tab

WF Arguments Tab

In the last post I mentioned about loading and invoking a WorkFlow, the WorkFlowInvoker class’s Invoke method has an overload, which accepts inputs parameter, which is type of IDictionary<string, object>. You can create an Dictionary<string, object> and pass it as the parameter to the Invoke method.

You can get the results from the WorkFlow in similar fashion, it is also returning IDictionary<string, object>. Here is the code snippet, which provides one input parameter and accepts the results.

var inputParameters = new Dictionary<string, object>();
inputParameters.Add("CurrentDate", DateTime.Now.AddDays(-20));
var result = WorkflowInvoker.Invoke(activity, inputParameters);
object timeAgo = string.Empty;
if (result.TryGetValue("Result", out timeAgo))
{
    Console.WriteLine(timeAgo);
}

You can use any type as the argument type.

Happy Coding :)