Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Is this possible to specify access modifier at compile time?

Ex:
if(accessModifier.public)
    change method void A() into Public void A()
else if(accessModifier.protected)
    change method void A() into Protected void A()


I need to enable or disable the certain methods access permission to the outer world based on the condition get it from users.

how can i do this?

Thanks in advance

Best Regards,
SRJ
Posted
Updated 16-Sep-12 20:58pm
v4

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.
 
Share this answer
 
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
C#
#if PUBLIC
    public void A()
#else
    protected void A()
#endif
    {
        //here comes the body of A()
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900