将 属性 地址分配给另一个 属性 (参考)。更改 属性 时更改另一个 属性。 C#

Assign property address to another property (reference). Change property when another property is changed. c#

我想将一个 属性 分配给另一个 class。更改传递的 属性 时自动更改此 属性。类似在 C++ 中引用相同地址的东西。在 C# 中可以吗?

我想实现的效果是,当 PressureValve class 中的 base 属性 "IsOpen" 改变时,同时自动改变 Component class 中的 propert "IsEnabled"。

提前感谢您的帮助。

类:

    class Component
    {
        public Component(int id, string description, ref anotherPropertyFromDifferentClass)
        {
            Id = id;
            Description = description;
            //Assign IsEnabled to another property, change it always when anotherPropertyFromDifferentClass is changed.
            IsEnabled = ref anotherPropertyFromDifferentClass;
        }
        public int Id { get; set; }
        public string Description { get; set; }
        public bool IsEnabled { get; set; }
    }
    class PressureValve
    {
        public bool IsOpen { get; set; }
    }

节目:

            PressureValve pressureValve = new PressureValve();
           
            //status read from external device, changing during programm running
            pressureValve.IsOpen = externalInfo;
            //create component, change component IsEnabled property automatically when pressureValve.IsOpen is changed
            Component component = new Component(1,"Valve status", ref pressureValve.IsOpen)



 

编辑: 我想扩展已接受答案中建议的功能。 如何从 CommonProperty 动态分配 属性?

例如,CommonProperty属性中有多个class:

    class CommonProperty //Change this class name to Your desired one.
{
    public bool IsOpen { get; set; }
public bool IsPoweredUp{ get; set; }
public bool IsRunning{ get; set; }

}

那我想有多个Component对象class

class Component
{
    public int Id { get; set; }
    public string Description { get; set; }
    public bool IsEnabled =>  //Here to assign property dynamically while creating object.
    public CommonProperty ValveStatus { get; } //Set this only on Constructor since we want to update it automatically through the object reference

    public Component(int id, string description, CommonProperty valveStatus)
    {
        Id = id;
        Description = description;
        ValveStatus = valveStatus;
        //Assign IsEnabled to another property, change it always when anotherPropertyFromDifferentClass is changed.
    }
}

并创建对象:

//create component, change component IsEnabled property automatically when assigned property from pressureValve is changed
Component component = new Component(1, "Valve status", commonProp, propertyName or type?);

您可以使用 ref int 来完成它,但需要更多接线。

这是最简单的方法,而且维护起来更干净。另外,以后如果 auto-change 有多个参数,你可以简单地将它们添加到 CommonProperty class:

为要自动更新的属性声明 Class:

class CommonProperty //Change this class name to Your desired one.
{
    public bool IsOpen { get; set; }
}

更新现有的 class 定义:

class PressureValve
{
    public CommonProperty ValveStatus { get; set; }
}

class Component
{
    public int Id { get; set; }
    public string Description { get; set; }
    public bool IsEnabled => ValveStatus.IsOpen; //Only Getter
    public CommonProperty ValveStatus { get; } //Set this only on Constructor since we want to update it automatically through the object reference

    public Component(int id, string description, CommonProperty valveStatus)
    {
        Id = id;
        Description = description;
        ValveStatus = valveStatus;
        //Assign IsEnabled to another property, change it always when anotherPropertyFromDifferentClass is changed.
    }
}

这是无需太多更改即可轻松完成的方法,

测试方法如下:

PressureValve pressureValve = new PressureValve();

var commonProp = new CommonProperty {IsOpen = externalInfo};

//status read from external device, changing during programm running
pressureValve.ValveStatus = commonProp;
//create component, change component IsEnabled property automatically when pressureValve.IsOpen is changed
Component component = new Component(1, "Valve status", commonProp);

//Before Change
Console.WriteLine($"pressureValve IsEnabled: {pressureValve.ValveStatus.IsOpen.ToString()}");
Console.WriteLine($"component IsOpen: {component.IsEnabled.ToString()}");

//Change commonProp
commonProp.IsOpen = false; //since its an object, all the references in pressureValve & component will update
Console.WriteLine($"IsOpen Set to False");

//Check After Value Change
Console.WriteLine($"pressureValve IsEnabled: {pressureValve.ValveStatus.IsOpen.ToString()}");
Console.WriteLine($"component IsOpen: {component.IsEnabled.ToString()}");