Ejemplo n.º 1
0
    private static void ValidateTypeInstanceEquality()
    {
        Console.WriteLine($"{nameof(ValidateTypeInstanceEquality)}");
        var inAsm    = EmptyType.Create();
        var otherAsm = EmptyType2.Create();

        Type inAsmInterfaceType    = inAsm.GetType().GetInterface(nameof(IEmptyType));
        Type otherAsmInterfaceType = otherAsm.GetType().GetInterface(nameof(IEmptyType));

        // Sanity checks
        Assert.True(inAsmInterfaceType == inAsmInterfaceType);
        Assert.True(inAsmInterfaceType.IsEquivalentTo(inAsmInterfaceType));
        Assert.False(inAsmInterfaceType.IsEquivalentTo(inAsm.GetType()));
        Assert.True(otherAsmInterfaceType == otherAsmInterfaceType);
        Assert.True(otherAsmInterfaceType.IsEquivalentTo(otherAsmInterfaceType));
        Assert.False(otherAsmInterfaceType.IsEquivalentTo(otherAsm.GetType()));

        // The intrinsic equality operations should fail
        Assert.False(inAsmInterfaceType == otherAsmInterfaceType);
        Assert.False(inAsmInterfaceType.Equals(otherAsmInterfaceType));
        Assert.False(otherAsmInterfaceType == inAsmInterfaceType);
        Assert.False(otherAsmInterfaceType.Equals(inAsmInterfaceType));

        // Determination of equal types requires API call
        Assert.True(inAsmInterfaceType.IsEquivalentTo(otherAsmInterfaceType));
        Assert.True(otherAsmInterfaceType.IsEquivalentTo(inAsmInterfaceType));
    }
Ejemplo n.º 2
0
    private static void TestGenericInterfaceEquivalence()
    {
        Console.WriteLine($"{nameof(TestGenericInterfaceEquivalence)}");
        var inAsm    = EmptyType.Create();
        var otherAsm = EmptyType2.Create();

        Type inAsmInterfaceType    = inAsm.GetType().GetInterface(nameof(IEmptyType));
        Type otherAsmInterfaceType = otherAsm.GetType().GetInterface(nameof(IEmptyType));

        Assert.True(typeof(IGeneric <>).MakeGenericType(inAsmInterfaceType).IsEquivalentTo(typeof(IGeneric <>).MakeGenericType(otherAsmInterfaceType)));
    }
Ejemplo n.º 3
0
    private static void TestByRefEquivalence()
    {
        Console.WriteLine($"{nameof(TestByRefEquivalence)}");
        var inAsm    = EmptyType.Create();
        var otherAsm = EmptyType2.Create();

        Type inAsmInterfaceType    = inAsm.GetType().GetInterface(nameof(IEmptyType));
        Type otherAsmInterfaceType = otherAsm.GetType().GetInterface(nameof(IEmptyType));

        Assert.IsTrue(inAsmInterfaceType.MakeByRefType().IsEquivalentTo(otherAsmInterfaceType.MakeByRefType()));
    }
Ejemplo n.º 4
0
    private static void InterfaceTypesFromDifferentAssembliesAreEquivalent()
    {
        Console.WriteLine($"{nameof(InterfaceTypesFromDifferentAssembliesAreEquivalent)}");
        var inAsm    = EmptyType.Create();
        var otherAsm = EmptyType2.Create();

        AreNotSameObject((IEmptyType)inAsm, (IEmptyType)otherAsm);

        void AreNotSameObject(IEmptyType a, IEmptyType b)
        {
            Assert.NotEqual(a, b);
        }
    }