コード例 #1
0
        public static bool TryParse <T>(string str, out T result)
        {
            if (!Introspecter.IsDuck <T, IParseable <T> >())
            {
                throw new Exception($"Type {typeof(T).FullName} does not implement all methods required by {typeof(IParseable<T>).FullName}.");
            }

            return(StaticDuckInterface <IParseable <T>, T> .Impl.TryParse(str, out result));
        }
コード例 #2
0
        public static void Main(string[] args)
        {
            // Expected Output:
            // Duck is a duck of IDuck
            // NotADuck is not a duck of IDuck
            Console.WriteLine($"Duck is {(Introspecter.IsDuck<IDuck, Duck>(new Duck()) ? "" : "not")} a duck of IDuck");
            Console.WriteLine($"NotADuck is {(Introspecter.IsDuck<IDuck, NotADuck>(new NotADuck()) ? "" : "not")} a duck of IDuck");



            var structDuck = DuckInterface <IDuck> .Duck(new StructDuck());

            var classDuck = DuckInterface <IDuck> .Duck(new Duck());

            structDuck.Quack();
            structDuck.Flap();
            classDuck.Quack();
            classDuck.Flap();

            // Expected Output:
            // InheritanceBase.M was called
            // InheritanceBase.N was called
            // InheritanceSub.M was called
            // InheritanceBase.N was called
            TestInheritance <InheritanceBase>();
            TestInheritance <InheritanceSub>();

            // Expected Output: HashSet-Count 0
            StaticInterface <ITestContainsGenericParams, TestContainsGenericParams> .Impl.MakeList <InheritanceSub>(new HashSet <InheritanceSub>());

            const int epochs     = 100;
            const int iterations = 100000;
            Stopwatch sw         = Stopwatch.StartNew();
            double    min1       = double.MaxValue;
            double    min2       = double.MaxValue;
            float     f          = (float)new Random().NextDouble();
            TypeCode  tc         = TypeCode.Boolean;

            for (int j = 0; j < epochs; ++j)
            {
                sw.Restart();
                for (int i = 0; i < iterations; ++i)
                {
                    tc = GetTypeCode(new MySubTC());
                }
                sw.Stop();

                if (sw.Elapsed.TotalMilliseconds < min1)
                {
                    min1 = sw.Elapsed.TotalMilliseconds;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                Console.Title = $"Epoch {j + 1}/{epochs}, Test 1";
            }
            for (int j = 0; j < epochs; ++j)
            {
                sw.Restart();
                for (int i = 0; i < iterations; ++i)
                {
                    tc = new MySubTC().GetTypeCode();
                }
                sw.Stop();

                if (sw.Elapsed.TotalMilliseconds < min2)
                {
                    min2 = sw.Elapsed.TotalMilliseconds;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                Console.Title = $"Epoch {j + 1}/{epochs}, Test 2";
            }

            Console.WriteLine("Generic = " + min1);
            Console.WriteLine("Non-Generic = " + min2);
            Console.WriteLine("", tc.ToString());

            Console.ReadLine();
        }