public void GetProperMethodsReturnsPublicStaticAndPublicInstanceProperMethods()
        {
            // Fixture setup
            Func <MethodInfoElement, string> orderBy = mie => mie.MethodInfo.ToString();

            var type     = typeof(TypeWithStaticAndInstanceMembers <int>);
            var methods  = new Methods <TypeWithStaticAndInstanceMembers <int> >();
            var expected = new IReflectionElement[]
            {
                new MethodInfoElement(methods.Select(t => t.PublicMethod())),
                new MethodInfoElement(type.GetMethod("PublicStaticVoidMethod")),
                new MethodInfoElement(type.GetMethod("ToString")),
                new MethodInfoElement(type.GetMethod("Equals")),
                new MethodInfoElement(type.GetMethod("GetHashCode")),
                new MethodInfoElement(type.GetMethod("GetType")),
            };

            // Exercise system
            var actual = type.GetProperMethods();

            // Verify outcome
            AssertUnorderedElementsEqual(
                expected,
                actual,
                orderBy);

            // Fixture teardown
        }
        public void GetPublicPropertiesAndFieldsYieldsStaticAndInstanceElements()
        {
            // Fixture setup
            var type             = typeof(TypeWithStaticAndInstanceMembers <int>);
            var properties       = new Properties <TypeWithStaticAndInstanceMembers <int> >();
            var fields           = new Fields <TypeWithStaticAndInstanceMembers <int> >();
            var expectedElements = new IReflectionElement[]
            {
                new PropertyInfoElement(type.GetProperty("PublicStaticReadOnlyProperty")),
                new PropertyInfoElement(type.GetProperty("PublicStaticProperty")),
                new PropertyInfoElement(properties.Select(i => i.PublicReadOnlyProperty)),
                new PropertyInfoElement(properties.Select(i => i.PublicProperty)),
                new FieldInfoElement(fields.Select(i => i.PublicReadOnlyField)),
                new FieldInfoElement(fields.Select(i => i.PublicField)),
                new FieldInfoElement(type.GetField("PublicStaticField")),
                new FieldInfoElement(type.GetField("PublicStaticReadOnlyFieldWithDefault")),
            };

            // Exercise system
            var actualElements = type.GetPublicPropertiesAndFields();

            // Verify outcome
            Assert.Equal(expectedElements, actualElements);

            // Fixture teardown
        }
Example #3
0
        public void DoesNotVisitElementsOtherThanPropertyAndField(
            IReflectionElement element)
        {
            var sut          = new ValueCollectingVisitor(new object());
            var acceptResult = element.Accept(sut);

            Assert.Equal(sut, acceptResult);
        }
        public void AsReflectionElementReturnsCorrectResult(
            object source,
            IReflectionElement expected)
        {
            IReflectionElement actual = source.AsReflectionElement();

            Assert.Equal(expected, actual);
        }
 int IEqualityComparer <IReflectionElement> .GetHashCode(IReflectionElement obj)
 {
     if (obj == null)
     {
         throw new ArgumentNullException("obj");
     }
     return(obj
            .Accept(this.visitor)
            .Value
            .Single()
            .GetHashCode());
 }
        bool IEqualityComparer <IReflectionElement> .Equals(IReflectionElement x, IReflectionElement y)
        {
            var values = new CompositeReflectionElement(x, y)
                         .Accept(this.visitor)
                         .Value
                         .ToArray();

            var distinctValues = values.Distinct(this.comparer);

            return(values.Length == 2 &&
                   distinctValues.Count() == 1);
        }
Example #7
0
            bool IEqualityComparer <IReflectionElement> .Equals(IReflectionElement x, IReflectionElement y)
            {
                T[] values = new CompositeReflectionElement(x, y)
                             .Accept(_visitor)
                             .Value
                             .ToArray();

                IEnumerable <T> distinctValues = values.Distinct(_comparer);

                return(values.Length == 2 &&
                       distinctValues.Count() == 1);
            }
Example #8
0
 public static IEnumerable <IReflectionElement> GetChildren(this IReflectionElement item)
 {
     if (item.GetType() == typeof(AssemblyMetadata))
     {
         var x = (AssemblyMetadata)item;
         return(x.m_Namespaces);
     }
     else if (item.GetType() == typeof(NamespaceMetadata))
     {
         var x = (NamespaceMetadata)item;
         return(x.m_Types);
     }
     else if (item.GetType() == typeof(TypeMetadata))
     {
         var x = (TypeMetadata)item;
         List <IReflectionElement> children = new List <IReflectionElement>();
         children.AddRange(x.Fields);
         children.AddRange(x.Properties);
         children.AddRange(x.Methods);
         children.AddRange(x.Attributes);
         children.AddRange(x.NestedTypes);
         children.AddRange(x.ImplementedInterfaces);
         children.AddRange(x.GenericArguments);
         if (x.BaseType != null)
         {
             children.Add(x.BaseType);
         }
         return(children);
     }
     else if (item.GetType() == typeof(MethodMetadata))
     {
         var x = (MethodMetadata)item;
         List <IReflectionElement> children = new List <IReflectionElement>();
         if (x.ReturnType.Name != "Void")
         {
             children.Add(x.ReturnType);
         }
         children.AddRange(x.Parameters);
         children.AddRange(x.Attributes);
         return(children);
     }
     return(null);
 }
Example #9
0
 public static string GetDescription(this IReflectionElement item)
 {
     if (item.GetType() == typeof(AssemblyMetadata))
     {
         return((item as AssemblyMetadata).GetDescription());
     }
     else if (item.GetType() == typeof(NamespaceMetadata))
     {
         return((item as NamespaceMetadata).GetDescription());
     }
     else if (item.GetType() == typeof(TypeMetadata))
     {
         return((item as TypeMetadata).GetDescription());
     }
     else if (item.GetType() == typeof(MethodMetadata))
     {
         return((item as MethodMetadata).GetDescription());
     }
     else
     {
         throw new NotSupportedException("Extension method does not support external implementations of ReflectionElement");
     }
 }
 private IEnumerable<Assembly> GetReferences(IReflectionElement reflectionElement)
 {
     return reflectionElement.Accept(this.memberReferenceCollector).Value;
 }
        private void EnsureDostNotExposeIndirectReferences(IReflectionElement reflectionElement)
        {
            var reference = this.GetReferences(reflectionElement)
                .FirstOrDefault(r => this.IndirectReferences.Contains(r));

            if (reference == null)
                return;

            var messageFormat = @"The indirect reference should not be exposed through public API.
            Indirect reference: {0}
            API(exposing)     : {1}";

            throw new NotExposedReferenceException(
                string.Format(
                    CultureInfo.CurrentCulture,
                    messageFormat,
                    reference,
                    reflectionElement.Accept(new DisplayNameCollector()).Value.Single()));
        }