Пример #1
0
 public ActionMetadata(MethodInfo action, ParameterMetadata[] parameters, int cancellationTokenIndex, Type resultType, bool hasSerializableParameters)
 {
     Action = action;
     Parameters = parameters;
     CancellationTokenIndex = cancellationTokenIndex;
     ResultType = resultType;
     HasSerializableParameters = hasSerializableParameters;
 }
 /*********
 ** Public methods
 *********/
 /// <summary>Get an error message indicating why the usage is invalid (or <c>null</c> if usage is valid).</summary>
 /// <param name="parameter">Represents a parameter annotated by a single contract annotation.</param>
 public string GetError(ParameterMetadata parameter)
 {
     if (!this.IsAllowedType(parameter.ParameterQualifiedType, parameter.ParameterTypeIsUnknown))
     {
         return String.Format(
             "This annotation is used incorrectly and will be ignored: {0} on the {1}::{2}({3}) parameter. The contract is only compatible with reference types.",
             parameter.Annotation.GetType(),
             parameter.TypeName,
             parameter.MethodName,
             parameter.Name
         );
     }
     return null;
 }
        private static IEnumerable<ParameterMetadata> GetRouteParameters(ServiceMethodMetadata metadata, IProxyMetadata proxyMetadata)
        {
            var routeParameters = new List<ParameterMetadata>();

            foreach (ParameterInfo parameter in metadata.MethodInfo.GetParameters())
            {
                if (metadata.UrlInfo.UrlTemplate.IndexOf(String.Concat("{", parameter.Name, "}"), StringComparison.OrdinalIgnoreCase) < 0)
                {
                    continue;
                }

                ParameterMetadata parameterMetadata = proxyMetadata != null ?
                                                                proxyMetadata.GetParameter(metadata.MethodInfo, parameter.Name, RequestParameterType.Route) :
                                                                null;

                var routeParameter = new ParameterMetadata
                {
                    Name = parameter.Name.ToLowerInvariant(),
                    ParameterType = RequestParameterType.Route,
                    Type = parameter.ParameterType,
                    IsOptionalParameter = parameter.DefaultValue != DBNull.Value,
                    RegexConstraint = GetParameterConstraint(parameter),
                    ExampleValue = parameterMetadata != null ? parameterMetadata.ExampleValue : null,
                    AllowedValues = parameterMetadata != null ? parameterMetadata.AllowedValues : null
                };

                routeParameters.Add(routeParameter);
            }

            return routeParameters;
        }
        /// <summary>
        /// Get cmdlets from the given assembly
        /// </summary>
        /// <param name="assemblyPath"></param>
        /// <returns></returns>
        public IList<CmdletSignatureMetadata> GetCmdlets(string assemblyPath)
        {
            IList<CmdletSignatureMetadata> result = new List<CmdletSignatureMetadata>();
            try
            {
                var assembly = Assembly.LoadFrom(assemblyPath);
                foreach (var type in assembly.GetCmdletTypes())
                {
                    var cmdlet = type.GetAttribute<CmdletAttribute>();
                    var outputs = type.GetAttributes<OutputTypeAttribute>();
                    var parameters = type.GetParameters();

                    var cmdletMetadata = new CmdletSignatureMetadata
                    {
                        VerbName = cmdlet.VerbName,
                        NounName = cmdlet.NounName,
                        ConfirmImpact = cmdlet.ConfirmImpact,
                        SupportsShouldProcess = cmdlet.SupportsShouldProcess,
                        ClassName = type.FullName,
                        AssemblyName = assembly.FullName
                    };
                    foreach (var output in outputs)
                    {
                        foreach (var outputType in output.Type)
                        {
                            var outputMetadata = new OutputMetadata
                            {
                                Type = outputType.Type
                            };
                            outputMetadata.ParameterSets.AddRange(output.ParameterSetName);
                        }
                    }

                    foreach (var parameter in parameters)
                    {
                        if ( parameter.Name == "Force" && parameter.PropertyType == typeof (SwitchParameter))
                        {
                            cmdletMetadata.HasForceSwitch = true;
                        }

                        var parameterData = new ParameterMetadata
                        {
                            Type = parameter.PropertyType,
                            Name = parameter.Name
                        };
                        if (parameter.HasAttribute<AliasAttribute>())
                        {
                            var aliases = parameter.GetAttributes<AliasAttribute>();
                            parameterData.AliasList.AddRange(
                                aliases.SelectMany(a => a.AliasNames));
                        }

                        cmdletMetadata.Parameters.Add(parameterData);
                    }

                    result.Add(cmdletMetadata);
                }
            }
            catch
            {
            }

            return result;
        }