Beispiel #1
0
        public void InitializeTest()
        {
            var type    = typeof(string);
            var builder = new TypeNameBuilder(type);

            Assert.Equal(builder.AssemblyName, type.Assembly().FullName);
            Assert.Equal(type, Type.GetType(builder.ToString()));

            builder = new TypeNameBuilder(type, assembly => assembly.GetName().Name);
            Assert.Equal(builder.AssemblyName, type.Assembly().GetName().Name);
            Assert.Equal(type, Type.GetType(builder.ToString()));

            type    = typeof(string[]);
            builder = new TypeNameBuilder(type);
            Assert.Equal(type, Type.GetType(builder.ToString()));

            type    = typeof(string[][, ]);
            builder = new TypeNameBuilder(type);
            Assert.Equal(type, Type.GetType(builder.ToString()));

            type    = typeof(Foo <,>);
            builder = new TypeNameBuilder(type);
            Assert.Equal(type, Type.GetType(builder.ToString()));

            type    = typeof(Foo <int, string>);
            builder = new TypeNameBuilder(type);
            Assert.Equal(type, Type.GetType(builder.ToString()));

            type    = typeof(Foo <int, string>[][, ][, , ]);
            builder = new TypeNameBuilder(type);
            Assert.Equal(type, Type.GetType(builder.ToString()));

            type    = typeof(Foo <,> .Bar <>);
            builder = new TypeNameBuilder(type);
            Assert.Equal(type, Type.GetType(builder.ToString()));

            type    = typeof(Foo <int, string> .Bar <bool>);
            builder = new TypeNameBuilder(type);
            Assert.Equal(type, Type.GetType(builder.ToString()));

            type    = typeof(Foo <int, string> .Bar <bool>[]);
            builder = new TypeNameBuilder(type);
            Assert.Equal(type, Type.GetType(builder.ToString()));

            type    = typeof(Foo <int, List <string> > .Bar <bool>);
            builder = new TypeNameBuilder(type);
            Assert.Equal(type, Type.GetType(builder.ToString()));

            type    = typeof(Foo <(int, string), List <string>[][, ]> .Bar <bool>[]);
            builder = new TypeNameBuilder(type);
            Assert.Equal(type, Type.GetType(builder.ToString()));
        }
Beispiel #2
0
        public void GenerateTest()
        {
            #region simple

            var builder = new TypeNameBuilder
            {
                BaseName     = "System.String",
                AssemblyName = "mscorlib"
            };
            Assert.Equal("System.String, mscorlib", builder.ToString());

            #endregion

            #region generic

            builder = new TypeNameBuilder
            {
                AssemblyName     = "mscorlib",
                Namespace        = "System.Collections.Generic",
                BaseName         = "List",
                GenericArguments =
                {
                    new TypeNameBuilder
                    {
                        AssemblyName = "mscorlib",
                        Namespace    = "System",
                        BaseName     = "Int32",
                    },
                }
            };
            Assert.Equal("System.Collections.Generic.List`1[[System.Int32, mscorlib]], mscorlib", builder.ToString());

            builder.GenericArguments[0] = null;

            Assert.Equal("System.Collections.Generic.List`1, mscorlib", builder.ToString());

            #endregion

            #region array

            builder = new TypeNameBuilder
            {
                AssemblyName     = "mscorlib",
                Namespace        = "System.Collections.Generic",
                BaseName         = "List",
                GenericArguments =
                {
                    new TypeNameBuilder
                    {
                        AssemblyName    = "mscorlib",
                        Namespace       = "System",
                        BaseName        = "Int32",
                        ArrayDimensions ={                       1 }
                    },
                },
                ArrayDimensions = { 2, 3 }
            };
            Assert.Equal("System.Collections.Generic.List`1[[System.Int32[], mscorlib]][,][,,], mscorlib", builder.ToString());
            Assert.Equal("List`1", builder.GetName());

            builder.GenericArguments[0] = null;

            Assert.Equal("System.Collections.Generic.List`1[,][,,]", builder.GetFullName());

            builder.BaseName = "";

            Assert.Equal("System.Collections.Generic.?`1[,][,,]", builder.GetFullName());

            #endregion

            #region nested

            builder = new TypeNameBuilder
            {
                BaseName = "X",
                Nested   = new TypeName {
                    BaseName = "Y"
                },
                Namespace = "NS"
            };
            Assert.Equal("NS.X+Y", builder.ToString());

            builder.ArrayDimensions.Add(1);
            Assert.Equal("NS.X+Y[]", builder.ToString());

            builder.Nested.GenericArguments.Add(new TypeNameBuilder {
                BaseName = "T"
            });
            Assert.Equal("NS.X+Y`1[[T]][]", builder.ToString());

            #endregion
            #region complex

            builder = new TypeNameBuilder
            {
                BaseName         = "System.Collections.Generic.Dictionary",
                AssemblyName     = "mscorlib",
                GenericArguments =
                {
                    new TypeNameBuilder
                    {
                        BaseName     = "System.Int32",
                        AssemblyName = "mscorlib",
                    },
                    new TypeNameBuilder
                    {
                        BaseName         = "System.Collections.Generic.List",
                        AssemblyName     = "mscorlib",
                        GenericArguments =
                        {
                            new TypeNameBuilder
                            {
                                BaseName        = "System.String",
                                AssemblyName    = "mscorlib",
                                ArrayDimensions ={                       1, 2 }
                            },
                        }
                    },
                },
                Nested = new TypeName {
                    BaseName = "Enumerator"
                }
            };
            Assert.Equal("System.Collections.Generic.Dictionary`2+Enumerator[[System.Int32, mscorlib],[System.Collections.Generic.List`1[[System.String[][,], mscorlib]], mscorlib]], mscorlib", builder.ToString());

            #endregion
        }