簡體   English   中英

如何從父 class 中的名稱動態設置屬性?

[英]How do I Dynamically set properties from the name in a parent class?

如何從父 class 動態地將每個 AlarmModel 的“名稱”屬性設置為 model(即 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; }
}

您應該將屬性“名稱”移動到父 class。 並嘗試將其擴展/繼承給子 class。

如果我理解正確,您想將每個AlarmModelName屬性設置為父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 SetName(AlarmModel model, [CallerMemberName] string propertyName = null)
    {
        model.Name = propertyName;
    }
}

如果您已經使用已經初始化的HardwareFaultTimeoutFaultGenericFault屬性初始化了NotificationModel ,則可以使用此示例:

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 ,但沒有初始化HardwareFaultTimeoutFaultGenericFault屬性,則可以使用此示例:

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聲明不在當前程序集中(因此它不會在Assembly.GetExecutingAssembly().GetTypes()中) - 您可以在AppDomain.CurrentDomain中搜索所有已加載的程序集:

var notificationModelType = AppDomain.CurrentDomain.GetAssemblies()
                                                   .SelectMany(assembly => assembly.GetTypes())
                                                   .FirstOrDefault(type => type.Name == "NotificationModel");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM