我无法找到完全符合我要求的答案。所以我写了一个小方法来做到这一点。它将同时占用两个对象,并合并它们的字段/属性,假设一个null
值表示一个未分配的字段/属性。
这是使用示例。创建一个类来保存通信类的选项,让通信类具有默认值,然后使用用户设置初始化通信。
示例设置类。
public class ComSettings
{
public int? Port;
public string? Address;
public bool? KeepAlive;
}
在构造函数中使用这些设置的示例类。
public class ComLibrary
{
private static ComSettings _defaults = new ComSettings { Port = 80, Address = "localhost" };
protected ComSettings settings;
public ComLibrary(ComSettings pSettings)
{
this.settings = ObjectMerge<ComSettings>(_defaults, pSettings);
}
}
这将允许不同的类使用,ComSettings
但每个类可能有不同的默认值。唯一的限制是字段/属性必须支持null
分配。
这是ObjectMerge
.
/// <summary>
/// Creates a new object that contains the properties of the two objects merged together.
/// </summary>
/// <typeparam name="T">The class type to merge.</typeparam>
/// <param name="pDefaults">Instance of the defaults object.</param>
/// <param name="pSettings">Instance of the settings object.</param>
/// <returns>A new instance of T with the merged results.</returns>
public static T ObjectMerge<T>(T pDefaults, T pSettings, bool pMergeFields = true, bool pMergeProperties = true) where T : class, new()
{
T target = new T();
Type type = typeof(T);
List<MemberInfo> infos = new List<MemberInfo>(type.GetMembers());
foreach (MemberInfo info in infos)
{
// Copy values from either defaults or settings
if (pMergeFields && info.MemberType == MemberTypes.Field)
{
FieldInfo field = (FieldInfo)info;
if (field.IsPublic)
{
object value = field.GetValue(pSettings);
value = (value == null) ? field.GetValue(pDefaults) : value;
field.SetValue(target, value);
}
}
// Copy values from either defaults or settings
if (pMergeProperties && info.MemberType == MemberTypes.Property)
{
PropertyInfo prop = (PropertyInfo)info;
if (prop.CanWrite && prop.CanRead)
{
object value = prop.GetValue(pSettings, null);
value = (value == null) ? prop.GetValue(pDefaults, null) : value;
prop.SetValue(target, value, null);
}
}
}
return target;
}
这是一个简单的单元测试。
/// <summary>
///This is a test class for CoreUtilsTest and is intended
///to contain all CoreUtilsTest Unit Tests
///</summary>
[TestClass()]
public class CoreUtilsTest
{
/// <summary>
/// A class to perform testing on.
/// </summary>
public class MyClassA
{
public string Param1;
public string Param2;
public string Param3;
}
/// <summary>
/// A class to perform testing on.
/// </summary>
public class MyClassB
{
private string _param1;
public string Param1
{
get { return _param1; }
set { _param1 = value; }
}
private string _param2;
public string Param2
{
get { return _param2; }
set { _param2 = value; }
}
private string _param3;
public string Param3
{
get { return _param3; }
set { _param3 = value; }
}
}
/// <summary>
///A test for SetProperties
///</summary>
[TestMethod()]
public void Merging_Fields()
{
MyClassA defaults = new MyClassA { Param1 = "defaults" };
MyClassA settings = new MyClassA { Param2 = "settings" };
MyClassA results = CoreUtils.ObjectMerge<MyClassA>(defaults, settings);
Assert.AreEqual("defaults", results.Param1);
Assert.AreEqual("settings", results.Param2);
Assert.AreEqual(null, results.Param3);
}
[TestMethod()]
public void Merging_Properties()
{
MyClassB defaults = new MyClassB { Param1 = "defaults" };
MyClassB settings = new MyClassB { Param2 = "settings" };
MyClassB results = CoreUtils.ObjectMerge<MyClassB>(defaults, settings);
Assert.AreEqual("defaults", results.Param1);
Assert.AreEqual("settings", results.Param2);
Assert.AreEqual(null, results.Param3);
}
}