public object FromMessage(IMessage message) { if (message == null) { return(null); } if (message.Payload == null) { throw new ArgumentException("Message payload must not be null."); } object[] args = new object[_parameterMetadata.Length]; for (int i = 0; i < _parameterMetadata.Length; i++) { MethodParameterMetadata metadata = this._parameterMetadata[i]; Type expectedType = metadata.ParameterType; HeaderAttribute headerAttr = metadata.HeaderAttr; if (headerAttr != null) { string headerName = metadata.HeaderName; object value = message.Headers.Get(headerName); if (value == null && headerAttr.Required) { throw new MessageHandlingException(message, "required header '" + headerName + "' not available"); } args[i] = value; } else if (metadata.HasHeadersAnnotation) { if (typeof(Properties).IsAssignableFrom(expectedType)) { args[i] = GetStringTypedHeaders(message); } else { args[i] = message.Headers; } } else if (expectedType.IsAssignableFrom(typeof(IMessage)) && typeof(IMessage).IsAssignableFrom(expectedType)) { args[i] = message; } else { args[i] = message.Payload; } } return(args); }
private void InitializeParameterMetadata() { bool foundMessageOrPayload = false; _parameterMetadata = new MethodParameterMetadata[_method.GetParameters().Length]; for (int i = 0; i < _parameterMetadata.Length; i++) { MethodParameterMetadata metadata = new MethodParameterMetadata(_method, i); if (metadata.HeaderAttr == null && !metadata.HasHeadersAnnotation) { // this is either a Message or the Object to be used as a Message payload if (foundMessageOrPayload) { throw new ArgumentException("only one Message or payload parameter is allowed"); } foundMessageOrPayload = true; } _parameterMetadata[i] = metadata; } }
public TypeMetadata(Type inputType) { Namespace = inputType.Namespace; Name = inputType.ToString(); AssemblyQualifiedName = inputType.AssemblyQualifiedName; // Get the properties of the type var properties = inputType.GetProperties(); // Sort the properties by name to retain ordering when loading cmdlets Array.Sort(properties, delegate(PropertyInfo p1, PropertyInfo p2) { return(p1.PropertyType.ToString().CompareTo(p2.PropertyType.ToString())); }); var methods = inputType.GetMethods() .Where(m => !m.IsSpecialName) .ToArray(); var constructors = inputType.GetConstructors(); ModuleMetadata moduleMetadata = CmdletLoader.ModuleMetadata; // If the type is an array if (inputType.HasElementType) { // Get the element type of the array ElementType = inputType.GetElementType().ToString(); // If the element type is not in the type dictionary, // add it and check its properties if (!moduleMetadata.TypeDictionary.ContainsKey(ElementType)) { moduleMetadata.TypeDictionary.Add(ElementType, new TypeMetadata() { Name = ElementType }); var typeMetadata = new TypeMetadata(inputType.GetElementType()); moduleMetadata.TypeDictionary[ElementType] = typeMetadata; } return; } // If the type is a generic if (inputType.IsGenericType) { // Get the argument types var genericTypeArguments = inputType.GetGenericArguments(); // For each of the arguments, add them to the list of arguments // and check if the type is in the type dictionary foreach (var arg in genericTypeArguments) { _genericTypeArguments.Add(arg.ToString()); if (!moduleMetadata.TypeDictionary.ContainsKey(arg.ToString())) { moduleMetadata.TypeDictionary.Add(arg.ToString(), new TypeMetadata() { Name = arg.ToString() }); var typeMetadata = new TypeMetadata(arg); moduleMetadata.TypeDictionary[arg.ToString()] = typeMetadata; } } return; } // If the type is in the System namespace, don't look at any of its properties if (Namespace.StartsWith("System")) { return; } // For each property, check to see if we have already processed its type before, // and if not, we will add it to the global dictionary and process the TypeMetadata. foreach (var property in properties) { // Get the property type var propertyType = property.PropertyType; // If the type has not been seen before, we will map it to a null value in the // global dictionary so we don't repeat the computation, and then create the // TypeMetadata object associated with it, and finally set it as the value // associated with the type name key in the dictionary. if (!moduleMetadata.TypeDictionary.ContainsKey(propertyType.ToString())) { moduleMetadata.TypeDictionary.Add(propertyType.ToString(), new TypeMetadata() { Name = propertyType.ToString() }); var typeMetadata = new TypeMetadata(propertyType); moduleMetadata.TypeDictionary[propertyType.ToString()] = typeMetadata; } // Add the property to the dictionary if (!_properties.ContainsKey(property.Name.ToString())) { _properties.Add(property.Name, propertyType.ToString()); } } foreach (var method in methods) { var methodParameterMetadata = new List <MethodParameterMetadata>(); MethodMetadata methodMetadata = null; if (moduleMetadata.TypeDictionary.ContainsKey(method.ReturnType.ToString())) { var typeMetadata = moduleMetadata.TypeDictionary[method.ReturnType.ToString()]; methodMetadata = new MethodMetadata() { Name = method.Name, ReturnType = typeMetadata.Name }; } else { moduleMetadata.TypeDictionary.Add(method.ReturnType.ToString(), new TypeMetadata() { Name = method.ReturnType.ToString() }); var typeMetadata = new TypeMetadata(method.ReturnType); moduleMetadata.TypeDictionary[method.ReturnType.ToString()] = typeMetadata; methodMetadata = new MethodMetadata() { Name = method.Name, ReturnType = typeMetadata.Name }; } foreach (var parameter in method.GetParameters()) { var parameterMetadata = new MethodParameterMetadata() { Name = parameter.Name, Type = parameter.GetType().ToString() }; methodParameterMetadata.Add(parameterMetadata); } methodMetadata.Parameters = methodParameterMetadata; _methods.Add(methodMetadata); } foreach (var constructor in constructors) { var constructorParameterMetadata = new List <MethodParameterMetadata>(); var constructorMetadata = new ConstructorMetadata(); foreach (var parameter in constructor.GetParameters()) { var parameterMetadata = new MethodParameterMetadata() { Name = parameter.Name, Type = parameter.GetType().ToString() }; constructorParameterMetadata.Add(parameterMetadata); } constructorMetadata.Parameters = constructorParameterMetadata; _constructors.Add(constructorMetadata); } }
public IMessage ToMessage(object obj) { object[] parameters = obj as object[]; if (parameters == null) { throw new ArgumentNullException("parameters must not be null"); } if (parameters.Length == 0) { throw new ArgumentException("Paramter ArrayUtils is required"); } if (parameters.Length != _method.GetParameters().Length) { throw new ArgumentException("wrong number of parameters: expected " + _method.GetParameters().Length + ", received " + parameters.Length); } IMessage message = null; object payload = null; IDictionary <string, object> headers = new Dictionary <string, object>(); for (int i = 0; i < parameters.Length; ++i) { object value = parameters[i]; MethodParameterMetadata metadata = _parameterMetadata[i]; if (metadata.HeaderAttr != null) { if (value != null) { headers.Add(metadata.HeaderName, value); } else { if (metadata.HeaderAttr.Required) { throw new ArgumentException("header '" + _parameterMetadata[i].HeaderName + "' is required"); } } } else if (metadata.HasHeadersAnnotation) { if (value != null) { AddHeadersAnnotatedParameterToMap(value, headers); } } else if (typeof(IMessage).IsAssignableFrom(metadata.ParameterType)) { message = (IMessage)value; } else { if (value == null) { throw new ArgumentException("payload object must not be null"); } payload = value; } } if (message != null) { if (headers.Count == 0) { return(message); } return(MessageBuilder.FromMessage(message).CopyHeadersIfAbsent(headers).Build()); } if (payload == null) { throw new ArgumentException("no parameter available for Message or payload"); } return(MessageBuilder.WithPayload(payload).CopyHeaders(headers).Build()); }