简体   繁体   中英

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 . 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. 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.

    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:

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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