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();

        }
    }
}
Office ProductivityC#.NET Programming

Avatar of undefined
Last Comment
saragani
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

Avatar of saragani
saragani

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Arun_Ganwar
Arun_Ganwar

ASKER

no want it in class receving the call :)
Avatar of Arun_Ganwar
Arun_Ganwar

ASKER

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

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
Avatar of Arun_Ganwar
Arun_Ganwar

ASKER

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

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
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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.
Avatar of saragani
saragani

Thanks for the compliments :-)
Avatar of Arun_Ganwar
Arun_Ganwar

ASKER

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);
                }
Avatar of Arun_Ganwar
Arun_Ganwar

ASKER

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 :)
Avatar of saragani
saragani

You can probably find everything on google and msdn
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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?
Avatar of Arun_Ganwar
Arun_Ganwar

ASKER

saragani... how do we come to kw the which frame to skip ...
Avatar of saragani
saragani

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


.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo