且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用条件更改访问修饰符

更新时间:2023-12-04 20:22:34

思考您要问的问题:最初必须拥有所有方法public才能完全编译代码.尽管有可能(可能我还没有尝试过)在运行时使用反射更改访问修饰符,但这并不是明智的选择,因为编译器已经为方法调用进行了编码.

而是在执行例程时检查用户的访问级别-并在此时抛出异常或类似情况.
Think about what you are asking: You would have to have all the methods initially public in order to compile your code at all. While it is (probably, I haven''t tried) possible to change the access modifier at runtime using reflection, it is not a sensible thing to do, as the compiler will already have coded for the method call already.

Instead, check the access level of the user when you execute the routine - and throw an exception or similar at that point.


尽管我不明白为什么有人想要更改访问修饰符在编译时,我认为这是可行的.
查看#if DEBUG - #endif子句.现在让我们考虑一个
Though I do not understand why somebody would like to change the access modifiers at compile time, I think it is feasable.
Look at the #if DEBUG - #endif clause. Now let''s consider a
#if PUBLIC
    public void A()
#else
    protected void A()
#endif
    {
        //here comes the body of A()
    }