Created
July 23, 2012 05:42
-
-
Save sandrinodimattia/3162117 to your computer and use it in GitHub Desktop.
DynamicComMapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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