Example #1
0
        public void CreateWithInterface()
        {
            var obj = TypeBasedExportDefinition.CreateDefinition("A", typeof(IEnumerable <>));

            Assert.AreEqual("A", obj.ContractName);
            Assert.AreEqual(TypeIdentity.CreateDefinition(typeof(IEnumerable <>)), obj.DeclaringType);
        }
Example #2
0
        public void CreateWithClass()
        {
            var obj = TypeBasedExportDefinition.CreateDefinition("A", typeof(List <int>));

            Assert.AreEqual("A", obj.ContractName);
            Assert.AreEqual(TypeIdentity.CreateDefinition(typeof(List <int>)), obj.DeclaringType);
        }
Example #3
0
        public void EqualsWithUnequalObjects()
        {
            var    first  = TypeIdentity.CreateDefinition(typeof(string));
            object second = TypeIdentity.CreateDefinition(typeof(object));

            Assert.IsFalse(first.Equals(second));
        }
Example #4
0
        public void RoundtripSerialize()
        {
            var original = TypeIdentity.CreateDefinition(typeof(string));
            var copy     = AssertExtensions.RoundTripSerialize(original);

            Assert.AreEqual(original, copy);
        }
Example #5
0
        public void CreateWithNestedClass()
        {
            var type = typeof(Nested <,>);
            var obj  = TypeBasedExportDefinition.CreateDefinition("A", type);

            Assert.AreEqual("A", obj.ContractName);
            Assert.AreEqual(TypeIdentity.CreateDefinition(typeof(Nested <,>)), obj.DeclaringType);
        }
Example #6
0
        public void CreateWithClass()
        {
            var obj       = ParameterDefinition.CreateDefinition(ParameterFromInt());
            var parameter = ParameterFromInt();

            Assert.AreEqual(parameter.Name, obj.Name);
            Assert.AreEqual(TypeIdentity.CreateDefinition(parameter.ParameterType), obj.Identity);
        }
Example #7
0
        public void Create()
        {
            var obj = MethodBasedExportDefinition.CreateDefinition("B", GetMethodForInt());

            Assert.AreEqual("B", obj.ContractName);
            Assert.AreEqual(TypeIdentity.CreateDefinition(typeof(int)), obj.DeclaringType);
            Assert.AreEqual(MethodDefinition.CreateDefinition(GetMethodForInt()), obj.Method);
        }
Example #8
0
        public void CreateWithClass()
        {
            var obj = TypeIdentity.CreateDefinition(typeof(List <int>));

            Assert.AreEqual(typeof(List <int>).Name, obj.Name);
            Assert.AreEqual(typeof(List <int>).Namespace, obj.Namespace);
            Assert.AreEqual(typeof(List <int>).FullName, obj.FullName);
            Assert.AreEqual(typeof(List <int>).Assembly.GetName().Name, obj.Assembly.Name);
        }
Example #9
0
        public void CreateWithInterface()
        {
            var obj = TypeIdentity.CreateDefinition(typeof(IEnumerable <>));

            Assert.AreEqual(typeof(IEnumerable <>).Name, obj.Name);
            Assert.AreEqual(typeof(IEnumerable <>).Namespace, obj.Namespace);
            Assert.AreEqual(typeof(IEnumerable <>).FullName, obj.FullName);
            Assert.AreEqual(typeof(IEnumerable <>).Assembly.GetName().Name, obj.Assembly.Name);
        }
Example #10
0
        public void CreateWithClass()
        {
            var obj      = PropertyBasedExportDefinition.CreateDefinition("A", GetPropertyForString());
            var property = GetPropertyForString();

            Assert.AreEqual("A", obj.ContractName);
            Assert.AreEqual(TypeIdentity.CreateDefinition(property.DeclaringType), obj.DeclaringType);
            Assert.AreEqual(PropertyDefinition.CreateDefinition(GetPropertyForString()), obj.Property);
        }
