MONO Malfunctions?

Here is a piece of code I wrote that fails in running when compiled by MONO. I checked it carefully according to the language specification I have known, and couldn't find any explanation for the failure. I didn't believe it is the final result, and turned to VS to have a try. The program unsurprisingly passed with intended result. And therefore the code is what can distinguish MONO and VS (of the version I am using). The only problem here is whether this distinction is a critical misinterpretation of the language on MONO's side or it's just that I'm using the language feature in an too unconventional and excessive way.

(The following is a simplified test program that can properly demonstrate the problem, Mar.27,2009)

using System; using System.Collections; using System.Collections.Generic; namespace QTest { class MonoGenericTest { class A { public override String ToString() { return "A"; } } class GEnumerator1<T> : IEnumerable where T : new() { T Entry = new T(); IEnumerator IEnumerable.GetEnumerator() { yield return Entry; } } class GEnumerator2<T> : IEnumerable where T : new() { IEnumerator IEnumerable.GetEnumerator() { yield return new T(); } } static void Main(String[] args) { GEnumerator1<A> ge1 = new GEnumerator1<A>(); GEnumerator2<A> ge2 = new GEnumerator2<A>(); IEnumerable[] arGe = new IEnumerable[2]{ ge1, ge2 }; for (int i = 0; i < arGe.Length; i++) { IEnumerable ge = arGe[i]; try { Console.WriteLine(": Test {0}", i); foreach (A a in ge) { Console.WriteLine(": item {0} generated", a); } } catch (Exception e) { Console.WriteLine("Error:/n{0}", e); } } } } }

The result by VisualStudio:

: Test 0
: item A generated
: Test 1
: item A generated
请按任意键继续. . .

The result of the program compiled by MONO:

: Test 0
Error:
System.TypeLoadException: “QTest.MonoGenericTest+GEnumerator1`1[T]”上的 Generi
cArguments[0] (“T”)与类型参数“T”的约束冲突。
在 QTest.MonoGenericTest.GEnumerator1`1.<IEnumerable.GetEnumerator>c__Iterato
r0.MoveNext()
在 QTest.MonoGenericTest.Main(String[] args)
: Test 1
: item A generated

As the exception indicates, the MONO runtime framework regards the type parameter passed into GEnumerator (A in this case) from as conflicting with the type constraint (specified by the clause initated with the 'where' keyword). I suspect there is some deficiencies in MONO's runtime type management (as different to C++, C#'s generic types are generally expanded at runtime, this is definitely the reason why there's no error message given out by the compiler)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值