Example #1
0
        public Argument(object target, ParameterInfo mi)
            : base(target, mi)
        {
            _default = null;
            _required = true;
            _allArguments = Parameter.IsDefined(typeof (AllArgumentsAttribute), true);

            if (Parameter.DefaultValue != DBNull.Value)
            {
                _default = Parameter.DefaultValue;
                _required = false;
            }

            foreach (DefaultValueAttribute a in mi.GetCustomAttributes(typeof (DefaultValueAttribute), true))
            {
                _default = a.Value;
                _required = false;
            }

            foreach (ArgumentAttribute a in mi.GetCustomAttributes(typeof (ArgumentAttribute), true))
            {
                if (a.HasDefault)
                {
                    _required = false;
                    _default = a.DefaultValue;
                }
            }

            if (Visible && _description == mi.ToString())
            {
                if (IsFlag)
                {
                    if (Required)
                        _description = String.Format("Required flag can be \"/{0}\" or \"/{0}:false\".", base.DisplayName);
                    else
                        _description = String.Format("Optional flag of \"/{0}\" or \"/{0}:false\".", base.DisplayName);
                }
                else
                {
                    if (Required)
                        _description = String.Format("Specifies the required value for \"{0}\" of type {1}.", base.DisplayName,
                            UnderlyingType.Name);
                    else
                        _description = String.Format("Specifies an optional value for \"{0}\" of type {1}.", base.DisplayName,
                            UnderlyingType.Name);
                }
            }
        }
        private static void BuildParameter(ParameterBuilder builder, System.Reflection.ParameterInfo paramInfo, int position, int count, CommandService service)
        {
            var attributes = paramInfo.GetCustomAttributes();
            var paramType  = paramInfo.ParameterType;

            builder.Name = paramInfo.Name;

            builder.IsOptional   = paramInfo.IsOptional;
            builder.DefaultValue = paramInfo.HasDefaultValue ? paramInfo.DefaultValue : null;

            foreach (var attribute in attributes)
            {
                // TODO: C#7 type switch
                if (attribute is SummaryAttribute)
                {
                    builder.Summary = (attribute as SummaryAttribute).Text;
                }
                else if (attribute is OverrideTypeReaderAttribute)
                {
                    builder.TypeReader = GetTypeReader(service, paramType, (attribute as OverrideTypeReaderAttribute).TypeReader);
                }
                else if (attribute is ParameterPreconditionAttribute)
                {
                    builder.AddPrecondition(attribute as ParameterPreconditionAttribute);
                }
                else if (attribute is ParamArrayAttribute)
                {
                    builder.IsMultiple = true;
                    paramType          = paramType.GetElementType();
                }
                else if (attribute is RemainderAttribute)
                {
                    if (position != count - 1)
                    {
                        throw new InvalidOperationException("Remainder parameters must be the last parameter in a command.");
                    }

                    builder.IsRemainder = true;
                }
            }

            builder.ParameterType = paramType;

            if (builder.TypeReader == null)
            {
                var        readers = service.GetTypeReaders(paramType);
                TypeReader reader  = null;

                if (readers != null)
                {
                    reader = readers.FirstOrDefault().Value;
                }
                else
                {
                    reader = service.GetDefaultTypeReader(paramType);
                }

                builder.TypeReader = reader;
            }
        }
        public static bool TryGetDefaultValue(ParameterInfo parameterInfo, out object value)
        {
            // this will get the default value as seen by the VB / C# compilers
            // if no value was baked in, RawDefaultValue returns DBNull.Value
            object rawDefaultValue = null;
            try {
              rawDefaultValue = parameterInfo.RawDefaultValue;
            }
            catch (NotImplementedException) {
              rawDefaultValue = null;
            }
            if (rawDefaultValue != DBNull.Value) {
                value = rawDefaultValue;
                return true;
            }

            // if the compiler did not bake in a default value, check the [DefaultValue] attribute
            DefaultValueAttribute[] attrs = (DefaultValueAttribute[])parameterInfo.GetCustomAttributes(typeof(DefaultValueAttribute), false);
            if (attrs == null || attrs.Length == 0) {
                value = default(object);
                return false;
            }
            else {
                value = attrs[0].Value;
                return true;
            }
        }
