Example #1
0
        private static bool CheckIfParametersEquals(ParameterInfo[] parameters, params Type[] targets)
        {
            if (parameters.Length != targets.Length)
                return false;

            return !parameters.Where((each, index) => each.ParameterType != targets[index]).Any();
        }
Example #2
0
        private static bool IsMatch(ParameterInfo[] parameters, Type[] parameterTypes)
        {
            if (parameterTypes == null)
                parameterTypes = EmptyTypes;

            if (parameters.Length != parameterTypes.Length)
                return false;

            return !parameters.Where((t, i) => t.ParameterType != parameterTypes[i]).Any();
        }
        /// <summary>
        /// Collect the arguments from given parameter types.
        /// </summary>
        /// <param name="dependencyResolver">The depenency resolver.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="resolutionContext">The resolution context.</param>
        /// <returns>The collected arguments.</returns>
        public object[] CollectArguments(Func<Type, object> dependencyResolver, ParameterInfo[] parameters, ResolutionContext resolutionContext)
        {
            var finalArguments = new List<object>();

            var dependencyParameters =
                parameters.Where(
                    p =>
                    resolutionContext.RegistrationContainer.HasRegistration(p.ParameterType)
                    ||
                    resolutionContext.RegistrationContainer.IsSupportedByRegistrationSource(p.ParameterType));

            Func<object, ParameterInfo, bool> argumentSelector = (argument, parameter) => parameter.ParameterType.IsInstanceOfType(argument);

            var runtimeArguments = resolutionContext.RuntimeArguments;
            var arguments = resolutionContext.Arguments;

            // Priority from heighest: Runtime arguments -> named / anonymous, Arguments -> named / anonymous / depdendency parameters.
            foreach (ParameterInfo parameter in parameters)
            {
                ParameterInfo localParameter = parameter;

                if (runtimeArguments.NamedArguments != null && runtimeArguments.NamedArguments.ContainsKey(parameter.Name))
                {
                    finalArguments.Add(runtimeArguments.NamedArguments[parameter.Name]);
                    continue;
                }

                if (runtimeArguments.AnonymousArguments != null && runtimeArguments.AnonymousArguments.Any(argument => argumentSelector(argument, localParameter)))
                {
                    finalArguments.Add(runtimeArguments.AnonymousArguments.FirstOrDefault(argument => argumentSelector(argument, localParameter)));
                    continue;
                }

                if (arguments.NamedArguments != null && arguments.NamedArguments.ContainsKey(parameter.Name))
                {
                    finalArguments.Add(arguments.NamedArguments[parameter.Name]);
                    continue;
                }

                if (arguments.AnonymousArguments != null && arguments.AnonymousArguments.Any(argument => argumentSelector(argument, localParameter)))
                {
                    finalArguments.Add(arguments.AnonymousArguments.FirstOrDefault(argument => argumentSelector(argument, localParameter)));
                    continue;
                }

                if (dependencyParameters.Contains(parameter))
                {
                    finalArguments.Add(dependencyResolver(parameter.ParameterType));
                    continue;
                }
            }

            return finalArguments.ToArray();
        }
		//===========================================================================================
		private void WriteAttributeForEach(IndentedTextWriter writer, ParameterInfo[] allParameters)
		{
			ParameterInfo[] parameters = allParameters.Where(p => _GraphicsMagickNET.GetXsdAttributeType(p) != null).ToArray();
			if (parameters.Length == 0)
				return;

			parameters = parameters.OrderBy(p => p.Name).ToArray();

			writer.WriteLine("for each(XmlAttribute^ attribute in element->Attributes)");
			WriteStartColon(writer);

			if (parameters.DistinctBy(p => _GraphicsMagickNET.GetCppTypeName(p)).Count() == 1)
			{
				writer.Write("arguments[attribute->Name] = _Variables->GetValue<");
				writer.Write(_GraphicsMagickNET.GetCppTypeName(parameters[0]));
				writer.WriteLine(">(attribute);");
			}
			else
			{
				for (int i = 0; i < parameters.Length; i++)
				{
					string xsdName = _GraphicsMagickNET.GetXsdName(parameters[i]);

					if (i > 0)
						writer.Write("else ");

					writer.Write("if (attribute->Name == \"");
					writer.Write(xsdName);
					writer.WriteLine("\")");
					writer.Indent++;
					writer.Write("arguments[\"");
					writer.Write(xsdName);
					writer.Write("\"] = ");
					WriteGetAttributeValue(writer, _GraphicsMagickNET.GetCppTypeName(parameters[i]), xsdName);
					writer.Indent--;
				}
			}

			WriteEndColon(writer);
		}
    private void WriteAttributeForEach(ParameterInfo[] allParameters)
    {
      ParameterInfo[] parameters = allParameters.Where(p => MagickScriptTypes.GetXsdAttributeType(p) != null).ToArray();
      if (parameters.Length == 0)
        return;

      parameters = parameters.OrderBy(p => p.Name).ToArray();

      WriteLine("foreach (XmlAttribute attribute in element.Attributes)");
      WriteStartColon();

      if (parameters.DistinctBy(p => GetName(p)).Count() == 1)
      {
        Write("arguments[attribute.Name] = Variables.GetValue<");
        Write(GetName(parameters[0]));
        WriteLine(">(attribute);");
      }
      else
      {
        for (int i = 0; i < parameters.Length; i++)
        {
          string xsdName = MagickScriptTypes.GetXsdName(parameters[i]);

          if (i > 0)
            Write("else ");

          Write("if (attribute.Name == \"");
          Write(xsdName);
          WriteLine("\")");
          Indent++;
          Write("arguments[\"");
          Write(xsdName);
          Write("\"] = ");
          WriteGetAttributeValue(GetName(parameters[i]));
          Indent--;
        }
      }

      WriteEndColon();
    }
		/// <summary>
		/// Copies fields from the temporary output parameters structure to the output parameters object specified by the caller.
		/// </summary>
		/// <param name="parameters">The list of parameters.</param>
		/// <param name="parameterWrapper">The local variable for the temporary object.</param>
		/// <param name="mIL">The ILGenerator to use.</param>
		private static void EmitOutputParameters(ParameterInfo[] parameters, LocalBuilder parameterWrapper, ILGenerator mIL)
		{
			foreach (var outputParameter in parameters.Where(p => p.IsOut))
			{
				mIL.Emit(OpCodes.Ldarg, outputParameter.Position + 1);
				mIL.Emit(OpCodes.Ldloc, parameterWrapper.LocalIndex);
				var fields = parameterWrapper.LocalType.GetFields();
				mIL.Emit(OpCodes.Ldfld, fields[outputParameter.Position]);
				mIL.Emit(OpCodes.Stobj, outputParameter.ParameterType.GetElementType());
			}
		}
 private static bool ParametersMatch(ParameterInfo[] methodParameters, object[] parameters)
 {
     return methodParameters.Length == parameters.Length
         && !methodParameters.Where((mp, i) => !mp.ParameterType.IsInstanceOfType(parameters[i])).Any();
 }
		//===========================================================================================
		private void WriteElementForEach(IndentedTextWriter writer, ParameterInfo[] allParameters)
		{
			ParameterInfo[] parameters = allParameters.Where(p => _GraphicsMagickNET.GetXsdAttributeType(p) == null).ToArray();
			if (parameters.Length == 0)
				return;

			writer.WriteLine("for each(XmlElement^ elem in element->SelectNodes(\"*\"))");
			WriteStartColon(writer);

			if (parameters.DistinctBy(p => _GraphicsMagickNET.GetCppTypeName(p)).Count() == 1)
			{
				writer.Write("arguments[elem->Name] = ");
				WriteCreateMethod(writer, _GraphicsMagickNET.GetCppTypeName(parameters[0]));
				writer.WriteLine("(elem);");
			}
			else
			{
				for (int i = 0; i < parameters.Length; i++)
				{
					string xsdName = _GraphicsMagickNET.GetXsdName(parameters[i]);

					if (i > 0)
						writer.Write("else ");

					writer.Write("if (elem->Name == \"");
					writer.Write(xsdName);
					writer.WriteLine("\")");
					writer.Indent++;
					writer.Write("arguments[\"");
					writer.Write(xsdName);
					writer.Write("\"] = ");
					WriteCreateMethod(writer, _GraphicsMagickNET.GetCppTypeName(parameters[i]));
					writer.WriteLine("(elem);");
					writer.Indent--;
				}
			}

			WriteEndColon(writer);
		}
    private void WriteElementForEach(ParameterInfo[] allParameters)
    {
      ParameterInfo[] parameters = allParameters.Where(p => MagickScriptTypes.GetXsdAttributeType(p) == null).ToArray();
      if (parameters.Length == 0)
        return;

      WriteLine("foreach (XmlElement elem in element.SelectNodes(\"*\"))");
      WriteStartColon();

      if (parameters.DistinctBy(p => GetName(p)).Count() == 1)
      {
        Write("arguments[elem.Name] = ");
        WriteCreateMethod(GetName(parameters[0]));
        WriteLine("(elem);");
      }
      else
      {
        for (int i = 0; i < parameters.Length; i++)
        {
          string xsdName = MagickScriptTypes.GetXsdName(parameters[i]);

          if (i > 0)
            Write("else ");

          Write("if (elem.Name == \"");
          Write(xsdName);
          WriteLine("\")");
          Indent++;
          Write("arguments[\"");
          Write(xsdName);
          Write("\"] = ");
          WriteCreateMethod(GetName(parameters[i]));
          WriteLine("(elem);");
          Indent--;
        }
      }

      WriteEndColon();
    }
 protected override ParameterInfo[] PrepareParams(ParameterInfo[] parameters)
 {
     return parameters.Where(p => p.ParameterType != typeof(System.Web.Mvc.FormCollection) && !p.ParameterType.IsSubclassOf(typeof(System.Web.Mvc.FormCollection))).ToArray();
 }