接口方法约束 [英] Interface Method Constraints

查看:114
本文介绍了接口方法约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以对类内部的方法施加约束,例如:

 公共  void  ConstrainedMethod< T>(T可变)其中 T:IConstraint
{
   // 可以使用IConstraint接口访问变量
} 



但是,该方法所驻留的对象(ConstrainedMethod< t>)我想成为另一个接口的一部分.但是,我没有看到如何在界面中放置约束.如果不是,而是约束对象,则编译器会说约束必须与接口匹配.

??????
我希望这样,但是语法不正确.

 公共 接口 IInterfaceWithConstrainedMethod
{
    void  ConstrainedMethod< T>(T变量)其中 T:IConstraint;
} 

解决方案

可能是一个误会,但以下内容应该可以:

 公共 界面 IInterfaceWithConstrainedMethod {
    void  ConstrainedMethod< T>(T变量)其中 T:System.Windows.IInputElement;
}

公共  a:IInterfaceWithConstrainedMethod {
   公共 无效 ConstrainedMethod< T>(可变性)其中 T:System.Windows.IInputElement {
   }

}
无效 SomeMethod(){
   ( a()).ConstrainedMethod( System.Windows.Controls.TextBox());
   // (new a()).ConstrainedMethod(new System.Windows.Controls.StyleSelector()); 
} 


后一个调用在编译时会失败,

类型'System.Windows.Controls.StyleSelector'不能用作通用类型或方法'SomeProject.a.ConstrainedMethod(T)'中的类型参数'T'.没有从'System.Windows.Controls.StyleSelector'到'System.Windows.IInputElement'的隐式引用转换.抱怨:

 界面 IConstraint
{
     void  SomeFunction( int  anInteger);
}

接口 IInterfaceWithConstrainedMethod
{
     void  ConstrainedMethod< T>(T变量)其中 T:IConstraint;
}



我不知道您对IConstraint的定义是什么,所以问题可能出在这里.
您为什么不张贴一个简化为可以重现您问题的最短形式的完整示例.这样我们可以更轻松地为您提供帮助.

问候,

曼弗雷德(Manfred)


We can put constraints on methods inside our classes as such:

public void ConstrainedMethod<T>(T varaible) where T : IConstraint
{
   //can access variable with the IConstraint interface
}



However, the ojbect the method resides with in (the ConstrainedMethod<t>) I would like to be part of another interface. However I am not seeing how one places the constraint in the interface. And if one does not, but constrains the object then the compiler says the constraints must match the interface.

??????
I would expect this but the syntax is incorrect.

public interface IInterfaceWithConstrainedMethod
{
   void ConstrainedMethod<T>(T varaible) where T : IConstraint;
}

解决方案

Perhaps a misunderstanding but the following should be fine:

public interface IInterfaceWithConstrainedMethod {
   void ConstrainedMethod<T>(T varaible) where T : System.Windows.IInputElement;
}

public class a : IInterfaceWithConstrainedMethod {
   public void ConstrainedMethod<T>(T varaible) where T : System.Windows.IInputElement {
   }

}
void SomeMethod() {
   (new a()).ConstrainedMethod(new System.Windows.Controls.TextBox());
   //(new a()).ConstrainedMethod(new System.Windows.Controls.StyleSelector());
}


The latter call would fail at compile time with

The type 'System.Windows.Controls.StyleSelector' cannot be used as type parameter 'T' in the generic type or method 'SomeProject.a.ConstrainedMethod(T)'. There is no implicit reference conversion from 'System.Windows.Controls.StyleSelector' to 'System.Windows.IInputElement'


I just tried the following and the compiler had nothing to complain about:

interface IConstraint
{
    void SomeFunction(int anInteger);
}

interface IInterfaceWithConstrainedMethod
{
    void ConstrainedMethod<T>(T varaible) where T : IConstraint;
}



I don''t know what your definition of IConstraint is, so maybe the problem lies there.
Why don''t you post a complete example reduced to the shortest possible form that will reproduce your problem. It would make it easier for us to help you then.

Regards,

Manfred


这篇关于接口方法约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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