Example #4
0
 internal static object GetCustomAttribute(Type type, ParameterInfo pi)
 {
     object [] attributes = pi.GetCustomAttributes (type, true);
     if (attributes.Length < 1)
         return null;
     return attributes [0];
 }
Example #5
0
 public virtual string GetARDataBindPrefix(ParameterInfo p)
 {
     var db = p.GetCustomAttributes<ARDataBindAttribute>();
     if (db.Length == 0)
         return null;
     return db[0].Prefix;
 }
Example #6
0
 public virtual string GetARFetch(ParameterInfo p)
 {
     var db = p.GetCustomAttributes<ARFetchAttribute>();
     if (db.Length == 0)
         return null;
     return db[0].RequestParameterName ?? p.Name;
 }
 private static void AppendParameterInfo(StringBuilder toolTipText, ParameterInfo parameterInfo, bool isLastParameter)
 {
     System.Type parameterType = parameterInfo.ParameterType;
     if (parameterType != null)
     {
         if (parameterType.IsByRef)
         {
             if (parameterInfo.IsOut)
             {
                 toolTipText.Append("out ");
             }
             else
             {
                 toolTipText.Append("ref ");
             }
             parameterType = parameterType.GetElementType();
         }
         else if (isLastParameter && parameterType.IsArray)
         {
             object[] customAttributes = parameterInfo.GetCustomAttributes(typeof(ParamArrayAttribute), false);
             if ((customAttributes != null) && (customAttributes.Length > 0))
             {
                 toolTipText.Append("params ");
             }
         }
         toolTipText.Append(RuleDecompiler.DecompileType(parameterType));
         toolTipText.Append(" ");
     }
     toolTipText.Append(parameterInfo.Name);
 }
    /// <summary>
    /// Resolves the value for the parameter.
    /// </summary>
    /// <param name="value">The initial value.</param>
    /// <param name="parameter">The parameter.</param>
    /// <returns>The new value.</returns>
    protected override object ResolveValue(object value, ParameterInfo parameter)
    {
      SitecoreConfigurationMapAttribute mapping = parameter.GetCustomAttributes(typeof(SitecoreConfigurationMapAttribute)).Cast<SitecoreConfigurationMapAttribute>().SingleOrDefault(map => map.ArgumentType == parameter.ParameterType);

      if (mapping == null)
      {
        mapping = parameter.Member.GetCustomAttributes(typeof(SitecoreConfigurationMapAttribute)).Cast<SitecoreConfigurationMapAttribute>().SingleOrDefault(map => map.ArgumentType == parameter.ParameterType);
      }

      if (mapping == null)
      {
        mapping = parameter.Member.ReflectedType.GetCustomAttributes(typeof(SitecoreConfigurationMapAttribute)).Cast<SitecoreConfigurationMapAttribute>().SingleOrDefault(map => map.ArgumentType == parameter.ParameterType);
      }

      if (mapping == null)
      {
        mapping = parameter.Member.ReflectedType.Assembly.GetCustomAttributes(typeof(SitecoreConfigurationMapAttribute)).Cast<SitecoreConfigurationMapAttribute>().SingleOrDefault(map => map.ArgumentType == parameter.ParameterType);
      }

      if (mapping == null)
      {
        return null;
      }

      return Factory.CreateObject(mapping.ConfigurationNode, true);
    }
        public static CustomPropertyInfo GetFromParamInfo(System.Reflection.ParameterInfo info)
        {
            if (info == null)
            {
                return(null);
            }

            CustomPropertyInfo retInfo = new CustomPropertyInfo();

            retInfo.PropertyName = info.Name;
            if (info.IsOut || info.ParameterType.IsByRef)
            {
                retInfo.PropertyType = EngineNS.Rtti.RttiHelper.GetTypeFromTypeFullName(info.ParameterType.FullName.Remove(info.ParameterType.FullName.Length - 1));
            }
            else
            {
                //retInfo.PropertyType = CSUtility.Program.GetTypeFromTypeFullName(info.ParameterType.FullName);
                retInfo.PropertyType = info.ParameterType;
            }

            foreach (var att in info.GetCustomAttributes(true))
            {
                retInfo.PropertyAttributes.Add(att as Attribute);
            }
            retInfo.DefaultValue = CodeGenerateSystem.Program.GetDefaultValueFromType(retInfo.PropertyType);
            retInfo.CurrentValue = retInfo.DefaultValue;
            return(retInfo);
        }
    /// <summary>
    /// Resolves the value for the parameter.
    /// </summary>
    /// <param name="value">The initial value.</param>
    /// <param name="parameter">The parameter.</param>
    /// <returns>The new value.</returns>
    protected override object ResolveValue(object value, ParameterInfo parameter)
    {
      IEnumerable<string> configurationNodes = parameter.GetCustomAttributes(typeof(SitecoreConfigurationReferenceAttribute)).Cast<SitecoreConfigurationReferenceAttribute>().Select(attribute => attribute.ConfigurationNode);

      if (!configurationNodes.Any())
      {
        configurationNodes = parameter.Member.GetCustomAttributes(typeof(SitecoreConfigurationReferenceAttribute)).Cast<SitecoreConfigurationReferenceAttribute>().Select(attribute => attribute.ConfigurationNode);
        
        if (parameter.Member.ReflectedType != null)
        {
          configurationNodes = configurationNodes.Union(parameter.Member.ReflectedType.GetCustomAttributes(typeof(SitecoreConfigurationReferenceAttribute)).Cast<SitecoreConfigurationReferenceAttribute>().Select(attribute => attribute.ConfigurationNode));
          configurationNodes = configurationNodes.Union(parameter.Member.ReflectedType.Assembly.GetCustomAttributes(typeof(SitecoreConfigurationReferenceAttribute)).Cast<SitecoreConfigurationReferenceAttribute>().Select(attribute => attribute.ConfigurationNode));
        }
      }

      XmlNode targetNode = null;

      foreach (string configurationNode in configurationNodes)
      {
        XmlNode nodeCandidate = Factory.GetConfigNode(configurationNode, true);

        if (parameter.ParameterType.IsAssignableFrom(Factory.CreateType(nodeCandidate, true)))
        {
          if (targetNode != null)
          {
            throw new InvalidOperationException(string.Format("Cannot resolve value for argument '{0}'. There are several configuration nodes applicable to this argument.", parameter.Name));
          }

          targetNode = nodeCandidate;
        }
      }

      return Factory.CreateObject(targetNode, true);
    }
        /// <summary>
        /// Gets the service name based on the given <paramref name="parameter"/>.
        /// </summary>
        /// <param name="parameter">The <see cref="ParameterInfo"/> for which to get the service name.</param>
        /// <returns>The name of the service for the given <paramref name="parameter"/>.</returns>
        private string GetServiceName(ParameterInfo parameter)
        {
            var injectAttribute =
                      (InjectAttribute)
                      parameter.GetCustomAttributes(typeof(InjectAttribute), true).FirstOrDefault();

            return injectAttribute != null ? injectAttribute.ServiceName : parameter.Name;
        }
