コード例 #1
0
        public bool TryGetAppropriateParameters(object[] parameters, Type[] pis, object subject)
        {
            bool compatible = true;

            for (int i = 0; i < pis.Length; i++)
            {
                // plymorphic
                if (parameters[i] != null && pis[i].IsInstanceOfType(parameters[i]))
                {
                    continue;
                }
                else if (parameters[i] == null && !pis[i].IsValueType)
                {
                    continue;
                }

                try
                {
                    if (pis[i].IsArray)
                    {
                        // try to convert every elements
                        JsArray array = parameters[i] as JsArray;

                        if (array == null)
                        {
                            compatible = false;
                            break;
                        }

                        Array newArray = Array.CreateInstance(pis[i], array.Length);

                        for (int k = 0; k < array.Length; k++)
                        {
                            newArray.SetValue(array[k.ToString()], k);
                        }
                    }
                    else if (typeof(Delegate).IsAssignableFrom(pis[i])) // wrap the JsFunction to a Delegate
                    {
                        DelegateWrapper dw = new DelegateWrapper();
                        dw.Visitor  = visitor;
                        dw.Function = (JsFunction)parameters[i];
                        dw.That     = visitor.Global.WrapClr(subject);

                        DynamicMethod dm = DelegateWrapper.GenerateDynamicMethod(pis[i]); // void (int)

                        parameters[i] = dm.CreateDelegate(pis[i], dw);
                    }
                    else if (pis[i].IsEnum && parameters[i] is string)
                    {
                        string[] enumNames = Enum.GetNames(pis[i]);
                        foreach (string name in ((string)parameters[i]).Split(' '))
                        {
                            if (Array.IndexOf(enumNames, name) < 0)
                            {
                                compatible = false;
                                break;
                            }
                        }
                        if (!compatible)
                        {
                            break;
                        }
                        parameters[i] = Enum.Parse(pis[i], (string)parameters[i]);
                    }
                    else
                    {
                        parameters[i] = Convert.ChangeType(parameters[i], pis[i]);
                    }
                }
                catch
                {
                    compatible = false;
                    break;
                }
            }
            return(compatible);
        }