Example #11
0
        public void CreateWithClass()
        {
            var obj      = PropertyDefinition.CreateDefinition(GetPropertyForString());
            var property = GetPropertyForString();

            Assert.AreEqual(property.Name, obj.PropertyName);
            Assert.AreEqual(TypeIdentity.CreateDefinition(property.PropertyType), obj.PropertyType);
            Assert.AreEqual(TypeIdentity.CreateDefinition(property.DeclaringType), obj.DeclaringType);
        }
        public void CreateWithClass()
        {
            var obj         = ConstructorDefinition.CreateDefinition(GetConstructorForString());
            var constructor = GetConstructorForString();

            Assert.That(
                obj.Parameters,
                Is.EquivalentTo(constructor.GetParameters().Select(p => ParameterDefinition.CreateDefinition(p))));
            Assert.AreEqual(TypeIdentity.CreateDefinition(constructor.DeclaringType), obj.DeclaringType);
        }
Example #13
0
        public void CreateWithNestedClass()
        {
            var type = typeof(Nested <,>);
            var obj  = TypeIdentity.CreateDefinition(type);

            Assert.AreEqual(type.Name, obj.Name);
            Assert.AreEqual(type.Namespace, obj.Namespace);
            Assert.AreEqual(type.FullName, obj.FullName);
            Assert.AreEqual(type.Assembly.GetName().Name, obj.Assembly.Name);
        }
Example #14
0
        public void CreateWithInterface()
        {
            var obj = TypeDefinition.CreateDefinition(typeof(IEnumerable <>), TypeIdentity.CreateDefinition);

            Assert.AreEqual(typeof(IEnumerable <>).FullName, obj.Identity.FullName);
            Assert.AreEqual(typeof(IEnumerable <>).IsClass, obj.IsClass);
            Assert.AreEqual(typeof(IEnumerable <>).IsInterface, obj.IsInterface);
            Assert.IsNull(obj.BaseType);
            Assert.That(obj.BaseInterfaces, Is.EquivalentTo(new[] { TypeIdentity.CreateDefinition(typeof(IEnumerable)) }));
        }
Example #15
0
        public void CreateWithClass()
        {
            var obj    = MethodDefinition.CreateDefinition(GetMethodForInt());
            var method = GetMethodForInt();

            Assert.AreEqual(method.Name, obj.MethodName);
            Assert.AreEqual(TypeIdentity.CreateDefinition(method.ReturnType), obj.ReturnType);
            Assert.That(
                obj.Parameters,
                Is.EquivalentTo(method.GetParameters().Select(p => ParameterDefinition.CreateDefinition(p))));
            Assert.AreEqual(TypeIdentity.CreateDefinition(method.DeclaringType), obj.DeclaringType);
        }
        public void RoundtripSerialize()
        {
            var original = ConstructorBasedImportDefinition.CreateDefinition(
                "A",
                TypeIdentity.CreateDefinition(typeof(char[])),
                ImportCardinality.ExactlyOne,
                CreationPolicy.NonShared,
                GetConstructorForString().GetParameters().First());
            var copy = AssertExtensions.RoundTripSerialize(original);

            Assert.AreEqual(original, copy);
        }
Example #17
0
        public void RoundtripSerialize()
        {
            var original = PropertyBasedImportDefinition.CreateDefinition(
                "A",
                TypeIdentity.CreateDefinition(typeof(int)),
                ImportCardinality.ExactlyOne,
                true,
                CreationPolicy.NonShared,
                typeof(string).GetProperty("Length"));
            var copy = AssertExtensions.RoundTripSerialize(original);

            Assert.AreEqual(original, copy);
        }
