Skip to content

Instantly share code, notes, and snippets.

@sandrinodimattia
Created July 23, 2012 05:42
Show Gist options
  • Save sandrinodimattia/3162117 to your computer and use it in GitHub Desktop.
Save sandrinodimattia/3162117 to your computer and use it in GitHub Desktop.
DynamicComMapper
public class DynamicComMapper
{
/// <summary>
/// Map all the properties of the object to the properties of the COM object.
/// </summary>
/// <param name="comObj"></param>
protected void Map(dynamic comObj)
{
foreach (PropertyInfo property in this.GetType().GetProperties())
{
try
{
// Retrieve the value of a property of the COM object.
object comPropertyValue = typeof(Object).InvokeMember(property.Name, BindingFlags.GetProperty, null, comObj, null);
// Set value in our managed object.
property.SetValue(this, comPropertyValue, null);
}
catch (COMException)
{
// You could skip errors for unknown properties.
// if (ex.ErrorCode != -2147352570)
// throw ex;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment