public void ExtendedType_GetMethod_CreatesConcreteMethods()
        {
            // Get the open type.
            ExtendedType openType = ExtendedType.Get(typeof(ComplexOverloads <>));

            Assert.IsTrue(openType.Type.ContainsGenericParameters);

            // Note this not only creates a concrete method by setting the method type param to Guid
            // It also has to make the enclosing type concrete by changing it to ComplexOverloads<bool> as
            // the first parameter uses the type's generic parameter.
            //
            // It actually matches: public void A<T1>(T a, ref T1 b) {}
            Method concreteMethod = openType.GetMethod(
                "A",
                1,
                typeof(bool),
                typeof(Guid).MakeByRefType(),
                TypeSearch.Void);

            Assert.IsNotNull(concreteMethod);
            Assert.IsFalse(concreteMethod.Info.ContainsGenericParameters);
            Assert.IsFalse(concreteMethod.ExtendedType.Type.ContainsGenericParameters);
            // The GetMethod
            Assert.AreNotSame(openType, concreteMethod.ExtendedType);
        }
Esempio n. 2
0
        public void Test_ReflectorStaticConstructor()
        {
            int       aCount  = 0;
            int       tCount  = 0;
            int       aeCount = 0;
            int       tlCount = 0;
            int       nsCount = 0;
            int       rCount  = 0;
            int       mCount  = 0;
            Stopwatch s       = new Stopwatch();

            s.Start();
            // Get every loaded assembly - this will include the framework, so lot's of types to test with!
            foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
            {
                aCount++;
                foreach (Type t in a.GetTypes().Where(t => !t.IsGenericTypeDefinition))
                {
                    Assert.IsNotNull(t);

                    // Make generic type
                    try
                    {
                        ExtendedType et = ExtendedType.Get(t);
                        Assert.IsNotNull(et);
                        Assert.IsNotNull(et.Methods);
                        mCount += et.Methods.Count();
                        rCount++;
                    }
                    catch (NotSupportedException)
                    {
                        nsCount++;
                    }
                    catch (ArgumentException)
                    {
                        // Not all types can be a generic argument (e.g. System.Void)
                        aeCount++;
                    }
                    catch (TypeLoadException)
                    {
                        // Not all types can be loaded
                        tlCount++;
                    }
                }
            }
            s.Stop();
            Trace.WriteLine(
                s.ToString(
                    "Reflecting '{0}' of '{1}' types in '{2}' assemblies [{3} type load errors, {4} argument exceptions, {5} not supported] - {6} methods found.",
                    rCount,
                    tCount,
                    aCount,
                    tlCount,
                    aeCount,
                    nsCount,
                    mCount));
        }
Esempio n. 3
0
        static LogContextTest()
        {
            // Set up access to private fields in LogContext.
            ExtendedType et = ExtendedType.Get(typeof(LogContext));

            _keyReservations =
                et.Fields.Single(f => f.Info.IsStatic && f.Info.IsInitOnly && f.Info.Name == "_keyReservations")
                .Getter <Dictionary <string, Guid> >()();
            _prefixReservations =
                et.Fields.Single(f => f.Info.IsStatic && f.Info.IsInitOnly && f.Info.Name == "_prefixReservations")
                .Getter <Dictionary <string, Guid> >()();
        }
        public void ExtendedType_CanGetConcreteMethods()
        {
            // Get the open type.
            ExtendedType openType = ExtendedType.Get(typeof(ComplexOverloads <>));

            Assert.IsTrue(openType.Type.ContainsGenericParameters);

            // Close the type
            ExtendedType closedType = openType.CloseType(typeof(int));

            Assert.IsNotNull(closedType);
            Assert.IsFalse(closedType.Type.ContainsGenericParameters);
            Assert.AreSame(openType.Type, closedType.Type.GetGenericTypeDefinition());

            // This time we have a concrete type.
            Method method = closedType.GetMethod("A", typeof(int));

            Assert.IsNotNull(method);
            // This method has no generic arguments and the type is concrete.
            Assert.IsFalse(method.Info.ContainsGenericParameters);

            Method method2 = closedType.GetMethod("A", 1, typeof(string), TypeSearch.Void);

            Assert.IsNotNull(method2);
            // This method has generic arguments and so it does contain generic parameters even though type is concrete.
            Assert.IsTrue(method2.Info.ContainsGenericParameters);

            // We can use the CloseMethod overload to close the method.
            Method method3 = method2.Close(typeof(int));

            Assert.IsNotNull(method3);
            // We haven't closed the extended type.
            Assert.AreSame(method2.ExtendedType, method3.ExtendedType);
            Assert.IsFalse(method3.ExtendedType.Type.ContainsGenericParameters);
            Assert.IsFalse(method3.Info.ContainsGenericParameters);

            // Finally we get the open type's open method.
            Method method4 = openType.GetMethod("A", 1, typeof(string), TypeSearch.Void);

            Assert.IsNotNull(method4);
            Assert.IsTrue(method4.Info.ContainsGenericParameters);
        }
