static string PrettyPrintParameterList(MethodInfo mi)
 {
     return("(" + string.Join(
                ", ",
                mi.GetParameters().Select(
                    pi =>
                    ObjectToCode.GetCSharpFriendlyTypeName(pi.ParameterType) + " " + pi.Name)) + ")");
 }
        [Test] //Has always been broken
        public void ConstructedSingleDimOfMultiDimArray()
        {
            // ReSharper disable once SuggestUseVarKeywordEvident
            // ReSharper disable once RedundantArrayCreationExpression
            object[][,] v = new[] { new object[2, 3] };

            Assert.AreEqual("object[][,]", ObjectToCode.GetCSharpFriendlyTypeName(v.GetType()));
        }
 static object PrettyPrintField(FieldInfo fi)
 {
     return("    "
            + (fi.IsLiteral ? "const " : (fi.IsStatic ? "static " : "") + (fi.IsInitOnly ? "readonly " : ""))
            + ObjectToCode.GetCSharpFriendlyTypeName(fi.FieldType)
            + " " + fi.Name
            + (fi.IsLiteral ? " = " + ObjectToCode.ComplexObjectToPseudoCode(fi.GetRawConstantValue()) : "")
            );
 }
        static string PrettyPrintMethod(MethodInfo mi)
        {
            var fakeTarget = mi.IsStatic ? "TYPE" : "inst";

            return("    " + ObjectToCode.GetCSharpFriendlyTypeName(mi.ReturnType) + " " + fakeTarget +
                   "." + mi.Name
                   + PrettyPrintGenericArguments(mi)
                   + PrettyPrintParameterList(mi));
        }
        static string PrettyPrintTypeHeader(Type type)
        {
            var prefix = type.IsEnum ? "enum" : type.IsValueType ? "struct" : type.IsInterface ? "interface" : "class";

            var baseType = type.BaseType == typeof(object) ? null : type.BaseType;
            var allInterfaces = type.GetInterfaces();
            var interfaces = baseType == null ? allInterfaces : allInterfaces.Except(baseType.GetInterfaces());
            var inheritanceTypes = new[] { baseType }.OfType <Type>().Concat(interfaces);
            var suffix = !inheritanceTypes.Any() || type.IsEnum ? "" : " : " + string.Join(", ", inheritanceTypes.Select(ObjectToCode.GetCSharpFriendlyTypeName));

            var name = ObjectToCode.GetCSharpFriendlyTypeName(type);

            return(prefix + " " + name + suffix);
        }
 public void SupportsBuiltInCases()
 {
     Assert.AreEqual("object", ObjectToCode.GetCSharpFriendlyTypeName(typeof(object)));
     Assert.AreEqual("string", ObjectToCode.GetCSharpFriendlyTypeName(typeof(string)));
     Assert.AreEqual("char", ObjectToCode.GetCSharpFriendlyTypeName(typeof(char)));
     Assert.AreEqual("byte", ObjectToCode.GetCSharpFriendlyTypeName(typeof(byte)));
     Assert.AreEqual("sbyte", ObjectToCode.GetCSharpFriendlyTypeName(typeof(sbyte)));
     Assert.AreEqual("short", ObjectToCode.GetCSharpFriendlyTypeName(typeof(short)));
     Assert.AreEqual("ushort", ObjectToCode.GetCSharpFriendlyTypeName(typeof(ushort)));
     Assert.AreEqual("int", ObjectToCode.GetCSharpFriendlyTypeName(typeof(int)));
     Assert.AreEqual("uint", ObjectToCode.GetCSharpFriendlyTypeName(typeof(uint)));
     Assert.AreEqual("long", ObjectToCode.GetCSharpFriendlyTypeName(typeof(long)));
     Assert.AreEqual("ulong", ObjectToCode.GetCSharpFriendlyTypeName(typeof(ulong)));
     Assert.AreEqual("void", ObjectToCode.GetCSharpFriendlyTypeName(typeof(void)));
 }
