小编rea*_*kle的帖子

为什么C#没有正确绑定到通用重写方法?

我已经定义了以下类和方法:

 using System;
 using System.Linq.Expressions;
 using System.Windows.Forms;

 public class ReturnValue<T, S> {}

 public class Something<T>
 {
     // Sorry about the odd formatting. Trying to get it to fit nicely...
     public ReturnValue<T, C>
     Do<C, S>(C control, Expression<Func<C, S>> controlProperty)
     where C : Control
     {
         return new ReturnValue<T, C>();
     }

     public ReturnValue<T, ToolStripItem>
     Do<S>(ToolStripItem control, Expression<Func<ToolStripItem, S>> controlProperty)
     {
         return new ReturnValue<T, ToolStripItem>();
     }
 }
Run Code Online (Sandbox Code Playgroud)

编译好了.呜啊!中途到那里.然后,我尝试稍后使用它像这样的代码:

 var toolStripItem = new ToolStripStatusLabel();

 var something = new Something<string>();
 something.Do(toolStripItem, t => t.Text); // Does …
Run Code Online (Sandbox Code Playgroud)

.net c# generics compiler-errors

8
推荐指数
1
解决办法
564
查看次数

标签 统计

.net ×1

c# ×1

compiler-errors ×1

generics ×1