Esempio n. 5
0
        public Method Close([NotNull] Type[] typeClosures, [NotNull] Type[] signatureClosures)
        {
            if (typeClosures == null)
            {
                throw new ArgumentNullException("typeClosures");
            }
            if (signatureClosures == null)
            {
                throw new ArgumentNullException("signatureClosures");
            }

            Debug.Assert(_genericArguments.Value != null);

            // Check input arrays are valid.
            if ((typeClosures.Length != ExtendedType.GenericArguments.Count()) ||
                (signatureClosures.Length != _genericArguments.Value.Count()))
            {
                return(null);
            }

            // If we have any type closures then we need to close the type and look for the method on there.
            if (typeClosures.Any(t => t != null))
            {
                // Close type
                ExtendedType et = ExtendedType.CloseType(typeClosures);

                // Check closure succeeded.
                if (et == null)
                {
                    return(null);
                }

                // Create new search.
                Debug.Assert(_parameters.Value != null);
                int          pCount               = _parameters.Value.Length;
                TypeSearch[] searchTypes          = new TypeSearch[pCount + 1];
                Type[]       typeGenericArguments = et.GenericArguments.Select(g => g.Type).ToArray();
                // Search for closed
                for (int i = 0; i < pCount; i++)
                {
                    Debug.Assert(_parameters.Value[i] != null);
                    Type pType = _parameters.Value[i].ParameterType;
                    Debug.Assert(pType != null);
                    searchTypes[i] = Reflection.ExpandParameterType(pType, signatureClosures, typeGenericArguments);
                }

                // Add return type
                Type rType = Info.ReturnType;
                searchTypes[pCount] = Reflection.ExpandParameterType(rType, signatureClosures, typeGenericArguments);

                // Search for method on new type.
                return(et.GetMethod(Info.Name, signatureClosures.Length, searchTypes));
            }

            // Substitute missing types with concrete ones.
            int closures = signatureClosures.Length;

            Type[] gta = new Type[closures];
            for (int i = 0; i < closures; i++)
            {
                gta[i] = signatureClosures[i] ?? _genericArguments.Value[i].Type;
            }

            // Create closed method, cache it and return.
            // ReSharper disable once AssignNullToNotNullAttribute
            string key = string.Join("|", gta.Select(t => ExtendedType.Get(t).Signature));

            Debug.Assert(_closedMethods.Value != null);
            return(_closedMethods.Value.GetOrAdd(
                       key,
                       k =>
            {
                try
                {
                    return new Method(ExtendedType, Info.MakeGenericMethod(gta));
                }
                catch (ArgumentException)
                {
                    return null;
                }
            }));
        }
        public void ExtendedType_CanDisambiguateMembers()
        {
            ExtendedType et = ExtendedType.Get(typeof(ComplexOverloads <>));

            IEnumerable <Method> methods = et.GetMethods("A");

            Assert.IsNotNull(methods);
            Assert.AreEqual(8, methods.Count()); //Assert.AreEqual(9, methods.Count());

            Method invalidMethod = et.GetMethod("A");

            Assert.IsNull(invalidMethod);

            Method method1 = et.GetMethod("A", TypeSearch.T1);

            Assert.IsNotNull(method1);
            Assert.IsTrue(method1.Info.ContainsGenericParameters);

            // Demonstrate cast to void
            bool[] castsRequired;
            invalidMethod = et.GetMethod("A", 0, true, true, out castsRequired, TypeSearch.Void);
            Assert.IsNotNull(invalidMethod);
            Assert.IsTrue(castsRequired[0]);

            Method method2 = et.GetMethod("A", typeof(int), TypeSearch.Void);

            Assert.IsNotNull(method2);
            Assert.AreNotEqual(method1, method2);

            Method method3 = et.GetMethod("A", typeof(int).MakeByRefType(), TypeSearch.Void);

            Assert.IsNotNull(method3);
            Assert.AreNotEqual(method3, method1);
            Assert.AreNotEqual(method3, method2);

            /*
             * Method method3b = et.GetMethod("A", typeof (int).MakePointerType(), TypeSearch.Void);
             * Assert.IsNotNull(method3b);
             * Assert.AreNotEqual(method3b, method1);
             * Assert.AreNotEqual(method3b, method2);*/

            Method method4 = et.GetMethod("A", typeof(string), typeof(string), TypeSearch.Void);

            Assert.IsNotNull(method4);
            Assert.IsTrue(method4.Info.IsStatic);
            Assert.AreNotEqual(method4, method1);
            Assert.AreNotEqual(method4, method2);
            Assert.AreNotEqual(method4, method3);

            Method method5 = et.GetMethod("A", typeof(string), typeof(string).MakeByRefType(), TypeSearch.Void);

            Assert.IsNotNull(method5);
            Assert.AreNotEqual(method5, method1);
            Assert.AreNotEqual(method5, method2);
            //Assert.AreNotEqual(method5, method3b);
            Assert.AreNotEqual(method5, method4);

            Method method6 = et.GetMethod("A", 1, typeof(string), TypeSearch.Void);

            Assert.IsNotNull(method6);
            Assert.AreNotEqual(method6, method1);
            Assert.AreNotEqual(method6, method2);
            //Assert.AreNotEqual(method6, method3b);
            Assert.AreNotEqual(method6, method4);
            Assert.AreNotEqual(method6, method5);

            Method method7 = et.GetMethod("A", 2, typeof(string), TypeSearch.Void);

            Assert.IsNotNull(method7);
            Assert.AreNotEqual(method7, method1);
            Assert.AreNotEqual(method7, method2);
            //Assert.AreNotEqual(method7, method3b);
            Assert.AreNotEqual(method7, method4);
            Assert.AreNotEqual(method7, method5);
            Assert.AreNotEqual(method7, method6);

            Method method8 = et.GetMethod(
                "A",
                1,
                new TypeSearch(GenericArgumentLocation.Type, "T"),
                new TypeSearch(GenericArgumentLocation.Signature, 0, true),
                TypeSearch.Void);

            Assert.IsNotNull(method8);
        }
