.NET late binding

| | No TrackBacks

I am pretty sure I lifted this from a news group posting but I am not sure where.

using System;
using System.Reflection;
using System.Runtime;
using System.Runtime.InteropServices;
namespace Adastra.General
{
  ///
  /// Easy access to late-binding wrapper
  ///
  public class ComCreateObject
  {
    private Type comType;
    private object comObject;
    ///
    /// Generate new class
    ///
    /// com object prog id
    public ComCreateObject(string ProgID)
    {
      comType = Type.GetTypeFromProgID(ProgID);
      if (comType == null)
        throw new ArgumentException(
          "Com ProgId not found",
          "ProgId");
      comObject = Activator.CreateInstance(comType);
    }
    ///
    /// Execute late-bound method
    ///
    /// method name
    /// parameters
    ///
    public object Execute(string method, 
      params object[] parameters)
    {
      return comType.InvokeMember(method, 
        BindingFlags.InvokeMethod, 
        null, 
        comObject, 
        parameters);
    }
    public void ExecuteSet(string method,
      object parameter)
    {
      ExecuteSet(method, new object[] { parameter });
    }
    ///
    /// Execute late-bound property property-set
    ///
    /// property name
    /// value
    public void ExecuteSet(string propertyName,
      params object[] parameters)
    {
      comType.InvokeMember(propertyName, 
        BindingFlags.SetProperty, 
        null, 
        comObject, 
        parameters);
    }
    ///
    /// Execute late-bound property property-get
    ///
    /// property name
    /// parameters
    ///
    public object ExecuteGet(string propertyName,
      params object[] parameters)
    {
      return comType.InvokeMember(propertyName, 
        BindingFlags.GetProperty, 
        null, 
        comObject, 
        parameters);
    }
    ///
    /// Execute late-bound property property-get
    ///
    /// property name
    ///
    public object ExecuteGet(string propertyName)
    {
      return comType.InvokeMember(propertyName, 
        BindingFlags.GetProperty, 
        null, 
        comObject, 
        null);
    }
    public void Dispose()
    {
      Marshal.ReleaseComObject(comObject);
      comObject = null;
    }
  }
}

// Get type of the COM object xxxx
Type objectType = Type.GetTypeFromProgID("xxxx");

// Create instance of the COM object
object comObject = Activator.CreateInstance( objectType);

// Get the Count property
int count = (int) objectType.InvokeMember(
    "Count",
    BindingFlags.GetProperty,
    null,
    comObject,
    null);

No TrackBacks

TrackBack URL: http://www.iwebthereforeiam.com/cgi-bin/mt/mt-tb.cgi/902

Leave a comment

Verification (needed to reduce spam):

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.32-en

About this Entry

This page contains a single entry by Hugh Brown published on January 25, 2005 8:10 AM.

Windows keyboard handling was the previous entry in this blog.

Weird WikiPedia articles is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.