public void ExtendedType_CanDisambiguateConstructors()
        {
            // Implicit cast supported
            ExtendedType et = typeof(ComplexOverloads <>);

            // Grab all constructors
            IEnumerable <Constructor> constructors = et.Constructors;

            Assert.IsNotNull(constructors);
            // Note only two non-static constructors (static constructor treated separately).
            Assert.AreEqual(2, constructors.Count());

            // Grab the static constructor
            Constructor staticConstructor = et.StaticConstructor;

            Assert.IsNotNull(staticConstructor);
            Assert.IsTrue(staticConstructor.Info.IsStatic);
            Assert.IsFalse(constructors.Contains(staticConstructor));

            // Retrieve parameterless constructor
            Constructor constructor = et.GetConstructor(typeof(ComplexOverloads <int>));

            Assert.IsNotNull(constructor);

            // Retrieve the generic constructor
            Constructor genericConstructor = et.GetConstructor(
                TypeSearch.T1,
                typeof(string),
                typeof(ComplexOverloads <int>));

            Assert.IsNotNull(genericConstructor);
            Assert.AreNotSame(constructor, genericConstructor);
        }
Esempio n. 2
0
        public Constructor Close([NotNull] Type[] typeClosures)
        {
            if (typeClosures == null)
            {
                throw new ArgumentNullException("typeClosures");
            }

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

            // If we haven't got any type closures, we can return this constructor.
            if (typeClosures.All(t => t == null))
            {
                return(this);
            }

            // 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, Array <Type> .Empty, typeGenericArguments);
            }

            // Add return type
            searchTypes[pCount] = et.Type;

            // Search for constructor on new type.
            return(et.GetConstructor(searchTypes));
        }
        public void Constructor_FuncConstructorValid()
        {
            ExtendedType et          = typeof(ComplexOverloads <int>);
            Constructor  constructor = et.GetConstructor(typeof(int), typeof(string), typeof(ComplexOverloads <int>));

            Assert.IsNotNull(constructor);
            Func <int, string, ComplexOverloads <int> > constFunc =
                constructor.GetFunc(typeof(int), typeof(string), typeof(ComplexOverloads <int>)) as
                Func <int, string, ComplexOverloads <int> >;

            Assert.IsNotNull(constFunc);
            int    a = Random.Next();
            string s = Random.RandomString();
            ComplexOverloads <int> co = constFunc(a, s);

            Assert.IsNotNull(co);
            Assert.AreEqual(a, co.Value);
            Assert.AreEqual(s, co.Value2);
        }