Link to home
Start Free TrialLog in
Avatar of Arun_Ganwar
Arun_Ganwar

asked on

How do i stop the access for some classes

In the below example ...there is a public property for a private member in class program
both the class A and B hve obj of class program
If the call to set or get the value of private string pvtstr from A should be allowed but if call from class B should not be allowed

In simple how do i find from which class the cal is comming from ...to set or get the value of a private memeber using public property



namespace ExampleForProperty
{
    class Program
    {
        private string pvtStr;
        public string _pvtstr
        {
            get
            {
                return pvtStr;
            }
            set
            {
                pvtStr = value;
            }
        }

        static void Main(string[] args)
        {
            A ob = new A();
            B ob1 = new B();
            ob.calfrmA();
            ob1.calfrmB();
        }
    }

    class A
    {
        public void calfrmA()
        {
            Program obj = new Program();
           
           
        }
    }          

    class B
    {
        public void calfrmB()
        {
            Program obj1 = new Program();

        }
    }
}
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

I think you would have to build such functionality into the class making the call, not the class receiving the call.
Avatar of saragani
saragani

You cannot do it with a property.

A function might give you a solution (A function for getting the value from the property and a function for setting the value).

The function will also need to get the caller as an argument (Better have the caller implement an interface).

for example:

namespace ExampleForProperty
{
        class Program
    {
        private string pvtStr;

        public string GetPvtStr(ICallerAccess iCallerAccess)
        {
            if (iCallerAccess.AllowAccess)
            {
                return pvtStr;
            }
            else
            {
                throw new ApplicationException("Access Denied");
            }
        }

        public void SetPvtStr(string value, ICallerAccess iCallerAccess)
        {
            if (iCallerAccess.AllowAccess)
            {
                pvtStr = value;
            }
            else
            {
                throw new ApplicationException("Access Denied");
            }
        }

        static void Main(string[] args)
        {
            A ob = new A();
            B ob1 = new B();
            ob.calfrmA();
            ob1.calfrmB();
        }
    }

    class A : ICallerAccess
    {
        public void calfrmA()
        {
            Program obj = new Program();
            obj.SetPvtStr("12345", this);
        }

        public bool AllowAccess
        {
            get
            { 
                return true;
            }
        }
    }

    class B : ICallerAccess
    {
        public void calfrmB()
        {
            Program obj1 = new Program();
            obj1.SetPvtStr("12345", this);
        }

         public bool AllowAccess
        {
            get
            { 
                return false;
            }
        }
    }

    public interface ICallerAccess
    {
        bool AllowAccess { get; }
    }
} 

Open in new window

You might also be able to do it using reflection and checking the call stack, but I need to check if it's possible and if so then how to do it...
ASKER CERTIFIED SOLUTION
Avatar of saragani
saragani

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Arun_Ganwar

ASKER

no want it in class receving the call :)
saragani... let me try n get bck to u :)
You can't prevent the class from getting the call since the property/function is public.

If function is internal then you can have all classes in the same assembly access that property while classes in different assembly will not see it
saragani ... can u plz tell how can get the class name ...like its from A or B the cal is being made
I'm not sure you can do it with reflection, but you have my other solution where the sender (caller) is being sent to the function
The code supplied by saragani is very nice (I've learnt something) and only required a quick and minor modification to give you the class name.

However I still think this question of 'allowed' really belongs in the class that wants to perform the call.  Again saragani showed a nice implementation of that in an earlier comment.
Thanks for the compliments :-)
Hello Saragani  i got it
StackFrame frame = new StackFrame(1);
string str = frame.GetMethod().GetType().Name;
if (className == "ExampleForProperty.A")
                {
                    pvtStr = value;
                    Console.WriteLine("U hve access  " + className+ ");
                }
                else
                {
                    Console.WriteLine("sorry no access "+className);
                }
hello saragani can u share some documents on stack frames ... it would be grt help
it was cox of u i hve solved the above ptoblem
Thk u soo much :)
You can probably find everything on google and msdn
Just a final point.

As I and saragani have been saying implementing the 'allowed' in the class making the call is an alternative - for me the better way.

What you have now is in the recipient a list of class names for those that are allowed (or denied) access.  Nothing in the class making the call indicates if it will be alllowed or not.  This list needs to be maintained/updated.  Ask yourself one question - is this a good design for the future?
saragani... how do we come to kw the which frame to skip ...
Well, if you just want to get the frame of the caller than you go to frame at index 1
0 is your current position, and on each call to function the new function location is being pushed to index 0