Esempio n. 1
0
    public static void TestClosedStaticDelegate()
    {
        TestClass foo = new TestClass();

        foo.structField.o1 = new object();
        foo.structField.o2 = new object();
        StructReturningDelegate testDelegate = foo.TestFunc;
        TestStruct returnedStruct            = testDelegate();

        Assert.True(RuntimeHelpers.ReferenceEquals(foo.structField.o1, returnedStruct.o1));
        Assert.True(RuntimeHelpers.ReferenceEquals(foo.structField.o2, returnedStruct.o2));
    }
Esempio n. 2
0
        public void WeakReference_NonGeneric()
        {
            object        o1 = new char[10];
            WeakReference w  = new WeakReference(o1);

            VerifyStillAlive(w);
            Assert.IsTrue(RuntimeHelpers.ReferenceEquals(o1, w.Target));
            Assert.IsFalse(w.TrackResurrection);
            GC.KeepAlive(o1);

            object o2 = new char[100];

            w.Target = o2;
            VerifyStillAlive(w);
            Assert.IsTrue(RuntimeHelpers.ReferenceEquals(o2, w.Target));
            GC.KeepAlive(o2);

            Latch l = new Latch();

            w = MakeWeakReference(() => new C(l), null);
            GC.Collect();
            VerifyIsDead(w);

            // WARN: Compact Framework does not support to track resurrection
#if !WindowsCE
            l = new Latch();
            w = MakeWeakReference(() => new ResurrectingC(l), true);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            if (!l.FinalizerRan)
            {
                Console.WriteLine("Attempted GC but could not force test object to finalize. Test skipped.");
            }
            else
            {
                VerifyStillAlive(w);
            }

            l = new Latch();
            w = MakeWeakReference(() => new C(l), true);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            if (!l.FinalizerRan)
            {
                Console.WriteLine("Attempted GC but could not force test object to finalize. Test skipped.");
            }
            else
            {
                VerifyIsDead(w);
            }
#endif
        }
        public static void NonGeneric()
        {
            object        o1 = new char[10];
            WeakReference w  = new WeakReference(o1);

            VerifyStillAlive(w);
            Assert.True(RuntimeHelpers.ReferenceEquals(o1, w.Target));
            Assert.False(w.TrackResurrection);
            GC.KeepAlive(o1);

            object o2 = new char[100];

            w.Target = o2;
            VerifyStillAlive(w);
            Assert.True(RuntimeHelpers.ReferenceEquals(o2, w.Target));
            GC.KeepAlive(o2);

            Latch l = new Latch();

            w = MakeWeakReference(() => new C(l));
            GC.Collect();
            VerifyIsDead(w);

            l = new Latch();
            w = MakeWeakReference(() => new ResurrectingC(l), true);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            if (!l.FinalizerRan)
            {
                Console.WriteLine("Attempted GC but could not force test object to finalize. Test skipped.");
            }
            else
            {
                VerifyStillAlive(w);
            }

            l = new Latch();
            w = MakeWeakReference(() => new C(l), true);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            if (!l.FinalizerRan)
            {
                Console.WriteLine("Attempted GC but could not force test object to finalize. Test skipped.");
            }
            else
            {
                VerifyIsDead(w);
            }
        }
Esempio n. 4
0
    public static void Equals()
    {
        C    c  = new C();
        DFoo d1 = c.Foo;
        DFoo d2 = c.Foo;

        Assert.False(RuntimeHelpers.ReferenceEquals(d1, d2));
        bool b;

        b = d1.Equals(d2);
        Assert.True(b);
        Assert.True(d1 == d2);
        Assert.False(d1 != d2);

        d1 = c.Foo;
        d2 = c.Goo;
        b  = d1.Equals(d2);
        Assert.False(b);
        Assert.False(d1 == d2);
        Assert.True(d1 != d2);

        d1 = new C().Foo;
        d2 = new C().Foo;
        b  = d1.Equals(d2);
        Assert.False(b);
        Assert.False(d1 == d2);
        Assert.True(d1 != d2);

        DGoo dgoo = c.Foo;

        d1 = c.Foo;
        b  = d1.Equals(dgoo);
        Assert.False(b);

        int h  = d1.GetHashCode();
        int h2 = d1.GetHashCode();

        Assert.Equal(h, h2);
    }
Esempio n. 5
0
    public static void TestDynamicInvoke()
    {
        A a1 = new A();
        A a2 = new A();
        B b1 = new B();
        B b2 = new B();

        DynamicInvokeDelegate testDelegate = DynamicInvokeTestFunction;

        // Check that the delegate behaves as expected
        A refParam    = b2;
        B outParam    = null;
        A returnValue = testDelegate(a1, b1, ref refParam, out outParam);

        Assert.True(RuntimeHelpers.ReferenceEquals(returnValue, a1));
        Assert.True(RuntimeHelpers.ReferenceEquals(refParam, b1));
        Assert.True(RuntimeHelpers.ReferenceEquals(outParam, b2));

        // Check dynamic invoke behavior
        object[] parameters = new object[4];
        parameters[0] = a1;
        parameters[1] = b1;
        parameters[2] = b2;
        parameters[3] = null;

        object retVal = testDelegate.DynamicInvoke(parameters);

        Assert.True(RuntimeHelpers.ReferenceEquals(retVal, a1));
        Assert.True(RuntimeHelpers.ReferenceEquals(parameters[2], b1));
        Assert.True(RuntimeHelpers.ReferenceEquals(parameters[3], b2));

        // Check invoke on a delegate that takes no parameters.
        Action emptyDelegate = EmptyFunc;

        emptyDelegate.DynamicInvoke(new object[] { });
        emptyDelegate.DynamicInvoke(null);
    }
Esempio n. 6
0
 public bool Equals(Vessel foo1, Vessel foo2)
 {
     return(RuntimeHelpers.ReferenceEquals(foo1, foo2));
 }