Esempio n. 1
0
        //----------------------------------//

        /// <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));
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Add a new global function.
 /// </summary>
 public void Add(JsMethod jsMethod)
 {
     _last = jsMethod;
     Components.Add(jsMethod);
 }
Esempio n. 3
0
 /// <summary>
 /// Add a raw string as javascript.
 /// </summary>
 public void Add(string command)
 {
     _last = null;
     Components.Add(new JsCommandString(command));
 }
Esempio n. 4
0
 /// <summary>
 /// Add a custom command to the builder.
 /// </summary>
 public void Add(JsCommand command)
 {
     _last = command;
     Components.Add(command);
 }
Esempio n. 5
0
 /// <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));
 }