简体   繁体   English

C#罕见地访问对象的属性

[英]C# uncommon access to object's properties

I have several master objects. 我有几个主要对象。 Each ot them has list of slave objects. 它们每个都有从属对象列表。 Each of the slave objects has two fields: field1 and field2 . 每个从属对象都有两个字段: field1field2 I need to have access to the fields from the main objects ONLY if the main object, who asked for the field, is not an owner of the slave object. 仅当请求字段的主对象不是从属对象的所有者时,才需要从主对象访问字段。

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();
    }
}

First, what I tried, was this . 首先,我尝试过的是这个 I just tried to check, like in the first answer, what object is the asker. 就像在第一个答案中一样,我只是尝试检查哪个对象是询问者。 But I have something like Project1.MainObj for any instance of MainObj. 但是对于MainObj的任何实例,我都有类似Project1.MainObj的东西。 So, I can't recognize whether the asker is the owner or not. 因此,我无法识别请求者是否是所有者。

Code after changes (not works as i want) 更改后的代码(不适用于我想要的)

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);
    }
}

My friend, this should work out the way you need. 我的朋友,这应该可以解决您需要的方式。 But you gotta add IDs to parent objects. 但是您必须将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;
    }
}

And your previous version did not work out because your main objects are of reference type thich are compared by reference by default. 您的先前版本无法正常运行,因为您的主要对象是引用类型,默认情况下会通过引用对其进行比较。 So better use object IDs implement IEqualtyComparer in MainObj: 因此,最好使用对象ID在MainObj中实现IEqualtyComparer:

 class MainObj : IEqualityComparer

It's easy to fix 容易修复

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);
}

but it's not pretty. 但这不漂亮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM