ConfigurationExtensions.cs (1499B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="ConfigurationExtensions.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.Management.Configuration.UnitTests.Helpers 8 { 9 using System.Linq; 10 using System.Reflection; 11 12 /// <summary> 13 /// Contains extension methods for configuration objects. 14 /// </summary> 15 internal static class ConfigurationExtensions 16 { 17 /// <summary> 18 /// Assigns the given properties to the configuration unit. 19 /// </summary> 20 /// <param name="unit">The unit to assign the properties of.</param> 21 /// <param name="properties">The properties to assign.</param> 22 /// <returns>The given ConfigurationUnit.</returns> 23 internal static ConfigurationUnit Assign(this ConfigurationUnit unit, object properties) 24 { 25 PropertyInfo[] unitProperties = typeof(ConfigurationUnit).GetProperties(); 26 27 foreach (PropertyInfo property in properties.GetType().GetProperties()) 28 { 29 PropertyInfo matchingProperty = unitProperties.First(pi => pi.Name == property.Name); 30 matchingProperty.SetValue(unit, property.GetValue(properties)); 31 } 32 33 return unit; 34 } 35 } 36 }