Example #12
0
        /// <summary>
        /// Provide the list of attributes applied to the specified parameter.
        /// </summary>
        /// <param name="reflectedType">The reflectedType the type used to retrieve the parameterInfo.</param>
        /// <param name="parameter">The parameter to supply attributes for.</param>
        /// <returns>The list of applied attributes.</returns>
        public override IEnumerable <Attribute> GetCustomAttributes(Type reflectedType, System.Reflection.ParameterInfo parameter)
        {
            Requires.NotNull(parameter, nameof(parameter));
            var attributes = parameter.GetCustomAttributes <Attribute>(false);
            List <Attribute> cachedAttributes = ReadParameterCustomAttributes(reflectedType, parameter);

            return(cachedAttributes == null ? attributes : attributes.Concat(cachedAttributes));
        }
Example #13
0
        /// <summary>
        /// Gets the attributes for the given <paramref name="parameter"/>.
        /// </summary>
        /// <param name="parameter">A <see cref="ParameterInfo"/> for which attributes need to be resolved.
        /// </param>
        /// <returns>An <see cref="IEnumerable{object}"/> containing the attributes on the
        /// <paramref name="parameter"/> before the attributes on the <paramref name="parameter"/> type.</returns>
        public static IEnumerable<object> GetAttributesForParameter(ParameterInfo parameter)
        {
            // Return the parameter attributes first.
            var parameterAttributes = parameter.GetCustomAttributes();
            var typeAttributes = parameter.ParameterType.GetTypeInfo().GetCustomAttributes();

            return parameterAttributes.Concat(typeAttributes);
        }
        // allowing Custom Marshaling to be injected
        public XlParameterInfo(ParameterInfo paramInfo)
        {
            // Add Name and Description
            // CONSIDER: Override Marshaler for row/column arrays according to some attribute

            // Some pre-checks
            if (paramInfo.ParameterType.IsByRef)
                throw new DnaMarshalException("Parameter is ByRef: " + paramInfo.Name);

            // Default Name and Description
            Name = paramInfo.Name;
            Description = "";
            AllowReference = false;

            // Get Description
            object[] attribs = paramInfo.GetCustomAttributes(false);
            // Search through attribs for Description
            foreach (object attrib in attribs)
            {
                System.ComponentModel.DescriptionAttribute desc =
                    attrib as System.ComponentModel.DescriptionAttribute;
                if (desc != null)
                {
                    Description = desc.Description;
                }
                //// HACK: Some problem with library references -
                //// For now relax the assembly reference and use late-bound
                Type attribType = attrib.GetType();
                if (TypeHelper.TypeHasAncestorWithFullName(attribType, "ExcelDna.Integration.ExcelArgumentAttribute"))
                {
                    string name = (string)attribType.GetField("Name").GetValue(attrib);
                    string description = (string)attribType.GetField("Description").GetValue(attrib);
                    object allowReference = attribType.GetField("AllowReference").GetValue(attrib);

                    if (name != null)
                        Name = name;
                    if (description != null)
                        Description = description;
                    if (allowReference != null)
                        AllowReference = (bool)allowReference;
                }
                // HACK: Here is the other code:
                //ExcelArgumentAttribute xlparam = attrib as ExcelArgumentAttribute;
                //if (xlparam != null)
                //{
                //    if (xlparam.Name != null)
                //    {
                //        Name = xlparam.Name;
                //    }
                //    if (xlparam.Description != null)
                //    {
                //        Description = xlparam.Description;
                //    }
                //    AllowReference = xlparam.AllowReference;
                //}
            }
            SetTypeInfo(paramInfo.ParameterType, false, false);
        }
        private static void BuildParameter(ParameterBuilder builder, System.Reflection.ParameterInfo paramInfo, int position, int count, CommandService service, IServiceProvider services)
        {
            var attributes = paramInfo.GetCustomAttributes();
            var paramType  = paramInfo.ParameterType;

            builder.Name = paramInfo.Name;

            builder.IsOptional   = paramInfo.IsOptional;
            builder.DefaultValue = paramInfo.HasDefaultValue ? paramInfo.DefaultValue : null;

            foreach (var attribute in attributes)
            {
                switch (attribute)
                {
                case SummaryAttribute summary:
                    builder.Summary = summary.Text;
                    break;

                case OverrideTypeReaderAttribute typeReader:
                    builder.TypeReader = GetTypeReader(service, paramType, typeReader.TypeReader, services);
                    break;

                case ParamArrayAttribute _:
                    builder.IsMultiple = true;
                    paramType          = paramType.GetElementType();
                    break;

                case ParameterPreconditionAttribute precon:
                    builder.AddPrecondition(precon);
                    break;

                case NameAttribute name:
                    builder.Name = name.Text;
                    break;

                case RemainderAttribute _:
                    if (position != count - 1)
                    {
                        throw new InvalidOperationException($"Remainder parameters must be the last parameter in a command. Parameter: {paramInfo.Name} in {paramInfo.Member.DeclaringType.Name}.{paramInfo.Member.Name}");
                    }

                    builder.IsRemainder = true;
                    break;

                default:
                    builder.AddAttributes(attribute);
                    break;
                }
            }

            builder.ParameterType = paramType;

            if (builder.TypeReader == null)
            {
                builder.TypeReader = service.GetDefaultTypeReader(paramType)
                                     ?? service.GetTypeReaders(paramType)?.FirstOrDefault().Value;
            }
        }
 /// <summary>
 /// Converts an object to a type compatible with a given parameter.
 /// </summary>
 /// <param name="value">The object value to convert.</param>
 /// <param name="destinationType">The destination <see cref="Type"/> to which <paramref name="value"/> should be converted.</param>
 /// <param name="memberInfo">The parameter for which the <paramref name="value"/> is being converted.</param>
 /// <returns>
 /// An <see cref="Object"/> of type <paramref name="destinationType"/>, converted using
 /// type converters specified on <paramref name="memberInfo"/> if available. If <paramref name="value"/>
 /// is <see langword="null"/> then the output will be <see langword="null"/> for reference
 /// types and the default value for value types.
 /// </returns>
 /// <exception cref="InvalidOperationException">
 /// Thrown if conversion of the value fails.
 /// </exception>
 public static object ChangeToCompatibleType(object value, Type destinationType, ParameterInfo memberInfo)
 {
     TypeConverterAttribute attrib = null;
     if (memberInfo != null)
     {
         attrib = memberInfo.GetCustomAttributes(typeof(TypeConverterAttribute), true).Cast<TypeConverterAttribute>().FirstOrDefault();
     }
     return ChangeToCompatibleType(value, destinationType, attrib);
 }