예제 #7
0
        static HashAnalysisResult ProcessList <T>(T[] objs)
        {
            string name = ObjectToCode.GetCSharpFriendlyTypeName(typeof(T));

            if (name.StartsWith("Tuple"))
            {
                name = "Tuple";
            }
            else if (name.Contains("AnonymousType"))
            {
                name = "Anonymous Type";
            }
            var collisions = AnalyzeHashCollisions(objs);

            return(new HashAnalysisResult {
                Name = name,
                Collisions = collisions,
                GetHashCodeNS = TimeInNS(() => {
                    foreach (var inst in objs)
                    {
                        inst.GetHashCode();
                    }
                }) / objs.Length,
                EqualsNS = TimeInNS(() => {
                    for (int i = 0; i < objs.Length; i++)
                    {
                        objs[i].Equals(objs[i]);
                        objs[i].Equals(objs[(i + 1) % objs.Length]);
                    }
                }) / objs.Length / 2,
                DistinctCountNS = collisions.Rate > 0.99 ? double.NaN : TimeInNS(() => {
                    objs.Distinct().Count();
                }) / objs.Length,
                DictionaryNS = collisions.Rate > 0.99 ? double.NaN : TimeInNS(() => {
                    int idx = 0;
                    objs.ToDictionary(o => o, _ => idx++);
                }) / objs.Length,
            });
        }
 [Test] //TODO:Regression!
 public void UnboundGenericList()
 {
     Assert.AreEqual("List<T>", ObjectToCode.GetCSharpFriendlyTypeName(typeof(List <>)));
 }
 public void GenericArgumentTypes()
 {
     Assert.AreEqual("Func<Z>", ObjectToCode.GetCSharpFriendlyTypeName(typeof(Outer <,> .Nested <>).GetMethod("Method").GetParameters()[0].ParameterType));
 }
 public void RussianDolls()
 {
     Assert.AreEqual("Tuple<List<int>, Tuple<List<string>>>", ObjectToCode.GetCSharpFriendlyTypeName(typeof(Tuple <List <int>, Tuple <List <string> > >)));
 }
 public void NestedClasses()
 {
     Assert.AreEqual("Outer<string, int>.Nested<DateTime>", ObjectToCode.GetCSharpFriendlyTypeName(typeof(Outer <string, int> .Nested <DateTime>)));
 }
 public void ArrayGenericsMessyMix()
 {
     Assert.AreEqual("List<Tuple<int[], string[,]>[][]>[]", ObjectToCode.GetCSharpFriendlyTypeName(typeof(List <Tuple <int[], string[, ]>[][]>[])));
 }
 [Test] //TODO:Regression!
 public void UnboundNested()
 {
     Assert.AreEqual("Outer<X, Y>.Nested<Z>", ObjectToCode.GetCSharpFriendlyTypeName(typeof(Outer <,> .Nested <>)));
 }
 [Test] //Has always been broken
 public void SingleDimOfMultiDimArray()
 {
     Assert.AreEqual("object[][,]", ObjectToCode.GetCSharpFriendlyTypeName(typeof(object[][, ])));
 }
 public void MultiDimArray()
 {
     Assert.AreEqual("string[,,]", ObjectToCode.GetCSharpFriendlyTypeName(typeof(string[, , ])));
 }
 public void GenericList()
 {
     Assert.AreEqual("List<DateTime>", ObjectToCode.GetCSharpFriendlyTypeName(typeof(List <DateTime>)));
 }
 public void IntArray()
 {
     Assert.AreEqual("int[]", ObjectToCode.GetCSharpFriendlyTypeName(typeof(int[])));
 }
 public void SupportsSimpleExamples()
 {
     Assert.AreEqual("DateTime", ObjectToCode.GetCSharpFriendlyTypeName(typeof(DateTime)));
     Assert.AreEqual("Regex", ObjectToCode.GetCSharpFriendlyTypeName(typeof(Regex)));
     Assert.AreEqual("ExpressionToCode", ObjectToCode.GetCSharpFriendlyTypeName(typeof(ExpressionToCode)));
 }