//----------------------------------// //----------------------------------// internal JsMethod(JsClass jsClass, MethodInfo method) { Parameters = new Dictionary <string, Js>(); Commands = new ArrayRig <JsCommand>(); // does the method return a string? if (method.ReturnType != typeof(string)) { throw new InvalidOperationException("JsMethod '" + method.Name + "' doesn't return a string."); } // iterate the parameters foreach (var parameter in method.GetParameters()) { // ensure each parameter is a Js type if (!Js.IsAncestor(parameter.ParameterType)) { throw new InvalidOperationException("Parameter '" + parameter.Name + "' of method '" + method.Name + "' in class '" + method.DeclaringType.Name + "' doesn't inherit from Js."); } // add the parameter Parameters.Add(parameter.Name, parameter.HasDefaultValue ? (Js)parameter.DefaultValue : null); } // add the string returned from the method as the javascript command Commands.Add(new JsCommandString((string)method.Invoke(jsClass, new object[Parameters.Count]))); }
//----------------------------------// /// <summary> /// Create a template from the specified js obj class. /// </summary> public JsPrototype(JsClass jsClass) { Constructors = new ArrayRig <Dictionary <string, Js> >(); Properties = new Dictionary <string, Js>(); Methods = new Dictionary <string, JsMethod>(); var type = jsClass.GetType(); Name = type.Name; var constructorMethod = type.GetMethod("Constructor", BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly, null, new Type[0], null); if (constructorMethod != null && constructorMethod.ReturnType == typeof(string)) { _constructor = new JsCommandString((string)constructorMethod.Invoke(null, null)); } // iterate class constructors foreach (var constructor in type.GetConstructors()) { var parameters = new Dictionary <string, Js>(); Constructors.Add(parameters); // iterate parameters foreach (var parameter in constructor.GetParameters()) { // is the parameter a JsBuilder? if (parameter.ParameterType == typeof(JsBuilder)) { continue; } // is the type valid? if (!Js.IsAncestor(parameter.ParameterType)) { throw new InvalidOperationException("Parameter '" + parameter.Name + "' in class '" + Name + "' constructor doesn't inherit from Js."); } // add the parameter parameters.Add(parameter.Name, parameter.HasDefaultValue ? (Js)parameter.DefaultValue : null); } } // iterate the class properties foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance)) { var attribute = property.GetCustomAttribute <JsProperty>(true); if (attribute == null) { continue; } if (!Js.IsAncestor(property.PropertyType)) { throw new InvalidOperationException("Property '" + property.Name + "' in class '" + Name + "' doesn't inherit from Js."); } var propertyValue = property.GetValue(jsClass); if (propertyValue == null) { Properties.Add(property.Name, null); } else { Properties.Add(property.Name, (Js)propertyValue); } } // iterate class functions foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance)) { var attribute = method.GetCustomAttribute <JsFunction>(true); if (attribute == null) { continue; } Methods.Add(method.Name, new JsMethod(jsClass, method)); } }
//----------------------------------// //----------------------------------// internal JsCommandNew(JsClass jsObj, Js[] parameters) { Class = jsObj; Parameters = parameters; }
/// <summary> /// Create a new object of the specified type. /// </summary> internal void Add(JsClass jsClass, Js[] parameters) { _last = jsClass; Components.Add(new JsCommandNew(jsClass, parameters)); }