具有反射的C#中的接口和类行为 [英] Interface and class behavour in C# with reflection

查看:54
本文介绍了具有反射的C#中的接口和类行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





在我的项目开发过程中,我观察到了界面和类的strang行为(根据我对OOPS概念的理解)。以下是详细信息:



我有一个DBOperations类迫使2个交互式IReadOnly和IDML。



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





我的想法是,对于选择操作,我会使用IReadOnly接口,对于CRUD操作,我会使用IDML接口。



在我的调用类中,我可以从IDML接口访问



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

方法IReadOnly界面!



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





请告知行为。



提前致谢!





问候,

Divya



我尝试了什么:



需要帮助来理解行为。



Please advise on the behavour.

Thanks in advance !


Regards,
Divya

What I have tried:

Need assistance in understanding the behaviour.

推荐答案

因为对象objIReadOnly包含对DBOperations对象的引用,它实现了两个接口,因此有两种方法。



当你使用反射时,它是一个运行时的东西:检查对象并找到该签名的方法(如果它存在)并执行。由于您的变量引用同时实现了IDML和IReadOnly接口,因此它还实现了UpdateDB和SelectData,因此Reflection可以找到这两种方法并调用它们。在运行时,变量的类型是无关紧要的:使用它引用的对象。如果找到所请求的方法,则抛出运行时异常。

编译时间不同:它使用变量类型来确定对象是否确实存在于可以引用的所有对象中。如果确实如此,那很好。否则会生成编译器错误,并且无法运行代码。
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.


这篇关于具有反射的C#中的接口和类行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