Example #17
0
 public static string DescribeParameter(ParameterInfo parameter)
 {
     var result = string.Empty;
     if (parameter.IsOut) result += "out ";
     if (parameter.IsRetval) result += "ref ";
     if (parameter.GetCustomAttributes(typeof(ParamArrayAttribute), true).Length > 0) result += "params ";
     result += DescribeType(parameter.ParameterType);
     return result.Trim();
 }
		public static TemplateDefaultValueAttribute GetFromParameter(ParameterInfo parameter)
		{
			object[] attributes = parameter.GetCustomAttributes(typeof(TemplateDefaultValueAttribute), true);

			if (attributes == null || attributes.Length == 0)
				return null;

			return (TemplateDefaultValueAttribute)attributes[0];
		}
 	internal void AddRulesForParameterInfo(ParameterInfo parameterInfo)
 	{
 		var parameterAttributes = parameterInfo.GetCustomAttributes(RuleAttributeType, true);
 		for (var parameterRuleIndex = 0; parameterRuleIndex < parameterAttributes.Length; parameterRuleIndex++)
 		{
 			var parameterRuleAttribute = (IRuleAttribute)parameterAttributes[parameterRuleIndex];
 			var parameterRule = parameterRuleAttribute.CreateRule(this);
             Rules.Add(parameterRule);
 		}
 	}