Esempio n. 7
0
            /// <summary>
            /// Initializes a new instance of the <see cref="PerfCategoryType" /> class.
            /// </summary>
            /// <param name="type">The type.</param>
            public PerfCategoryType([NotNull] Type type)
            {
                if (type == null)
                {
                    throw new ArgumentNullException("type");
                }
                Type = type;
                if ((type == typeof(PerfCategory)) ||
                    !type.DescendsFrom(typeof(PerfCategory)))
                {
                    Exception =
                        new InvalidOperationException(
                            string.Format(
                                // ReSharper disable once AssignNullToNotNullAttribute
                                Resources.PerfCategoryType_Must_Descend_From_PerfCategory,
                                type.FullName));
                    return;
                }

                // Try to get constructor that takes a string.
                try
                {
                    Creator = type.ConstructorFunc <string, PerfCategory>(true);
                }
                catch (Exception e)
                {
                    Exception =
                        new InvalidOperationException(
                            string.Format(
                                // ReSharper disable once AssignNullToNotNullAttribute
                                Resources.PerfCategoryType_Invalid_Constructor,
                                type.FullName),
                            e);
                    return;
                }

                try
                {
                    // ReSharper disable PossibleNullReferenceException
                    // ReSharper disable once AssignNullToNotNullAttribute
                    CreationData = ExtendedType.Get(type)
                                   .Fields
                                   .Single(
                        f => f.Info.IsStatic &&
                        f.Info.IsInitOnly &&
                        f.ReturnType ==
                        typeof(CounterCreationData[]))
                                   .Getter <CounterCreationData[]>()();
                    // ReSharper restore PossibleNullReferenceException
                }
                catch (Exception e)
                {
                    // Store the exception.
                    Exception =
                        new InvalidOperationException(
                            string.Format(
                                // ReSharper disable once AssignNullToNotNullAttribute
                                Resources.PerfCategoryType_Missing_Static_Readonly_Field,
                                type.FullName),
                            e);
                }
            }