简体   繁体   English

是否可以访问接口类型的其他成员?

[英]Is it possible to access the other members of an interface type?

I'm curious if this is possible, and if not, what are the reasons behind it if any, and how would one handle this programming scenario? 我很好奇这是否可能,如果没有,那么它背后的原因是什么,以及如何处理这种编程方案?

Let's say i have this interface: 假设我有这个界面:

public interface IBook
{
    void BookClient();
    void CancelClient();
}

and i have this class that implements the above interface: 我有这个实现上述接口的类:

public class Photographer : IBook
{
     public void BookClient()
     {
        // do something
     }

     public void CancelClient()
     {
        // do something
     }

     // non-interface methods
     public void SaveClients()
     {
        // do something
     }

     public void DeleteClients()
     {
        // do something
     }
} 

Now if I assign this class to an interface type somewhere in my code such as: 现在,如果我将此类分配给我的代码中的某个接口类型,例如:

IBook photo;
photo = new Photographer();

Is it possible to do this: 是否有可能做到这一点:

// non-interface member from the Photographer class
photo.SaveClients();

Can someone straighten me out on this issue and perhaps point me in the right direction regarding this. 有人可以在这个问题上理顺我,也许可以指出我正确的方向。 Thanks. 谢谢。

Yes, it's possible, but you have to cast your photo into Photographer first: 是的,这是可能的,但您必须先将photo投入Photographer

// non-interface member from the Photographer class
((Photographer)photo).SaveClients();

It's not possible with just photo.SaveClients() syntax, because you can easily create another class: 仅使用photo.SaveClients()语法是photo.SaveClients() ,因为您可以轻松地创建另一个类:

class TestClass : IBook
{
    public void BookClient()
    {
       // do something
    }
     public void CancelClient()
    {
       // do something
    }
}

And use it like that: 并使用它:

IBook photo;
photo = new Photographer();
photo = new TestClass();

// what should happen here?
photo.SaveClients();

So as long as you use the variable as an interface implementation, you can only access member declared in that interface. 因此,只要将变量用作接口实现,就只能访问该接口中声明的成员。 However, the object is still a class instance, so you can use other class members, but you have to explicitly cast to that type first. 但是,该对象仍然是一个类实例,因此您可以使用其他类成员,但必须首先显式转换为该类型。

Interface types can only reference interface members. 接口类型只能引用接口成员。 You're attempting to invoke a member of the class which is not part of the interface. 您正在尝试调用不属于该接口的类的成员。

You can try (can't test right now): 你可以试试(现在不能测试):

IBook photo;
photo = new Photographer();

(photo as Photographer).SaveClients();
// or
((Photographer)photo).SaveClients();

Yes, it's possible, using reflection. 是的,有可能,使用反射。 Also you can cast to Photographer and then call SaveClients() . 您也可以投射到Photographer ,然后调用SaveClients()

In my opinion a good solution is defining each group of actions you need to call in an interface, then, cast to that interface, and call the method you need: 在我看来,一个好的解决方案是定义您需要在界面中调用的每组操作,然后,转换为该界面,并调用您需要的方法:

public interface IBook
{
    void BookClient();
    void CancelClient();
}

public interface IClient
{
    void SaveClients();
}

And then use as: 然后用作:

IBook photo = new Photographer();
// now cast photo object as a IClient
IClient client = photo as IClient;
if (client != null)
{
    client.SaveClients();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通用接口/基类-无类型约束的访问成员 - Generic interface/base class - access members without type constraints 如何通过只有一个类型而不使用反射来访问一个类型实现的 static 抽象接口成员? - How to access a type's implemented static abstract interface members by having only a type without using reflection? 是否可以为我无法更改的类型实现接口(在 C# 的预览功能的上下文中:static 抽象接口成员) - Is it possible to implement an interface for a type I can't change (in the context of C#'s preview feature: static abstract interface members) 如何从接口访问派生类成员? - How to access derived class members from an interface? C#中接口成员的访问修饰符 - Access modifiers on interface members in C# 如何确定类型是否实现接口并使用该接口的成员 - How to determine if a type implements an interface and use members of that interface 接口实现上成员的返回类型必须与接口定义完全匹配? - The return type of the members on an Interface Implementation must match exactly the interface definition? 无法访问WPF中其他ViewModel类的成员 - Failing To Access Members of Other ViewModel Class in WPF 如何在C#中访问其他班级成员 - How to access other class members in C# 声明期间可以使用匿名类型成员吗? - Possible to use anonymous type members during declaration?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM