private bool IsParameterMatch(Type parameterType, SmartObject smarty, out object convertedValue) { object value = smarty.Value; if (value == null) { convertedValue = value; return parameterType.IsClass; } if (parameterType.IsAssignableFrom(smarty.Type)) { convertedValue = value; return true; } if (smarty.CanBeInt32 && parameterType.IsAssignableFrom(_int32Type)) { convertedValue = (int)(double)value; return true; } if (smarty.CanBeInt64 && parameterType.IsAssignableFrom(_int64Type)) { convertedValue = (long)(double)value; return true; } if (parameterType.IsArray && smarty.CanBeArray() && parameterType.IsAssignableFrom(smarty.ArrayType)) { convertedValue = smarty.TypedArray; return true; } if (parameterType.IsArray && smarty.IsEmptyArray) { Type itemType = Type.GetType(parameterType.FullName.Substring(0, parameterType.FullName.Length - 2)); convertedValue = ((ArrayList)smarty.Value).ToArray(itemType); return true; } if (parameterType.IsEnum && smarty.CanBeInt32 && Enum.GetUnderlyingType(parameterType).IsAssignableFrom(_int32Type)) { convertedValue = Enum.Parse(parameterType, ((int)(double)value).ToString()); return true; } if (smarty.CanBeClass() && parameterType.IsAssignableFrom(smarty.ClassType)) { convertedValue = smarty; return true; } convertedValue = null; return false; }
private bool IsMethodAMatch(ParameterInfo[] parameters, List<SmartObject> inputs, out ArrayList newInputs) { newInputs = new ArrayList(inputs.Count); List<int> classIndices = new List<int>(); if (parameters.Length != inputs.Count) { return false; } if (parameters.Length == 0 && inputs.Count == 0) { return true; } for (int i = 0; i < inputs.Count; ++i) { object value; if (!IsParameterMatch(parameters[i].ParameterType, inputs[i], out value)) { return false; } if (value is SmartObject) { classIndices.Add(i); } newInputs.Add(value); } //we have a match, now create any objects that foreach (int index in classIndices) { SmartObject smarty = (SmartObject)newInputs[index]; Type type = smarty.ClassType; Hashtable hash = (Hashtable)smarty.Value; object o = Activator.CreateInstance(type); foreach (string key in hash.Keys) { PropertyInfo property = type.GetProperty(key); SmartObject smartObject = new SmartObject(hash[key]); object value; if (property != null && IsParameterMatch(property.PropertyType, smartObject, out value)) { property.SetValue(o, value, null); } } newInputs[index] = o; } return true; }