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

During development of my project I have observed a strang behaviour (As per my understanding about OOPS concepts) of interface and class. Below are the details:

I have a DBOperations class impementing 2 interafces IReadOnly and IDML.

class DBOperations : IDML,IReadOnly
    {
        public string UpdateDB()
        {
            return "Update Data";
        }

        public string SelectData()
        {
            return "Select Data";
        }
    }


public interface IDML
    {
        string UpdateDB();
    }


public interface IReadOnly
   {
       string SelectData();
   }


The idea is that for select operations I would use IReadOnly interface and for CRUD operations I would use IDML interface.

In my calling class I am able to access
UpdateDB
method from IDML interface from reference of IReadOnly interface !

IReadOnly objIReadOnly = new DBOperations();
            IDML objIDML = new DBOperations();
            
            Console.WriteLine(objIReadOnly.SelectData());
            Console.WriteLine(objIDML.UpdateDB());
            Console.WriteLine(objIReadOnly.GetType().GetMethod("UpdateDB").Invoke(objIReadOnly, null)); // Able to execute UpdateDB method Why ??


Please advise on the behavour.

Thanks in advance !


Regards,
Divya

What I have tried:

Need assistance in understanding the behaviour.
Posted
Updated 19-Apr-17 0:12am
Comments
Sinisa Hajnal 19-Apr-17 6:16am    
Just from reading the code (didn't check):
Because GetType will return the class type and DBOperations contains UpdateDB.

If you try objIReadOnly.UpdateDB it should fail, but calling from the object instance, you get access from the class not from the interface.

1 solution

Because the object objIReadOnly contains is a reference to a DBOperations object, which implements both interfaces, and thus has both methods.

When you use reflection, it's a runtime thing: the object is examined and a method of that signature is located (if it exists) and executed. Since the object that your variable references implements both the IDML and IReadOnly interfaces, it also implements both UpdateDB and SelectData, so Reflection can find both methods and call them. At run time, the type of the variable is irrelevant: the object it refers to is used. If the requested method is found, a run time exception is thrown.
Compile time is different: it uses the variable type to decide if the object definitely exists in all objects that could be referred to. If it does, then fine. Otherwise a compiler error is generated and the code cannot be run.
 
Share this answer
 
Comments
Divymital 20-Apr-17 0:36am    
Thanks for the response. I agree but is this not a shortcoming of using interface as the class using IReadOnly interface is entitled to operate on SelectData method only, However if the class is able to access Update data in any way (Here reflection) it may be a security concern.Can you please suggest some approach or workaround (If any) so that the class using IReadOnly interface cant access UpdateDb method even with reflection ?

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