Example #18
0
 /// <summary>
 /// Creates a new instance of the <see cref="ConstructorBasedImportDefinition"/> class
 /// based on the given <see cref="ParameterInfo"/>.
 /// </summary>
 /// <param name="contractName">The contract name that is used to identify the current import.</param>
 /// <param name="requiredTypeIdentity">The type identity of the export type expected.</param>
 /// <param name="cardinality">
 ///     One of the enumeration values that indicates the cardinality of the export object required by the import definition.
 /// </param>
 /// <param name="creationPolicy">
 ///     A value that indicates that the importer requires a specific creation policy for the exports used to satisfy this import.
 /// </param>
 /// <param name="parameter">The method for which the current object stores the serialized data.</param>
 /// <returns>The serialized definition for the given parameter.</returns>
 /// <exception cref="ArgumentNullException">
 ///     Thrown if <paramref name="parameter"/> is <see langword="null" />.
 /// </exception>
 public static ConstructorBasedImportDefinition CreateDefinition(
     string contractName,
     TypeIdentity requiredTypeIdentity,
     ImportCardinality cardinality,
     CreationPolicy creationPolicy,
     ParameterInfo parameter)
 {
     return(CreateDefinition(
                contractName,
                requiredTypeIdentity,
                cardinality,
                creationPolicy,
                parameter,
                t => TypeIdentity.CreateDefinition(t)));
 }
 public static PropertyBasedImportDefinition CreateDefinition(
     string contractName,
     TypeIdentity requiredTypeIdentity,
     ImportCardinality cardinality,
     bool isRecomposable,
     CreationPolicy creationPolicy,
     PropertyInfo property)
 {
     return(CreateDefinition(
                contractName,
                requiredTypeIdentity,
                cardinality,
                isRecomposable,
                creationPolicy,
                property,
                t => TypeIdentity.CreateDefinition(t)));
 }
        public void Create()
        {
            var obj = ConstructorBasedImportDefinition.CreateDefinition(
                "A",
                TypeIdentity.CreateDefinition(typeof(char[])),
                ImportCardinality.ExactlyOne,
                CreationPolicy.NonShared,
                GetConstructorForString().GetParameters().First());
            var constructor = GetConstructorForString();
            var parameter   = constructor.GetParameters().First();

            Assert.AreEqual("A", obj.ContractName);
            Assert.AreEqual(TypeIdentity.CreateDefinition(typeof(char[])), obj.RequiredTypeIdentity);
            Assert.AreEqual(ImportCardinality.ExactlyOne, obj.Cardinality);
            Assert.IsFalse(obj.IsRecomposable);
            Assert.IsTrue(obj.IsPrerequisite);
            Assert.AreEqual(ConstructorDefinition.CreateDefinition(constructor), obj.Constructor);
            Assert.AreEqual(TypeIdentity.CreateDefinition(typeof(string)), obj.DeclaringType);
            Assert.AreEqual(ParameterDefinition.CreateDefinition(parameter), obj.Parameter);
        }
Example #21
0
        public void CreateWithClass()
        {
            var obj = PropertyBasedImportDefinition.CreateDefinition(
                "A",
                TypeIdentity.CreateDefinition(typeof(int)),
                ImportCardinality.ExactlyOne,
                true,
                CreationPolicy.NonShared,
                typeof(string).GetProperty("Length"));
            var property = GetPropertyForString();

            Assert.AreEqual("A", obj.ContractName);
            Assert.AreEqual(TypeIdentity.CreateDefinition(typeof(int)), obj.RequiredTypeIdentity);
            Assert.AreEqual(ImportCardinality.ExactlyOne, obj.Cardinality);
            Assert.IsTrue(obj.IsRecomposable);
            Assert.IsFalse(obj.IsPrerequisite);
            Assert.AreEqual(CreationPolicy.NonShared, obj.RequiredCreationPolicy);
            Assert.AreEqual(TypeIdentity.CreateDefinition(property.DeclaringType), obj.DeclaringType);
            Assert.AreEqual(PropertyDefinition.CreateDefinition(property), obj.Property);
        }
