For a general purpose about the dynamic type, you can start with this topic.
Here's a trivial example using the AutoCAD COM API in a standalone application (the only context which requires COM using).
Referencing AutoCAD 19 interop libraries, this application will only work with AutoCAD 3013 and 3014 (the main interest is Visual Studio intellisense works).
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace EarlyBindingStandaloneSample
{
class Program
{
static void Main(string[] args)
{
AcadApplication acadApp;
// check if a AutoCAD session exists
try
{
acadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.19");
}
catch
{
// if there is no AutoCAD session active then there will be one created
try
{
acadApp = (AcadApplication)Activator.CreateInstance(
Type.GetTypeFromProgID("AutoCAD.Application.19"), true);
}
catch
{
MessageBox.Show("Unable to start AutoCAD 19.");
return;
}
}
// if successful set visibility status
while (true)
{
try { acadApp.Visible = true; break; }
catch { }
}
acadApp.WindowState = AcWindowState.acMax;
// get AutoCAD active document
AcadDocument acadDoc = acadApp.ActiveDocument;
// add a circle to de active document model space
AcadCircle circle = acadDoc.ModelSpace.AddCircle(new[] { 20.0, 10.0, 0.0 }, 10.0);
circle.color = ACAD_COLOR.acRed;
acadApp.ZoomWindow(new[] { 5.0, -5.0, 0.0 }, new[] { 35.0, 25.0, 0.0 });
}
}
}
The same app using late binding with the 'dynamic' type, this application should work with every AutoCAD version. Typically, just remove references to AutoCAD interop from the upper code and repair the errors due to AutoCAD COM types missing by using the dynamic type and the enumeration integer equivalences.
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace LateBindingStandaloneSample
{
class Program
{
static void Main(string[] args)
{
dynamic acadApp;
// check if a AutoCAD session exists
try
{
acadApp = Marshal.GetActiveObject("AutoCAD.Application");
}
catch
{
// if there is no AutoCAD session active then there will be one created
try
{
acadApp = Activator.CreateInstance(
Type.GetTypeFromProgID("AutoCAD.Application"), true);
}
catch
{
MessageBox.Show("Unable to start AutoCAD.");
return;
}
}
// if successful set visibility status
while (true)
{
try { acadApp.Visible = true; break; }
catch { }
}
acadApp.WindowState = 3;
// get AutoCAD active document
var acadDoc = acadApp.ActiveDocument;
// add a circle to de active document model space
var circle = acadDoc.ModelSpace.AddCircle(new[] { 20.0, 10.0, 0.0 }, 10.0);
circle.color = 1;
acadApp.ZoomWindow(new[] { 5.0, -5.0, 0.0 }, new[] { 35.0, 25.0, 0.0 });
}
}
}