为什么CodeTypeReference使用泛型参数截断我的类型?

时间:2018-01-04 19:28:13

标签: c# generics codedom

我试图在CodeDom中定义类型"foo<a,b,c,d,e>"的基类,但它出现为"foo<a"

以下是一个示例测试:T1和T2通过,但T3和T4失败。

T3: Expected string length 14 but was 5. Strings differ at index 5.
  Expected: "foo<a,b,c,d,e>"
  But was:  "foo<a"
  ----------------^

-

using System.CodeDom;
using NUnit.Framework;

namespace Test
{
    [TestFixture]
    public class Strange
    {
        [Test]
        public void T1()
        {
            var name = "foo<a,b,c>";
            var codeTypeParameter1 = new CodeTypeParameter(name);
            var codeTypeReference1 = new CodeTypeReference(codeTypeParameter1);
            Assert.AreEqual(codeTypeParameter1.Name, name);
            Assert.AreEqual(name, codeTypeReference1.BaseType);
        }

        [Test]
        public void T2()
        {
            var name = "foo<a,b,c,d>";
            var codeTypeParameter1 = new CodeTypeParameter(name);
            var codeTypeReference1 = new CodeTypeReference(codeTypeParameter1);
            Assert.AreEqual(codeTypeParameter1.Name, name);
            Assert.AreEqual(name, codeTypeReference1.BaseType);
        }

        [Test]
        public void T3()
        {
            var name = "foo<a,b,c,d,e>";
            var codeTypeParameter1 = new CodeTypeParameter(name);
            var codeTypeReference1 = new CodeTypeReference(codeTypeParameter1);
            Assert.AreEqual(codeTypeParameter1.Name, name);
            Assert.AreEqual(name, codeTypeReference1.BaseType);
        }  

        [Test]
        public void T4()
        {
            var name = "foo<a,b,c,d,e,f>";
            var codeTypeParameter1 = new CodeTypeParameter(name);
            var codeTypeReference1 = new CodeTypeReference(name);
            Assert.AreEqual(codeTypeParameter1.Name, name);
            Assert.AreEqual(name, codeTypeReference1.BaseType);
        }
    }
} 

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。问题是为什么会发生(我不知道),但是我能够通过使用显式代码类型引用来找到解决方法。即

var type1 = new CodeTypeReference("Type1");
var type2 = new CodeTypeReference("Type2");
var genericType = new CodeTypeReference("GenericType");
genericType .TypeArguments.Add(tupleType);
genericType .TypeArguments.Add(returnType);