我申报会员时可以指定接口吗? [英] Can I specify interfaces when I declare a member?

查看:96
本文介绍了我申报会员时可以指定接口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的班级成员作为控制,并且要实现我们定义的接口。

I need a member of my class to be a Control, and for it to implement an interface we define.

如果我这样声明......

If I declare it like this...

public class MyClass
{
    public Control MyMember;
}

...然后我没有得到接口方法,但如果我声明就像这样...

... then I don't get the interface methods, but if I declare it like this...

public class MyClass
{
    public IMyInterface MyMember;
}

...然后我没有得到Control方法。有没有办法指定必须将MyMember初始化为从两者继承的类型?我在MSDN上找不到一个。类似......

...then I don't get the Control methods. Is there a way to specify that MyMember must be initialised to a type that inherits from both? I can't find one on MSDN. Something like...

public class MyClass
{
    public Control : IMyInterface MyMember;
}

...或...

public class MyClass
{
    public Control MyMember : IMyInterface;
}

...除了这些都不起作用。我可以在声明成员时指定接口,如果是,如何?

... except that neither of those work. Can I specify interfaces when I declare a member, and if so, how?

推荐答案

您可以使用带有约束的泛型:

You can use generics with constraints:

public interface MyClass {
    public T GetMyControl() where T : Control, IMyInterface { /* ........ */ }
}

这篇关于我申报会员时可以指定接口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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