How do i stop the access for some classes

Arun_Ganwar
Arun_Ganwar used Ask the Experts™
on
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();

        }
    }
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant

Commented:
I think you would have to build such functionality into the class making the call, not the class receiving the call.

Commented:
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

Commented:
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...
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Commented:
Hi, you can also do:

var callerFunction = (new StackFrame(1).GetMethod().Name);

By this way you will get either: "calfrmA" or "calfrmB"

Author

Commented:
no want it in class receving the call :)

Author

Commented:
saragani... let me try n get bck to u :)

Commented:
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

Author

Commented:
saragani ... can u plz tell how can get the class name ...like its from A or B the cal is being made

Commented:
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
AndyAinscowFreelance programmer / Consultant

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

Commented:
Thanks for the compliments :-)

Author

Commented:
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);
                }

Author

Commented:
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 :)

Commented:
You can probably find everything on google and msdn
AndyAinscowFreelance programmer / Consultant

Commented:
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?

Author

Commented:
saragani... how do we come to kw the which frame to skip ...

Commented:
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


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial