private static T MapToDto <T>(this ManagementObject managementObject) where T : class { var type = typeof(T); var dto = type.CreateInstance() as T; var wmiProperties = managementObject.ToPropertyDataArray(); foreach (var dtoProperty in type.Properties()) { var found = wmiProperties.Where(x => x.Name == dtoProperty.Name).FirstOrDefault(); // check if the property is an array (both sides) if (found.IsArray != dtoProperty.PropertyType.IsArray) { throw new InvalidCastException("Source and destination must be same"); } else if (found.IsArray && dtoProperty.PropertyType.IsArray) { var dtoArrayElementType = dtoProperty.PropertyType.GetElementType(); var foundArray = (Array)found.Value; dto.SetPropertyValue( dtoProperty.Name, CloneAndConvertArray(foundArray, dtoArrayElementType)); } else { dto.SetPropertyValue( dtoProperty.Name, found.Value.ConvertWmiValue(dtoProperty.PropertyType)); } } return(dto); }