Exemple #1
0
            private static void SetImportingMember(object part, MemberInfo member, object value)
            {
                Requires.NotNull(part, nameof(part));
                Requires.NotNull(member, nameof(member));

                bool containsGenericParameters = member.DeclaringType.GetTypeInfo().ContainsGenericParameters;

                if (containsGenericParameters)
                {
                    member = ReflectionHelpers.CloseGenericType(member.DeclaringType, part.GetType()).GetTypeInfo()
                             .GetMember(member.Name, MemberTypes.Property | MemberTypes.Field, DeclaredOnlyLookup)[0];
                }

                var property = member as PropertyInfo;

                if (property != null)
                {
                    property.SetValue(part, value);
                    return;
                }

                var field = member as FieldInfo;

                if (field != null)
                {
                    field.SetValue(part, value);
                    return;
                }

                throw new NotSupportedException();
            }
            private static void SetImportingMember(object part, MemberInfo member, object?value)
            {
                Requires.NotNull(part, nameof(part));
                Requires.NotNull(member, nameof(member));
                Requires.Argument(member.DeclaringType is object, nameof(member), "DeclaringType must not be null.");

                bool containsGenericParameters = member.DeclaringType.GetTypeInfo().ContainsGenericParameters;

                if (containsGenericParameters)
                {
                    member = ReflectionHelpers.CloseGenericType(member.DeclaringType, part.GetType()).GetTypeInfo()
                             .GetMember(member.Name, MemberTypes.Property | MemberTypes.Field, DeclaredOnlyLookup)[0];
                }

                try
                {
                    switch (member)
                    {
                    case PropertyInfo property:
                        property.SetValue(part, value);
                        break;

                    case FieldInfo field:
                        field.SetValue(part, value);
                        break;

                    default:
                        throw new NotSupportedException();
                    }
                }
                catch (Exception ex)
                {
                    throw new TargetInvocationException(string.Format(CultureInfo.CurrentCulture, Strings.ExceptionThrownByPartUnderInitialization, part.GetType().FullName), ex);
                }
            }
Exemple #3
0
            private static void SetImportingMember(object part, MemberInfo member, object value)
            {
                Requires.NotNull(part, nameof(part));
                Requires.NotNull(member, nameof(member));

                bool containsGenericParameters = member.DeclaringType.GetTypeInfo().ContainsGenericParameters;

                if (containsGenericParameters)
                {
                    member = ReflectionHelpers.CloseGenericType(member.DeclaringType, part.GetType()).GetTypeInfo()
                             .GetMember(member.Name, MemberTypes.Property | MemberTypes.Field, DeclaredOnlyLookup)[0];
                }

                switch (member)
                {
                case PropertyInfo property:
                    property.SetValue(part, value);
                    break;

                case FieldInfo field:
                    field.SetValue(part, value);
                    break;

                default:
                    throw new NotSupportedException();
                }
            }