Beispiel #1
0
        private ICollection <object> GetNormalizedCollection(Type itemType, object instance)
        {
            Assumes.NotNull(itemType);

            object collectionObject = null;

            if (this._member.CanRead)
            {
                try
                {
                    collectionObject = this._member.GetValue(instance);
                }
                catch (TargetInvocationException exception)
                {
                    throw new ComposablePartException(
                              String.Format(CultureInfo.CurrentCulture,
                                            Strings.ReflectionModel_ImportCollectionGetThrewException,
                                            this._member.GetDisplayName()),
                              this.Definition.ToElement(),
                              exception.InnerException);
                }
            }

            if (collectionObject == null)
            {
                ConstructorInfo constructor = this.ImportType.ActualType.GetConstructor(Type.EmptyTypes);

                // If it contains a default public constructor create a new instance.
                if (constructor != null)
                {
                    try
                    {
                        collectionObject = constructor.SafeInvoke();
                    }
                    catch (TargetInvocationException exception)
                    {
                        throw new ComposablePartException(
                                  String.Format(CultureInfo.CurrentCulture,
                                                Strings.ReflectionModel_ImportCollectionConstructionThrewException,
                                                this._member.GetDisplayName(),
                                                this.ImportType.ActualType.FullName),
                                  this.Definition.ToElement(),
                                  exception.InnerException);
                    }

                    SetSingleMemberValue(instance, collectionObject);
                }
            }

            if (collectionObject == null)
            {
                throw new ComposablePartException(
                          String.Format(CultureInfo.CurrentCulture,
                                        Strings.ReflectionModel_ImportCollectionNull,
                                        this._member.GetDisplayName()),
                          this.Definition.ToElement());
            }

            return(CollectionServices.GetCollectionWrapper(itemType, collectionObject));
        }
        private ICollection <object> GetNormalizedCollection(Type itemType, object?instance)
        {
            if (itemType == null)
            {
                throw new ArgumentNullException(nameof(itemType));
            }

            object?collectionObject = null;

            if (_member.CanRead)
            {
                try
                {
                    collectionObject = _member.GetValue(instance);
                }
                catch (TargetInvocationException exception)
                {
                    throw new ComposablePartException(
                              SR.Format(
                                  SR.ReflectionModel_ImportCollectionGetThrewException,
                                  _member.GetDisplayName()),
                              Definition.ToElement(),
                              exception.InnerException);
                }
            }

            if (collectionObject == null)
            {
                ConstructorInfo?constructor = ImportType.ActualType.GetConstructor(Type.EmptyTypes);

                // If it contains a default public constructor create a new instance.
                if (constructor != null)
                {
                    try
                    {
                        collectionObject = constructor.SafeInvoke();
                    }
                    catch (TargetInvocationException exception)
                    {
                        throw new ComposablePartException(
                                  SR.Format(
                                      SR.ReflectionModel_ImportCollectionConstructionThrewException,
                                      _member.GetDisplayName(),
                                      ImportType.ActualType.FullName),
                                  Definition.ToElement(),
                                  exception.InnerException);
                    }

                    SetSingleMemberValue(instance, collectionObject);
                }
            }

            if (collectionObject == null)
            {
                throw new ComposablePartException(
                          SR.Format(
                              SR.ReflectionModel_ImportCollectionNull,
                              _member.GetDisplayName()),
                          Definition.ToElement());
            }

            return(CollectionServices.GetCollectionWrapper(itemType, collectionObject));
        }