Example #20
0
        public ParameterData(ParameterInfo parameterInfo)
        {
            Type = parameterInfo.ParameterType;

            Attribute[] attributes = parameterInfo.GetCustomAttributes(typeof(ServiceKeyAttribute)).ToArray();
            if (attributes.Length > 0)
                ServiceKey = ((ServiceKeyAttribute)attributes.First()).Key;

            InjectableAttribute = parameterInfo.GetCustomAttribute<InjectableAttribute>();
        }
Example #21
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="ParameterDescriptor" /> class.
 /// </summary>
 /// <param name="pi">A ParameterInfo taken from reflection.</param>
 public ParameterDescriptor(ParameterInfo pi)
 {
     Name = pi.Name;
     Type = pi.ParameterType;
     HasDefaultValue = !(pi.DefaultValue.IsDbNull());
     DefaultValue = pi.DefaultValue;
     IsOut = pi.IsOut;
     IsRef = pi.ParameterType.IsByRef;
     IsVarArgs = (pi.ParameterType.IsArray && pi.GetCustomAttributes(typeof (ParamArrayAttribute), true).Any());
 }
Example #22
0
 public Consolery.OptionalData GetOptional(ParameterInfo info)
 {
     if (info.IsOptional)
     {
         return new Consolery.OptionalData { Default = info.DefaultValue };
     }
     object[] attributes = info.GetCustomAttributes(typeof(OptionalAttribute), false);
     var attribute = (OptionalAttribute)attributes[0];
     return new Consolery.OptionalData { AltNames = attribute.AltNames, Default = attribute.Default };
 }