Example #22
0
            protected override PropertyBasedImportDefinition Copy(PropertyBasedImportDefinition original)
            {
                if (original.ContractName.Equals("A"))
                {
                    return(PropertyBasedImportDefinition.CreateDefinition(
                               "A",
                               TypeIdentity.CreateDefinition(typeof(int)),
                               ImportCardinality.ExactlyOne,
                               true,
                               CreationPolicy.NonShared,
                               typeof(string).GetProperty("Length")));
                }

                return(PropertyBasedImportDefinition.CreateDefinition(
                           "B",
                           TypeIdentity.CreateDefinition(typeof(int)),
                           ImportCardinality.ExactlyOne,
                           true,
                           CreationPolicy.NonShared,
                           typeof(Version).GetProperty("Build")));
            }
Example #23
0
        /// <summary>
        /// Creates a new instance of the <see cref="TypeDefinition"/> class based on the given <see cref="Type"/>.
        /// </summary>
        /// <param name="type">The type for which a serialized definition needs to be created.</param>
        /// <param name="identityGenerator">The function that creates type identities.</param>
        /// <returns>The serialized definition for the given type.</returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="type"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="identityGenerator"/> is <see langword="null" />.
        /// </exception>
        public static TypeDefinition CreateDefinition(Type type, Func <Type, TypeIdentity> identityGenerator)
        {
            {
                Lokad.Enforce.Argument(() => type);
                Lokad.Enforce.Argument(() => identityGenerator);
            }

            // Note that the following call may lead to a StackOverflow if the identityGenerator function
            // isn't smart enough to verify if we're in the process of generating a TypeDefinition for a given
            // type. e.g. if we're handling System.Boolean we also have to process System.IComparable<System.Boolean>
            // which could lead to an infinite loop.
            var identity = TypeIdentity.CreateDefinition(type, identityGenerator);

            // Generic types that don't have a base generic type have themselves as their own generic type definition!
            // e.g. the generic type definition for IEnumerable<T> is ... IEnumerable<T>. So we only assign a
            // generic type definition to a type that isn't one itself.
            return(new TypeDefinition(
                       identity,
                       type.BaseType != null ? identityGenerator(type.BaseType) : null,
                       type.GetInterfaces().Select(i => identityGenerator(i)).ToArray(),
                       identity.IsGenericType && type != type.GetGenericTypeDefinition() ? identityGenerator(type.GetGenericTypeDefinition()) : null,
                       type.IsClass,
                       type.IsInterface));
        }
Example #24
0
        public void CreateWithClass()
        {
            var obj = TypeDefinition.CreateDefinition(typeof(List <int>), TypeIdentity.CreateDefinition);

            Assert.AreEqual(typeof(List <int>).FullName, obj.Identity.FullName);
            Assert.AreEqual(typeof(List <int>).IsClass, obj.IsClass);
            Assert.AreEqual(typeof(List <int>).IsInterface, obj.IsInterface);

            Assert.AreEqual(typeof(List <int>).BaseType.FullName, obj.BaseType.FullName);

            var interfaces = new[]
            {
                TypeIdentity.CreateDefinition(typeof(IList <int>)),
                TypeIdentity.CreateDefinition(typeof(ICollection <int>)),
                TypeIdentity.CreateDefinition(typeof(IEnumerable <int>)),
                TypeIdentity.CreateDefinition(typeof(IReadOnlyCollection <int>)),
                TypeIdentity.CreateDefinition(typeof(IReadOnlyList <int>)),
                TypeIdentity.CreateDefinition(typeof(IList)),
                TypeIdentity.CreateDefinition(typeof(ICollection)),
                TypeIdentity.CreateDefinition(typeof(IEnumerable)),
            };

            Assert.That(obj.BaseInterfaces, Is.EquivalentTo(interfaces));
        }
