是否有可能重写了IEnumerable在VC ++ / CLI? [英] Is it possible to override IEnumerable in VC++/CLI?

查看:158
本文介绍了是否有可能重写了IEnumerable在VC ++ / CLI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它返回一个的IEnumerable 的界面,我想是因为数据来自第三方托管的DLL来实现这一点VC ++ / CLI。

I have an interface which returns an IEnumerable, and I want to implement this in VC++/CLI because the data comes from a third-party unmanaged DLL.

到目前为止,我有:

public ref class MyEnumerable : IEnumerable<SomeType^> {
public:
    virtual IEnumerator<SomeType^>^ GetEnumerator();
}

不过,编译器会抱怨与C2393:共变的返回类型不支持托管类型

But the compiler complains with C2393: "Covariant returns types are not supported in managed types".

这是否意味着我不能执行的IEnumerable S在C ++中,还是有解决方法吗?

Does that mean that I cannot implement IEnumerables in C++, or is there a workaround?

推荐答案

哎呀,这是一个非常笨拙的错误消息。它是什么的真正的抱怨是缺少执行非通用系统::收藏集:: IEnumerable的::的GetEnumerator()方法。你必须实现它,因为一般的IEnumerable&LT;>接口继承非泛型之一。东西是有道理的,当仿制药首次加入.NET 2.0,今天没有那么多。我们还挺坚持与.NET 1.x的遗产。

Yikes, it is an awfully clumsy error message. What it is really complaining about is the missing implementation of the non-generic System::Collections::IEnumerable::GetEnumerator() method. You must implement it because the generic IEnumerable<> interface inherits the non-generic one. Something that made sense when generics were first added in .NET 2.0, not so much today. We're kinda stuck with the .NET 1.x legacy.

否则容易当你激活你的秘密去codeR环办,使它看起来像这样的:

Otherwise easy to do when you activate your secret decoder ring, make it look like this:

public ref class MyEnumerable : IEnumerable<SomeType^> {
public:
    virtual IEnumerator<SomeType^>^ GetEnumerator();
private:
    virtual System::Collections::IEnumerator^ GetEnumerator1x() 
               = System::Collections::IEnumerable::GetEnumerator {
        return GetEnumerator();
    }
};

这篇关于是否有可能重写了IEnumerable在VC ++ / CLI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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