如何將每個 AlarmModel 的“名稱”屬性動態設定為父類中的模型名稱(即 HardwareFault)?
public class NotificationModel
{
public string UnrelatedProperty { get; set; } // any solution should ignore other properties.
public AlarmModel HardwareFault{ get; set; }
public AlarmModel TimeoutFault { get; set; }
public AlarmModel GenericFault { get; set; }
}
public class AlarmModel
{
public string Name { get; set; }
public bool isActive { get; set; }
public bool isEnabled { get; set; }
public bool isSilenced { get; set; }
}
uj5u.com熱心網友回復:
您應該將屬性“名稱”移至父類。并嘗試將其擴展/繼承到子類。
uj5u.com熱心網友回復:
如果您已經 NotificationModel使用也已初始化的HardwareFault, TimeoutFault,GenericFault屬性進行了初始化,則可以使用以下示例:
var notificationModel = new NotificationModel()
{
HardwareFault = new AlarmModel(),
GenericFault = new AlarmModel(),
TimeoutFault = new AlarmModel()
};
notificationModel?.GetType().GetProperties().Where(p => p.PropertyType.Name == "AlarmModel") // Or p.PropertyType == typeof(AlarmModel)
.ToList().ForEach(alarmModelProperty =>
{
// Get AlarmModel property instance
var alarmModelInstance = alarmModelProperty.GetValue(notificationModel);
// Get Name property
var nameProperty = alarmModelIntance?.GetType().GetProperty("Name");
// Set Name property value with name of AlarmModel property
nameProperty?.SetValue(alarmModelInstance, alarmModelProperty.Name);
});
如果您已經初始化 NotificationModel, 但沒有初始化HardwareFault, TimeoutFault,GenericFault屬性,您可以使用以下示例:
var notificationModel = new NotificationModel();
notificationModel?.GetType().GetProperties().Where(p => p.PropertyType.Name == "AlarmModel") // Or p.PropertyType == typeof(AlarmModel)
.ToList().ForEach(alarmModelProperty =>
{
// Initialize new AlarmModel
var alarmModelInstance = Activator.CreateInstance(alarmModelProperty.PropertyType);
// Get Name property of AlarmModel
var nameProperty = alarmModelInstance.GetType().GetProperty("Name");
// Set Name property of AlarmModel
nameProperty?.SetValue(alarmModelInstance, alarmModelProperty.Name);
// Set AlarmModel property of NotificationModel
alarmModelProperty.SetValue(notificationModel, alarmModelInstance);
});
如果您需要使用反射進行完全初始化,您可以使用以下示例:
var notificationModelType = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(type => type.Name == "NotificationModel"); // Or type == typeof(NotificationModel)
if (notificationModelType is null) // No NotificationModel found in current assembly
return;
// Initializing NotificationModel
var notificationModel = Activator.CreateInstance(notificationModelType);
notificationModel?.GetType().GetProperties().Where(p => p.PropertyType.Name == "AlarmModel") // Or p.PropertyType == typeof(AlarmModel)
.ToList().ForEach(alarmModelProperty =>
{
// Initialize new AlarmModel
var alarmModelInstance = Activator.CreateInstance(alarmModelProperty.PropertyType);
// Get Name property of AlarmModel
var nameProperty = alarmModelInstance.GetType().GetProperty("Name");
// Set Name property of AlarmModel
nameProperty?.SetValue(alarmModelInstance, alarmModelProperty.Name);
// Set AlarmModel property of NotificationModel
alarmModelProperty.SetValue(notificationModel, alarmModelInstance);
});
如果NotificationModel在當前程式集中宣告 NOT (因此它不會在 中Assembly.GetExecutingAssembly().GetTypes()) - 您可以在 中搜索所有加載的程式集AppDomain.CurrentDomain:
var notificationModelType = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.FirstOrDefault(type => type.Name == "NotificationModel");
uj5u.com熱心網友回復:
如果我理解正確,您想將Name每個AlarmModel的屬性設定為父級中的屬性名稱NotificationModel嗎?
如果是這種情況,您可以執行以下操作:
public class NotificationModel
{
private AlarmModel _hardwareFault;
private AlarmModel _timeoutFault;
private AlarmModel _genericFault;
public string UnrelatedProperty { get; set; } // any solution should ignore other properties.
public AlarmModel HardwareFault
{
get => _hardwareFault;
set
{
_hardwareFault = value;
SetName(value);
}
}
public AlarmModel TimeoutFault
{
get => _timeoutFault;
set
{
_timeoutFault= value;
SetName(value);
}
}
public AlarmModel GenericFault
{
get => _genericFault;
set
{
_genericFault= value;
SetName(value);
}
}
private void SetName(AlarmModel model, [CallerMemberName] string propertyName = null)
{
model.Name = propertyName;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/363449.html