Example #25
0
 /// <summary>
 /// Creates a new instance of the <see cref="MethodBasedScheduleConditionDefinition"/> class
 /// based on the given <see cref="MethodInfo"/>.
 /// </summary>
 /// <param name="contractName">The contract name that is used to identity the current condition.</param>
 /// <param name="method">The method for which a serialized definition needs to be created.</param>
 /// <returns>The serialized definition for the given method.</returns>
 /// <exception cref="ArgumentNullException">
 ///     Thrown if <paramref name="method"/> is <see langword="null" />.
 /// </exception>
 public static MethodBasedScheduleConditionDefinition CreateDefinition(string contractName, MethodInfo method)
 {
     return(CreateDefinition(contractName, method, t => TypeIdentity.CreateDefinition(t)));
 }
Example #26
0
 /// <summary>
 /// Creates a new instance of the <see cref="MethodDefinition"/> class based on the given <see cref="PropertyInfo"/>.
 /// </summary>
 /// <param name="property">The property for which a serialized definition needs to be created.</param>
 /// <returns>The serialized definition for the given property.</returns>
 /// <exception cref="ArgumentNullException">
 ///     Thrown if <paramref name="property"/> is <see langword="null" />.
 /// </exception>
 public static PropertyDefinition CreateDefinition(PropertyInfo property)
 {
     return(CreateDefinition(property, t => TypeIdentity.CreateDefinition(t)));
 }
Example #27
0
 /// <summary>
 /// Creates a new instance of the <see cref="MethodDefinition"/> class based on the given <see cref="MethodInfo"/>.
 /// </summary>
 /// <param name="method">The method for which a serialized definition needs to be created.</param>
 /// <returns>The serialized definition for the given method.</returns>
 /// <exception cref="ArgumentNullException">
 ///     Thrown if <paramref name="method"/> is <see langword="null" />.
 /// </exception>
 public static MethodDefinition CreateDefinition(MethodInfo method)
 {
     return(CreateDefinition(method, t => TypeIdentity.CreateDefinition(t)));
 }
Example #28
0
 /// <summary>
 /// Creates a new instance of the <see cref="ConstructorDefinition"/> class based on the given <see cref="ConstructorInfo"/>.
 /// </summary>
 /// <param name="constructor">The constructor for which a serialized definition needs to be created.</param>
 /// <returns>The serialized definition for the given constructor.</returns>
 /// <exception cref="ArgumentNullException">
 ///     Thrown if <paramref name="constructor"/> is <see langword="null" />.
 /// </exception>
 public static ConstructorDefinition CreateDefinition(ConstructorInfo constructor)
 {
     return(CreateDefinition(constructor, t => TypeIdentity.CreateDefinition(t)));
 }
Example #29
0
 /// <summary>
 /// Creates a new instance of the <see cref="TypeBasedExportDefinition"/> class
 /// based on the given <see cref="Type"/>.
 /// </summary>
 /// <param name="contractName">The contract name that is used to identify the current export.</param>
 /// <param name="declaringType">The method for which the current object stores the serialized data.</param>
 /// <returns>The serialized definition for the given type.</returns>
 /// <exception cref="ArgumentNullException">
 ///     Thrown if <paramref name="declaringType"/> is <see langword="null" />.
 /// </exception>
 public static TypeBasedExportDefinition CreateDefinition(string contractName, Type declaringType)
 {
     return(CreateDefinition(contractName, declaringType, t => TypeIdentity.CreateDefinition(t)));
 }
 /// <summary>
 /// Creates a new instance of the <see cref="PropertyBasedExportDefinition"/> class based on the given <see cref="PropertyInfo"/>.
 /// </summary>
 /// <param name="contractName">The contract name that is used to identify the current export.</param>
 /// <param name="property">The property for which a serialized definition needs to be created.</param>
 /// <returns>The serialized definition for the given property.</returns>
 /// <exception cref="ArgumentNullException">
 ///     Thrown if <paramref name="property"/> is <see langword="null" />.
 /// </exception>
 public static PropertyBasedExportDefinition CreateDefinition(string contractName, PropertyInfo property)
 {
     return(CreateDefinition(contractName, property, t => TypeIdentity.CreateDefinition(t)));
 }