Prevent Cast Between Two Interfaces

本秂侑毒 提交于 2021-02-10 15:40:19

问题


I have an internal class which contains implementation details, including properties, which should not be publicly accessible. I have to pass this class to public callers, and allow them to pass it around, store it, serialize it, or do basically whatever they want with it. The class can be accessed publicly in two different ways. These ways are exposed as two public interfaces. I would like to prevent the user from casting between the two public interfaces. Here is some code to demonstrate what I am talking about.

void Main()
{
    var container = new ContainsDetails(5);

    Console.WriteLine(container.LessDetail.TheDetails);
    var more = (ContainsDetails.IFooPlus)(container.LessDetail);
    more.TheDetails = 10;
    Console.WriteLine(container.LessDetail.TheDetails);
}

public class ContainsDetails {
    public interface IFooPlus {
        int TheDetails {
            get;
            set;
        }
    }

    public interface IFoo {
        int TheDetails {
            get;
        }
    }

    private readonly Detail _myDetail;

    public ContainsDetails(int x) {
        _myDetail = new Detail(x);
    }

    public IFoo LessDetail {
        get {
            return _myDetail;
        }
    }

    public IFooPlus MoreDetail {
        get {
            return _myDetail;
        }
    }

    public bool ProcessFoo(IFoo foo) {
        var theDeets = foo as Detail;
        if (theDeets != null) {
            return theDeets.TheDetails++%2 == 0;
        } else {
            throw new ArgumentException("foo argument must have originated from ContainsDetails.");
        }
    }

    private class Detail : IFooPlus, IFoo {
        private int _myX;

        private Detail() {}

        internal Detail(int x) {
            _myX = x;
        }

        public int TheDetails {
            get {return  _myX; }
            set { _myX = value; }
        }
    }
}

Output is:

5
10

I would like for the output above to be:

5
System.InvalidCastException thrown.....

The class ContainsDetails is just present for demonstration. In my actual code the assembly fills the role of ContainsDetails. Similarly, Detail is an internal class in my actual code.

Here the user knows about both IFooPlus and IFoo. The user does not know that Detail exists. The user also does not know that the object returned by LessDetail is convertible to an IFooPlus. My code returns the same Detail object to the user at different times, and will return it as IFoo or IFooPlus depending on some complicated state of Detail and other internal objects. Also note the ProcessFoo method in ContainsDetails. My code needs to be able to accept an IFoo from the user, and process it as a Detail.

I would like for the cast var more = (ContainsDetails.IFooPlus)(container.LessDetail) to generate an InvalidCastException. It is prohibitively expensive to do runtime checking on all IFooPlus operations to determine if the Detail object is in the correct state for those operations. Instead I would like the user never to be able to treat an IFoo as an IFooPlus.

The only option I can think of is to embed some IFoo member subclass inside of Detail and have the Detail and the subclass hold references to each other. Yuck.

Is there a best practice for preventing this sort of casting, or should I just enclose the IFoo functionality in a member subclass of Detail?


回答1:


The simplest approach is to have a read-only wrapper which has a reference to the writable one. So LessDetail would always return the same wrapper reference, but MoreDetail would return a reference to the "real" type. You can then make both of them implement an internal interface which allows you to do whatever you need to, with the wrapper probably proxying those calls to the underlying object.

Alternatively, you could try to revisit your design - it doesn't sound ideal at the moment, either in terms of not being able to cast between the types or that ProcessFoo accepts any IFoo at compile-time, but is really only able to handle one particular implementation. This feels like an abuse of inheritance to me.



来源:https://stackoverflow.com/questions/11389900/prevent-cast-between-two-interfaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!