【问题标题】:C# uncommon access to object's propertiesC# 对对象属性的不常见访问
【发布时间】:2016-05-10 03:09:11
【问题描述】:

我有几个主对象。每个 ot 他们都有从属对象的列表。每个从属对象都有两个字段:field1 和 field2。仅当请求字段的主要对象不是从属对象的所有者时,我才需要访问主要对象中的字段。

class SlaveObj()
{
    ...
    private readonly int field1;
    private readonly string field2;
    ...
    public int GetField1()
    {
        // if asker object is not my owner
        // return field1
    }
}

class MainObj()
{
    ...
    List<SlaveObj> slaves = new List<SlaveObj>();
    ...
    public int GetField1(MainObj other)
    {
        return other.slaves[0].GetField1();
    }
}

首先,我尝试的是this。我只是试图检查,就像在第一个答案中一样,提问者是什么对象。但是对于 MainObj 的任何实例,我都有类似 Project1.MainObj 的东西。所以,我无法识别提问者是否是所有者。

更改后的代码(不能按我的意愿工作)

class SlaveObj()
{
    ...
    private MainObj owner;
    private readonly int field1;
    private readonly string field2;
    ...
    public int GetField1(MainObj asker)
    {
        if(asker != owner) return field1;
    }
}

class MainObj()
{
    ...
    List<SlaveObj> slaves = new List<SlaveObj>();
    ...
    public int GetField1(MainObj other)
    {
        return other.slaves[0].GetField1(this);
    }
}

【问题讨论】:

  • 当您执行slaves[0].GetField1() 时,SlaveObj 不知道从哪个MainObj 实例(如果有!)调用了该方法。您需要为SlaveObj 提供一种方法来识别其包含的MainObj,例如,通过构造函数注入在question you link to 中显示。
  • 您能否添加一个示例,何时GetField1() 应返回field1 以及何时field2(我不清楚您所说的所有者是什么)?也许您不需要SlaveObj.GetField1(),只需检查MainObj.GetField1() 中的所有权?
  • @Sinatr 我添加了我的一段代码。我需要知道外部 Main 对象的一个​​ SlaveObj 的属性,但不知道它自己的 slaveObj 属性。
  • @gekOn 如果主对象不是SlaveObj.GetField1()内部从属对象的主对象,你想返回什么?

标签: c# oop properties


【解决方案1】:

我的朋友,这应该能如你所愿。但是你必须给父对象添加 ID。

    internal class SlaveObj
{
    private MainObj owner;
    private readonly int field1;
    private readonly string field2;

    public SlaveObj(MainObj parent)
    {
        this.owner = parent;
    }

    public int GetFieldID(int askerID)
    {
        if (askerID != owner.ID) return field1;
        return 0;
    }
}

class MainObj
{
    public int ID;

    List<SlaveObj> slaves = new List<SlaveObj>();

    public int GetFieldID(MainObj other)
    {
        return other.slaves[0].GetFieldID(this.ID);
    }

    public MainObj(int id)
    {
        this.ID = id;
    }
}

而且您之前的版本没有成功,因为您的主要对象是引用类型,默认情况下通过引用进行比较。所以最好使用对象 ID 在 MainObj 中实现 IEqualtyComparer:

 class MainObj : IEqualityComparer

【讨论】:

  • ID 很有趣 :-) 谢谢
【解决方案2】:

很容易解决

class SlaveObj()
{
    MainObj _owner;
    readonly int _field1 = ...;
    readonly string _field2 = ...;

    // you need a way to set owner, e.g. constructor parameter
    public SlaveObj(MainObj owner)
    {
        _owner = owner; // good example why underscore in field name is good
    }

    // return type: object
    // renamed
    // using C# 6.0 features to confuse people
    public object GetFieldX(MainObj asker) => asker != _owner ? _field1 : _field2;
}

class MainObj()
{
    List<SlaveObj> _slaves = new List<SlaveObj>();

    // return first slave field value
    // has nothing to do with instance, therefore static
    // will return null if no slave
    public static object GetFieldX(MainObj owner) => owner?.FirstOrDefault()?.GetFieldX(this);
}

但它并不漂亮。

【讨论】:

    猜你喜欢
    • 2017-08-03
    • 2011-02-22
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    相关资源
    最近更新 更多