static IEnumerable <IAttribute> GetAttributes(IEntity entity, Predicate <IType> attributeTypePredicate, bool inherit) { if (!inherit) { foreach (var attr in entity.Attributes) { if (attributeTypePredicate(attr.AttributeType)) { yield return(attr); } } yield break; } ITypeDefinition typeDef = entity as ITypeDefinition; if (typeDef != null) { foreach (var baseType in typeDef.GetNonInterfaceBaseTypes().Reverse()) { ITypeDefinition baseTypeDef = baseType.GetDefinition(); if (baseTypeDef == null) { continue; } foreach (var attr in baseTypeDef.Attributes) { if (attributeTypePredicate(attr.AttributeType)) { yield return(attr); } } } yield break; } IMember member = entity as IMember; if (member != null) { HashSet <IMember> visitedMembers = new HashSet <IMember>(); do { member = member.MemberDefinition; // it's sufficient to look at the definitions if (!visitedMembers.Add(member)) { // abort if we seem to be in an infinite loop (cyclic inheritance) break; } foreach (var attr in member.Attributes) { if (attributeTypePredicate(attr.AttributeType)) { yield return(attr); } } } while (member.IsOverride && (member = InheritanceHelper.GetBaseMember(member)) != null); yield break; } throw new NotSupportedException("Unknown entity type"); }
public void DynamicAndObjectShouldBeConsideredTheSameTypeWhenMatchingSignatures() { string program = @"using System.Collections.Generic; public class Base { public virtual void M1(object p) {} public virtual void M2(List<object> p) {} public virtual object M3() { return null; } public virtual List<object> M4() { return null; } public virtual void M5(dynamic p) {} public virtual void M6(List<dynamic> p) {} public virtual dynamic M7() { return null; } public virtual List<dynamic> M8() { return null; } } public class Derived : Base { public override void M1(dynamic p) {} public override void M2(List<dynamic> p) {} public override dynamic M3() { return null; } public override List<dynamic> M4() { return null; } public override void M5(object p) {} public override void M6(List<object> p) {} public override object M7() { return null; } public override List<object> M8() { return null; } }"; var unresolvedFile = new CSharpParser().Parse(program, "program.cs").ToTypeSystem(); var compilation = new CSharpProjectContent().AddAssemblyReferences(CecilLoaderTests.Mscorlib).AddOrUpdateFiles(unresolvedFile).CreateCompilation(); var dtype = (ITypeDefinition)ReflectionHelper.ParseReflectionName("Derived").Resolve(compilation); var btype = (ITypeDefinition)ReflectionHelper.ParseReflectionName("Base").Resolve(compilation); foreach (var name in new[] { "M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8" }) { Assert.That(InheritanceHelper.GetBaseMember(dtype.Methods.Single(m => m.Name == name)), Is.EqualTo(btype.Methods.Single(m => m.Name == name)), name + " does not match"); } foreach (var name in new[] { "M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8" }) { Assert.That(InheritanceHelper.GetDerivedMember(btype.Methods.Single(m => m.Name == name), dtype), Is.EqualTo(dtype.Methods.Single(m => m.Name == name)), name + " does not match"); } }
public void PrivateMemberDoesNotDisruptSearchForBaseMember() { string program = @"using System.Collections.Generic; public class Base { public virtual int M(); } public class Middle : Base { private int M(); } public class Derived : Middle { public override void M() {} }"; var unresolvedFile = new CSharpParser().Parse(program, "program.cs").ToTypeSystem(); var compilation = new CSharpProjectContent().AddAssemblyReferences(CecilLoaderTests.Mscorlib).AddOrUpdateFiles(unresolvedFile).CreateCompilation(); var dtype = (ITypeDefinition)ReflectionHelper.ParseReflectionName("Derived").Resolve(compilation); var btype = (ITypeDefinition)ReflectionHelper.ParseReflectionName("Base").Resolve(compilation); Assert.AreEqual(btype.Methods.Single(m => m.Name == "M"), InheritanceHelper.GetBaseMember(dtype.Methods.Single(m => m.Name == "M"))); }