private static void AssertNotEqual(Type x, Type y)
        {
            var eq = new TypeEqualityComparer();

            Assert.IsFalse(eq.Equals(x, y));
            Assert.IsFalse(eq.Equals(y, x));
            Assert.AreNotEqual(eq.GetHashCode(x), eq.GetHashCode(y));
        }
예제 #2
0
        public IEnumerable <TypeDefinition> GetSubTypes(TypeDefinition type)
        {
            var comparer = new TypeEqualityComparer();

            return(Types.Where(t => !comparer.Equals(t, type) &&
                               GetBaseTypes(t).Contains(type, comparer)));
        }
예제 #3
0
 static bool Equals(IMemberRefParent @class, TypeDef type)
 {
     if (@class == null || type == null)
     {
         return(false);
     }
     if (@class is TypeDef td)
     {
         return(TypeEqualityComparerInstance.Equals(td, type));
     }
     if (@class is TypeRef tr)
     {
         return(TypeEqualityComparerInstance.Equals(tr, type));
     }
     if (@class is TypeSpec ts)
     {
         var typeSig = ts.TypeSig.RemovePinnedAndModifiers();
         var tdrSig  = typeSig as TypeDefOrRefSig;
         if (tdrSig == null && typeSig is GenericInstSig gis)
         {
             tdrSig = gis.GenericType;
         }
         if (tdrSig != null)
         {
             if (tdrSig.TypeDefOrRef is TypeDef td2)
             {
                 return(TypeEqualityComparerInstance.Equals(td2, type));
             }
             if (tdrSig.TypeDefOrRef is TypeRef tr2)
             {
                 return(TypeEqualityComparerInstance.Equals(tr2, type));
             }
             return(false);
         }
         return(false);
     }
     if (@class is MethodDef md)
     {
         return(TypeEqualityComparerInstance.Equals(md.DeclaringType, type));
     }
     if (@class is ModuleRef mr)
     {
         return(type.IsGlobalModuleType && StringComparer.OrdinalIgnoreCase.Equals(mr.Name, type.Module.Name));
     }
     return(false);
 }
예제 #4
0
        public void StructuralTypeEqualityComparer_StructuralEquals()
        {
            var rt1 = RuntimeCompiler.CreateRecordType(new Dictionary <string, Type>
            {
                { "Foo", typeof(int) },
                { "Bar", typeof(string) }
            },
                                                       true);

            var rt2 = RuntimeCompiler.CreateRecordType(new Dictionary <string, Type>
            {
                { "Foo", typeof(int) },
                { "Bar", typeof(string) },
            },
                                                       true);

            var at1 = RuntimeCompiler.CreateAnonymousType(new Dictionary <string, Type>
            {
                { "Foo", typeof(int) },
                { "Bar", typeof(string) }
            },
                                                          null);

            var at2 = RuntimeCompiler.CreateAnonymousType(new Dictionary <string, Type>
            {
                { "Foo", typeof(int) },
                { "Bar", typeof(string) }
            },
                                                          null);

            var eq   = new StructuralTypeEqualityComparer();
            var orig = new TypeEqualityComparer();

            Assert.IsFalse(orig.Equals(rt1, rt2));
            Assert.IsTrue(eq.Equals(rt1, rt2));
            Assert.IsFalse(orig.Equals(at1, at2));
            Assert.IsTrue(eq.Equals(at1, at2));
        }
예제 #5
0
        public void TypeSlimExtensions_KnownTypes()
        {
            var d = new Dictionary <Type, TypeSlim>
            {
                { typeof(Action), TypeSlimExtensions.ActionType },
                { typeof(bool), TypeSlimExtensions.BooleanType },
                { typeof(Expression <>), TypeSlimExtensions.GenericExpressionType },
                { typeof(int), TypeSlimExtensions.IntegerType },
                { typeof(void), TypeSlimExtensions.VoidType },
            };

            var eqt = new TypeEqualityComparer();
            var eqs = new TypeSlimEqualityComparer();

            foreach (var kv in d)
            {
                Assert.IsTrue(eqt.Equals(kv.Key, kv.Value.ToType()));
                Assert.IsTrue(eqs.Equals(kv.Key.ToTypeSlim(), kv.Value));
            }
        }
