private string GetTargetName(
     Option<BindingConfig> bindingConfig,
     TypePair typePair,
     MemberInfo sourceMember,
     Dictionary<string, string> targetBindings)
 {
     Option<string> targetName;
     List<BindAttribute> binds = sourceMember.GetAttributes<BindAttribute>();
     BindAttribute bind = binds.FirstOrDefault(x => x.TargetType.IsNull());
     if (bind.IsNull())
     {
         bind = binds.FirstOrDefault(x => typePair.Target.IsAssignableFrom(x.TargetType));
     }
     if (bind.IsNotNull())
     {
         targetName = new Option<string>(bind.MemberName);
     }
     else
     {
         targetName = bindingConfig.Map(x => x.GetBindField(sourceMember.Name));
         if (targetName.HasNoValue)
         {
             string targetMemberName;
             if (targetBindings.TryGetValue(sourceMember.Name, out targetMemberName))
             {
                 targetName = new Option<string>(targetMemberName);
             }
             else
             {
                 targetName = new Option<string>(sourceMember.Name);
             }
         }
     }
     return targetName.Value;
 }
Example #2
0
        public static ITestAction[] GetActionsFromAttributeProvider(MemberInfo attributeProvider)
        {
            if (attributeProvider == null)
                return new ITestAction[0];

            var actions = attributeProvider.GetAttributes<ITestAction>(false).ToList();
            actions.Sort(SortByTargetDescending);

            return actions.ToArray();
        }
Example #3
0
File: Test.cs Project: nunit/nunit
 /// <summary>
 /// Modify a newly constructed test by applying any of NUnit's common
 /// attributes, based on a supplied ICustomAttributeProvider, which is
 /// usually the reflection element from which the test was constructed,
 /// but may not be in some instances. The attributes retrieved are 
 /// saved for use in subsequent operations.
 /// </summary>
 /// <param name="provider">An object deriving from MemberInfo</param>
 public void ApplyAttributesToTest(MemberInfo provider)
 {
     foreach (IApplyToTest iApply in provider.GetAttributes<IApplyToTest>(true))
         iApply.ApplyToTest(this);
 }
        public static ReflectionMemberImportDefinition CreateMemberImportDefinition(MemberInfo member, ICompositionElement origin)
        {
            Requires.NotNull(member, "member");

            ReflectionWritableMember reflectionMember = member.ToReflectionWritableMember();
            IAttributedImport attributedImport = AttributedModelDiscovery.GetAttributedImport(reflectionMember, member);
            ImportType importType = new ImportType(reflectionMember.ReturnType, attributedImport.Cardinality);

            if (importType.IsPartCreator)
            {
                return new PartCreatorMemberImportDefinition(
                    new LazyMemberInfo(member),
                    origin,
                    new ContractBasedImportDefinition(
                        attributedImport.GetContractNameFromImport(importType),
                        attributedImport.GetTypeIdentityFromImport(importType),
                        CompositionServices.GetRequiredMetadata(importType.MetadataViewType),
                        attributedImport.Cardinality,
                        attributedImport.AllowRecomposition,
                        false,
                        (attributedImport.RequiredCreationPolicy != CreationPolicy.NewScope) ? CreationPolicy.NonShared : CreationPolicy.NewScope,
                        CompositionServices.GetImportMetadata(importType, attributedImport)));
            }
            else
            {
                // A Standard parameter import is not allowed to be marked as requiring NewScope at this time.
                if(attributedImport.RequiredCreationPolicy == CreationPolicy.NewScope)
                {
                    throw new ComposablePartException(
                        String.Format(CultureInfo.CurrentCulture,
                            Strings.InvalidPartCreationPolicyOnImport,
                            attributedImport.RequiredCreationPolicy),
                        origin);
                }

                //Does this Import re-export the value if so, make it a rpe-requisite
                bool isPrerequisite = member.GetAttributes<ExportAttribute>().Length > 0;
                return new ReflectionMemberImportDefinition(
                    new LazyMemberInfo(member),
                    attributedImport.GetContractNameFromImport(importType),
                    attributedImport.GetTypeIdentityFromImport(importType),
                    CompositionServices.GetRequiredMetadata(importType.MetadataViewType),
                    attributedImport.Cardinality,
                    attributedImport.AllowRecomposition,
                    isPrerequisite,
                    attributedImport.RequiredCreationPolicy,
                    CompositionServices.GetImportMetadata(importType, attributedImport),
                    origin);
            }
        }
 internal static int GetMaxStringLength(MemberInfo info)
 {
     MaxLengthAttribute[] attrs = info.GetAttributes<MaxLengthAttribute>().ToArray();
     int maxLength = attrs.Any()
                         ? attrs.First().Length
                         : DefaultMaxStringLength;
     return maxLength;
 }
        internal static string GetColumnName(MemberInfo info)
        {
            string name = info.Name;

            ColumnAttribute[] attrs = info.GetAttributes<ColumnAttribute>().ToArray();
            if (attrs.Any())
            {
                string attributeName = attrs.First().Name;
                if (!string.IsNullOrEmpty(attributeName))
                {
                    name = attributeName;
                }
            }

            return name;
        }
 internal static Collation GetCollation(MemberInfo info)
 {
     CollationAttribute[] attrs = info.GetAttributes<CollationAttribute>().ToArray();
     return attrs.Any() ? attrs.First().Collation : Collation.Default;
 }
 internal static string GetDefaultValue(MemberInfo info)
 {
     DefaultAttribute[] attrs = info.GetAttributes<DefaultAttribute>().ToArray();
     return attrs.Any() ? attrs.First().Value : null;
 }
 private bool IsIgnore(Option<BindingConfig> bindingConfig, TypePair typePair, MemberInfo sourceMember)
 {
     List<IgnoreAttribute> ignores = sourceMember.GetAttributes<IgnoreAttribute>();
     if (ignores.Any(x => x.TargetType.IsNull()))
     {
         return true;
     }
     if (ignores.FirstOrDefault(x => typePair.Target.IsAssignableFrom(x.TargetType)).IsNotNull())
     {
         return true;
     }
     return bindingConfig.Map(x => x.IsIgnoreSourceField(sourceMember.Name)).Value;
 }