Example #23
0
        public static bool MarkedAuthenticationIgnore(ParameterInfo parameterInfo)
        {
            var result = parameterInfo.GetCustomAttributes(typeof(AuthenticationIgnoreAttribute), false).Length > 0;

            if (false == result) return false;

            if (NeverIgnoreParameter(parameterInfo.Name))
                throw new ArgumentException(string.Format("Parameter: {0} can not be marked ignore.", parameterInfo.Name));

            return true;
        }
Example #24
0
        public ReflectedParameterModel(ParameterInfo parameterInfo)
        {
            ParameterInfo = parameterInfo;

            // CoreCLR returns IEnumerable<Attribute> from GetCustomAttributes - the OfType<object>
            // is needed to so that the result of ToList() is List<object>
            Attributes = parameterInfo.GetCustomAttributes(inherit: true).OfType<object>().ToList();

            ParameterName = parameterInfo.Name;
            IsOptional = ParameterInfo.HasDefaultValue;
        }
Example #25
0
        /// <summary>
        /// Provide the list of attributes applied to the specified parameter.
        /// </summary>
        /// <param name="reflectedType">The reflectedType the type used to retrieve the parameterInfo.</param>
        /// <param name="parameter">The parameter to supply attributes for.</param>
        /// <returns>The list of applied attributes.</returns>
        public override IEnumerable <Attribute> GetCustomAttributes(Type reflectedType, System.Reflection.ParameterInfo parameter)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }

            IEnumerable <Attribute> attributes       = parameter.GetCustomAttributes <Attribute>(false);
            List <Attribute>        cachedAttributes = ReadParameterCustomAttributes(reflectedType, parameter);

            return(cachedAttributes == null ? attributes : attributes.Concat(cachedAttributes));
        }
        /// <summary>
        /// Return an IEnumerable providing data for use with the
        /// supplied parameter.
        /// </summary>
        /// <param name="parameter">A ParameterInfo representing one
        /// argument to a parameterized test</param>
        /// <returns>
        /// An IEnumerable providing the required data
        /// </returns>
        public IEnumerable GetDataFor(ParameterInfo parameter)
        {
            var data = new List<object>();

            foreach (IParameterDataSource source in parameter.GetCustomAttributes(typeof(IParameterDataSource), false))
            {
                foreach (object item in source.GetData(parameter))
                data.Add(item);
            }

            return data;
        }
 private static IModelBinder GetModelBinder(ParameterInfo parameter)
 {
     ModelBinderAttribute[] attrs = parameter.GetCustomAttributes<ModelBinderAttribute>().ToArray();
     switch (attrs.Length)
     {
         case 0:
             return null;
         case 1:
             return attrs[0].GetBinder();
         default:
             throw Error.MultipleModelBinderAttributes(parameter);
     }
 }
        public string GetMethodParamName(ParameterInfo parameterInfo)
        {
            var modelNameProvider = parameterInfo.GetCustomAttributes()
                .OfType<IModelNameProvider>()
                .FirstOrDefault();

            if (modelNameProvider == null)
            {
                return parameterInfo.Name;
            }

            return modelNameProvider.Name;
        }
 private object[] GenerateParameterValueSet(ParameterInfo parameterInfo)
 {
     var valueSet = new List<object>();
     foreach (var attribute in parameterInfo.GetCustomAttributes())
     {
         var valueGenerator = attribute as IValueGeneratorAttribute;
         if (valueGenerator != null)
         {
             valueSet.AddRange(valueGenerator.GenerateValues());
         }
     }
     return valueSet.ToArray();
 }
Example #30
0
 //--- Methods ---
 public ReflectedParameterInfo BuildParameter(ParameterInfo parameter) {
     var isByRef = parameter.ParameterType.IsByRef;
     var type = isByRef ? parameter.ParameterType.GetElementType() : parameter.ParameterType;
     var parameterInfo = new ReflectedParameterInfo {
         Name = parameter.Name,
         ParameterPosition = parameter.Position,
         IsOut = parameter.IsOut,
         IsRef = !parameter.IsOut && isByRef,
         IsParams = parameter.GetCustomAttributes(typeof(ParamArrayAttribute), false).Any(),
         Type = BuildType(type)
     };
     return parameterInfo;
 }
