[英]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.