Example #10
0
        /// <summary>
        /// Gets the display name getter.
        /// </summary>
        /// <param name="member">The member.</param>
        /// <returns></returns>
        public static Func<string> GetDisplayNameGetter(MemberInfo member) {
            if (member == null) return null;

            var displayAttribute = member.GetAttributes<DisplayAttribute>(true).FirstOrDefault();
            if (displayAttribute != null) return displayAttribute.GetName;

            return null;
        }
Example #11
0
 /// <summary>
 /// Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member.
 /// </summary>
 /// <typeparam name="TAttribute">The type of attribute to search for.</typeparam>
 /// <param name="memberInfo"> The member to inspect.</param>
 /// <param name="inherit">true to inspect the ancestors of element; otherwise, false.</param>
 /// <returns>A custom attribute that matches attributeType, or null if no such attribute is found.</returns>
 public static TAttribute GetAttribute <TAttribute>(this MemberInfo memberInfo, bool inherit = false) where TAttribute : System.Attribute
 {
     return(memberInfo.GetAttributes <TAttribute>(inherit).FirstOrDefault());
 }
 private static ConfigurationValidatorBase GetValidator(MemberInfo mi)
 {
     ConfigurationValidatorBase validator = new DefaultValidator();
     var validators = mi.GetAttributes<ValidationAttribute>();
     if (validators.IsNullOrEmpty() == false)
     {
         validator = new CompositeConfigurationValidator(validators, mi.Name);
     }
     return validator;
 }
Example #13
0
 /// <summary>
 /// Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member.
 /// </summary>
 /// <typeparam name="TAttribute">The type of attribute to search for.</typeparam>
 /// <param name="memberInfo"> The member to inspect.</param>
 /// <param name="defaultValue">Default value (null as default)</param>
 /// <param name="inherit">true to inspect the ancestors of element; otherwise, false.</param>
 /// <returns>A custom attribute that matches attributeType, or null if no such attribute is found.</returns>
 public static TAttribute GetAttribute <TAttribute>(this MemberInfo memberInfo, bool inherit = false, TAttribute defaultValue = default)
 => memberInfo.GetAttributes <TAttribute>(inherit).FirstOrDefault() ?? defaultValue;