Ais*_*war 6 .net c# generics casting
我正在从EmguCV扩展ImageBox控件.控件的Image属性可以设置为实现IImage接口的任何内容.
以下所有实现此接口:
Image<Bgr, Byte>
Image<Ycc, Byte>
Image<Hsv, Byte>
Run Code Online (Sandbox Code Playgroud)
现在我想Draw在上述类型的对象上调用该方法(它可能是什么).
问题是当我访问Image属性时,返回类型是IImage.IImage没有实现该Draw方法,但以上所有都做到了.
我相信我可以将类型的对象转换为IImage上面的一个(右边的),我可以访问该Draw方法.但我怎么知道正确的是什么?如果您有更好的方法,请同时提出建议.
编辑
对不起,我忘了提一条重要信息.上述每个类的Draw方法都有一个不同的参数.对于例如Draw用于Image<Bgr, Byte>采用类型的参数Bgr和Draw用于Image<Hsv, Byte>采用类型的参数Hsv来代替.
像这样的事情怎么样:
void foo( IImage image )
{
if ( image is Image<Bgr, Byte> )
{
((Image<Bgr, Byte>)(image)).Draw();
}
// handle the other types the same way.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1777 次 |
| 最近记录: |