예제 #6
0
        internal void Patch(ModuleDef module)
        {
            List <TypeDependency> owned = PatchDependency(module, typeDependencies.Concat(typeMixins.Select(m => m.Dependency)));

            typeDependencies.RemoveAll(owned.Contains);
            typeMixins.RemoveAll(m => owned.Contains(m.Dependency));
            HashSet <MethodSig> originals = new HashSet <MethodSig>();

            foreach (MethodSig original in typeDependencies.SelectMany(t => t.Originals.Select(module.ToSig)))
            {
                originals.Add(original);
            }
            foreach (KeyValuePair <MethodSignature, List <Action <MethodDef> > > method in mixins)
            {
                MethodDef methodDef = Resolve(module, method.Key);
                foreach (Action <MethodDef> action in method.Value)
                {
                    action(methodDef);
                }
            }
            PatchModule?.Invoke(module);
            TypeEqualityComparer              typeComparer        = new TypeEqualityComparer(ComparerOptions);
            SignatureEqualityComparer         comparer            = new SignatureEqualityComparer(ComparerOptions);
            Dictionary <TypeSig, TypeSig>     typeSubstitutions   = this.typeSubstitutions.GroupBy(p => module.ToSig(p.Key), p => module.ToSig(p.Value), typeComparer).Where(g => !typeComparer.Equals(g.Key, g.First())).ToDictionary(g => g.Key, Enumerable.First, typeComparer);
            Dictionary <FieldSig, FieldSig>   fieldSubstitutions  = this.fieldSubstitutions.GroupBy(p => module.ToSig(p.Key), p => module.ToSig(p.Value), comparer).Where(g => !comparer.Equals(g.Key, g.First())).ToDictionary(g => g.Key, Enumerable.First, comparer);
            Dictionary <MethodSig, MethodSig> methodSubstitutions = this.methodSubstitutions.GroupBy(p => module.ToSig(p.Key), p => module.ToSig(p.Value), comparer).Where(g => !comparer.Equals(g.Key, g.First())).ToDictionary(g => g.Key, Enumerable.First, comparer);

            foreach (TypeDef type in module.GetTypes())
            {
                foreach (FieldDef field in type.Fields)
                {
                    while (fieldSubstitutions.TryGetValue(field.FieldSig, out FieldSig newFieldSig))
                    {
                        field.FieldSig = newFieldSig;
                    }
                    field.FieldType = field.FieldType.ApplyToLeaf(typeSubstitutions.Substitute);
                }
                foreach (MethodDef method in type.Methods)
                {
                    method.MethodSig = methodSubstitutions.Substitute(method.MethodSig);
                    foreach (Parameter parameter in method.Parameters)
                    {
                        parameter.Type = parameter.Type.ApplyToLeaf(typeSubstitutions.Substitute);
                    }
                    method.ReturnType = method.ReturnType.ApplyToLeaf(typeSubstitutions.Substitute);
                }
            }
        }
 public bool CustomEqualityComparer()
 {
     return(_comparer.Equals(_actualType, ExpectedType));
 }
예제 #8
0
        public void StructuralTypeEqualityComparer_ManOrBoy2_Success()
        {
            var rtc = new RuntimeCompiler();
            var rt1 = rtc.GetNewRecordTypeBuilder();
            var rt2 = rtc.GetNewRecordTypeBuilder();
            var at1 = rtc.GetNewAnonymousTypeBuilder();
            var at2 = rtc.GetNewAnonymousTypeBuilder();

            rtc.DefineRecordType(
                rt1,
                new Dictionary <string, Type>
            {
                { "Self", rt1 },
                { "Copy", rt2 },
                { "Other", typeof(int) }
            },
                true);

            rtc.DefineRecordType(
                rt2,
                new Dictionary <string, Type>
            {
                { "Self", rt2 },
                { "Copy", rt1 },
                { "Other", typeof(int) }
            },
                true);

            rtc.DefineAnonymousType(
                at1,
                new Dictionary <string, Type>
            {
                { "Self", at1 },
                { "Copy", at2 },
                { "Other", typeof(int) }
            },
                null);

            rtc.DefineAnonymousType(
                at2,
                new Dictionary <string, Type>
            {
                { "Self", at2 },
                { "Copy", at1 },
                { "Other", typeof(int) }
            },
                null);

            var rt1c = rt1.CreateType();
            var rt2c = rt2.CreateType();
            var at1c = at1.CreateType();
            var at2c = at2.CreateType();

            var eq   = new StructuralTypeEqualityComparer();
            var orig = new TypeEqualityComparer();

            Assert.IsFalse(orig.Equals(rt1c, rt2c));
            Assert.IsTrue(eq.Equals(rt1c, rt2c));
            Assert.IsFalse(orig.Equals(at1c, at2c));
            Assert.IsTrue(eq.Equals(at1c, at2c));
        }
예제 #9
0
 public T?Resolve <T>(params IResolverParameter[] parameters) where T : class
 => _instances.FirstOrDefault(t => _typeComparer.Equals(t.Key, typeof(T))).Value as T;
 public bool CustomEqualityComparer() => _comparer.Equals(_actualType, ExpectedType);
        public void EqualsWithEqualObjects()
        {
            var comparer = new TypeEqualityComparer();

            Assert.IsTrue(comparer.Equals(typeof(string), typeof(string)));
        }
        public void EqualsWithBothObjectsNull()
        {
            var comparer = new TypeEqualityComparer();

            Assert.IsFalse(comparer.Equals(null, null));
        }
        public void EqualsWithSecondObjectNull()
        {
            var comparer = new TypeEqualityComparer();

            Assert.IsFalse(comparer.Equals(typeof(object), null));
        }
        public void EqualsWithFirstObjectNull()
        {
            var comparer = new TypeEqualityComparer();

            Assert.IsFalse(comparer.Equals(null, typeof(object)));
        }
 public bool Equals(GenericParamConstraint x, GenericParamConstraint y) => ReferenceEquals(x, y) || !(x is null) && !(y is null) && typeComparer.Equals(x.Constraint, y.Constraint);