Exemple #1
0
        public No24_NotCreateGenericMethodSpecializeInBaseClassOrInterface()
        {
            //目的:
            //①利用者が混乱する挙動を避ける

            //概要:
            //--------------------------------------------------------------------------------------
            //ジェネリクスメソッドを先に公開し、それに特定の親クラスやインタフェースに特化した処理をさせたいという目的で
            //もとのジェネリクスメソッドをオーバーロードする形でメソッドを実装してはならない
            //コンパイラはジェネリクスメソッドを優先して採用するため、意識して親クラスやインタフェースにパースしないと
            //オーバーロードメソッドを使用することができない。これは利用者に対して混乱を招く
            //どうしてもやりたいなら、親クラス・インタフェース・継承するクラス・実装するクラス全てに対してオーバーロードメソッドを用意する
            //そうすれば厳密に型一致するのでオーバーロードメソッドを利用できる

            //実際の振る舞い
            var d = new MyDerived();
            var a = new AnotherType();

            //②が実行される
            GenericsAndOverLoad.WriteMessage(d);
            GenericsAndOverLoad.WriteMessage(a);
            //③が実行される
            GenericsAndOverLoad.WriteMessage((IMessageWriter)d);
            GenericsAndOverLoad.WriteMessage((IMessageWriter)a);
            //①が実行される
            GenericsAndOverLoad.WriteMessage((MyBase)d);

            //厳密な型一致が優先され、ジェネリクスメソッドが優先される
        }
    static void Main()
    {
        LastType    lt    = new LastType();
        SomeType    st    = new SomeType();
        AnotherType atype = new AnotherType();

        st.myInt         = 7;
        atype.myString   = "BOB";
        atype.mySomeType = st;
        lt.mySomeType    = st;
        lt.myAnotherType = atype;

        string xmlOutput = YourAwesomeFunction(lt);
    }
Exemple #3
0
        public void DestructionTestModelInitialisation()
        {
            // we run this a few (100) times because threading isn't reliable ;p
            // each time (on a fresh model), we prepare a slew (20) threads - each eager to serialize one of two available types.
            // They all wait on a single gate, and then the fight begins! We track any failures, and Join all the threads
            // back together.
            var a = new ModelWithNonTrivialProperties()
            {
                J = new Dictionary<string, int> { { "abc", 123 } },
                D = new byte[] { 0, 1, 2, 3, 4 },
                G = new List<decimal> { 1, 2, 3, 4, 5 },
                H = new Dictionary<int, AnotherType> { { 1, new AnotherType { A = 456 } } },
                I = new AnotherType { B = "def" }
            };
            var b = new AnotherType() { A = 123 };
            for (int i = 0; i < 100; i++)
            {
                ManualResetEvent allGo = new ManualResetEvent(false);
                var model = TypeModel.Create();
                model.AutoCompile = true;
                object starter = new object();
                int waiting = 20;
                int failures = 0;
                Exception firstException = null;
                Thread[] threads = new Thread[20];
                for (int j = 0; j < 10; j++)
                {
                    threads[2 * j] = new Thread(() =>
                    {
                        try
                        {
                            if (Interlocked.Decrement(ref waiting) == 0) allGo.Set();
                            allGo.WaitOne();
                            model.Serialize(Stream.Null, a);
                        }
                        catch (Exception ex)
                        {
                            Interlocked.CompareExchange(ref firstException, ex, null);
                            Interlocked.Increment(ref failures);
                        }
                    });
                    threads[(2 * j) + 1] = new Thread(() =>
                    {
                        try
                        {
                            if (Interlocked.Decrement(ref waiting) == 0) allGo.Set();
                            allGo.WaitOne();
                            model.Serialize(Stream.Null, b);
                        }
                        catch (Exception ex)
                        {
                            Interlocked.CompareExchange(ref firstException, ex, null);
                            Interlocked.Increment(ref failures);
                        }
                    });
                }

                for (int j = 0; j < threads.Length; j++) threads[j].Start();
                for (int j = 0; j < threads.Length; j++) threads[j].Join();

                Assert.IsNull(firstException);
                Assert.AreEqual(0, Interlocked.CompareExchange(ref failures, 0, 0));

            }
        }
Exemple #4
0
        public void DestructionTestModelInitialisation()
        {
            // we run this a few (100) times because threading isn't reliable ;p
            // each time (on a fresh model), we prepare a slew (20) threads - each eager to serialize one of two available types.
            // They all wait on a single gate, and then the fight begins! We track any failures, and Join all the threads
            // back together.
            var a = new ModelWithNonTrivialProperties()
            {
                J = new Dictionary <string, int> {
                    { "abc", 123 }
                },
                D = new byte[] { 0, 1, 2, 3, 4 },
                G = new List <decimal> {
                    1, 2, 3, 4, 5
                },
                H = new Dictionary <int, AnotherType> {
                    { 1, new AnotherType {
                          A = 456
                      } }
                },
                I = new AnotherType {
                    B = "def"
                }
            };
            var b = new AnotherType()
            {
                A = 123
            };

            for (int i = 0; i < 100; i++)
            {
                ManualResetEvent allGo = new ManualResetEvent(false);
                var model = RuntimeTypeModel.Create();
                model.AutoCompile = true;
                object    starter        = new object();
                int       waiting        = 20;
                int       failures       = 0;
                Exception firstException = null;
                Thread[]  threads        = new Thread[20];
                for (int j = 0; j < 10; j++)
                {
                    threads[2 * j] = new Thread(() =>
                    {
                        try
                        {
                            if (Interlocked.Decrement(ref waiting) == 0)
                            {
                                allGo.Set();
                            }
                            allGo.WaitOne();
                            model.Serialize(Stream.Null, a);
                        }
                        catch (Exception ex)
                        {
                            Interlocked.CompareExchange(ref firstException, ex, null);
                            Interlocked.Increment(ref failures);
                        }
                    });
                    threads[(2 * j) + 1] = new Thread(() =>
                    {
                        try
                        {
                            if (Interlocked.Decrement(ref waiting) == 0)
                            {
                                allGo.Set();
                            }
                            allGo.WaitOne();
                            model.Serialize(Stream.Null, b);
                        }
                        catch (Exception ex)
                        {
                            Interlocked.CompareExchange(ref firstException, ex, null);
                            Interlocked.Increment(ref failures);
                        }
                    });
                }

                for (int j = 0; j < threads.Length; j++)
                {
                    threads[j].Start();
                }
                for (int j = 0; j < threads.Length; j++)
                {
                    threads[j].Join();
                }

                Assert.Null(firstException);
                Assert.Equal(0, Interlocked.CompareExchange(ref failures, 0, 0));
            }
        }
Exemple #5
0
 public MyDataClass(string myString, int, myNumber, AnotherType myObject)
 {
     StringData = myString;
     NumberData = myNumber;
     ObjectData = myObject;
 }