Example #31
0
		public Argument(object target, ParameterInfo mi)
			: base(target, mi)
		{
			_default = null;
			_required = true;
			_allArguments = Parameter.IsDefined(typeof(AllArgumentsAttribute), true);

			foreach (DefaultValueAttribute a in mi.GetCustomAttributes(typeof(DefaultValueAttribute), true))
			{
				_default = a.Value;
				_required = false;
			}

			foreach (ArgumentAttribute a in mi.GetCustomAttributes(typeof(ArgumentAttribute), true))
			{
				if (a.HasDefault)
				{
					_required = false;
					_default = a.DefaultValue;
				}
			}
		}
        /// <summary>
        /// Return an IEnumerable providing data for use with the
        /// supplied parameter.
        /// </summary>
        /// <param name="parameter">A ParameterInfo representing one
        /// argument to a parameterized test</param>
        /// <returns>
        /// An IEnumerable providing the required data
        /// </returns>
        public IEnumerable GetDataFor(ParameterInfo parameter)
        {
            ObjectList data = new ObjectList();

            foreach (Attribute attr in parameter.GetCustomAttributes(typeof(DataAttribute), false))
            {
                IParameterDataSource source = attr as IParameterDataSource;
                if (source != null)
                    foreach (object item in source.GetData(parameter))
                        data.Add(item);
            }

            return data;
        }
        public ParameterInfoWrapper(ParameterInfo parameter)
        {
            Name = parameter.Name;
            Type = parameter.ParameterType;

            var attributes = parameter.GetCustomAttributes(typeof(CommandParameterAttribute), true);
            if (attributes.Length > 0)
            {
                var commandParameter = (CommandParameterAttribute)attributes.First();
                Prototype = commandParameter.Prototype;

                Description = Description.GetNewIfValid(commandParameter.Description);
                DefaultValue = commandParameter.DefaultValue;
                IsRequired = commandParameter.IsRequired;
            }

            IsValueRequiredWhenOptionIsPresent = parameter.ParameterType != typeof(bool);
        }
Example #34
0
        static Attribute[] InternalParamGetCustomAttributes (ParameterInfo parameter, Type attributeType, bool inherit)
        {
            if (parameter.Member.MemberType != MemberTypes.Method)
                return null;

            var method = (MethodInfo) parameter.Member;
            var definition = method.GetBaseDefinition ();

			if (attributeType == null)
				attributeType = typeof (Attribute);

            if (method == definition)
		return (Attribute []) parameter.GetCustomAttributes (attributeType, inherit);

            var types = new List<Type> ();
            var custom_attributes = new List<Attribute> ();

            while (true) {
                var param = method.GetParametersInternal () [parameter.Position];
                var param_attributes = (Attribute []) param.GetCustomAttributes (attributeType, false);
                foreach (var param_attribute in param_attributes) {
                    var param_type = param_attribute.GetType ();
                    if (types.Contains (param_type))
                        continue;

                    types.Add (param_type);
                    custom_attributes.Add (param_attribute);
                }

                var base_method = method.GetBaseMethod ();
                if (base_method == method)
                    break;

                method = base_method;
            }

            var attributes = (Attribute []) Array.CreateInstance (attributeType, custom_attributes.Count);
            custom_attributes.CopyTo (attributes, 0);

            return attributes;
        }
Example #35
0
        public static bool TryGetDefaultValue(ParameterInfo parameterInfo, out object value)
        {
            object defaultValue = parameterInfo.DefaultValue;
            if (defaultValue != DBNull.Value)
            {
                value = defaultValue;
                return true;
            }

            DefaultValueAttribute[] attrs = (DefaultValueAttribute[])parameterInfo.GetCustomAttributes(typeof(DefaultValueAttribute), false);
            if (attrs == null || attrs.Length == 0)
            {
                value = default(object);
                return false;
            }
            else
            {
                value = attrs[0].Value;
                return true;
            }
        }
 public static bool HasAttribute <T>(this ParameterInfo member) where T : Attribute
 {
     return(member.GetCustomAttributes <T>().Any());
 }
Example #37
0
 public static IEnumerable <Attribute> GetCustomAttributes(this ParameterInfo parameter) =>
 parameter.GetCustomAttributes(false).Cast <Attribute>();
 public static Attribute GetCustomAttribute(this ParameterInfo info, Type type)
 {
     return(info.GetCustomAttributes(type, true)
            .Cast <Attribute>()
            .FirstOrDefault());
 }