Пример #1
0
        private void ProcessTypes(Type t, ScriptClassAttribute attribute, ScriptEngine engine)
        {
            var publicStaticMethods = t.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
                                      .Where(f => f.CustomAttributes.Any(x => (x.AttributeType == typeof(ScriptMethodAttribute) || x.AttributeType == typeof(ScriptPropertyAttribute)) && Delegate.CreateDelegate(typeof(ScriptEngine.ScriptCallbackCB), f) != null));

            foreach (var method in publicStaticMethods)
            {
                var methodAttributes   = method.GetCustomAttributes <ScriptMethodAttribute>();
                var propertyAttributes = method.GetCustomAttributes <ScriptPropertyAttribute>();

                foreach (var methodAttribute in methodAttributes)
                {
                    var name = methodAttribute.AppearAtRoot ? methodAttribute.MethodName : string.Format("{0}.{1}", attribute.ClassName, methodAttribute.MethodName);

                    var definition = "function " + name + "(";

                    if (methodAttribute.MethodParameters != null)
                    {
                        var isFirst = true;
                        foreach (var param in methodAttribute.MethodParameters)
                        {
                            if (!isFirst)
                            {
                                definition += ",";
                            }
                            isFirst     = false;
                            definition += param;
                        }
                    }

                    definition += ")";


                    engine.AddNative(definition, (ScriptEngine.ScriptCallbackCB)Delegate.CreateDelegate(typeof(ScriptEngine.ScriptCallbackCB), method), engine);
                }

                foreach (var propertyAttribute in propertyAttributes)
                {
                    var name = propertyAttribute.AppearAtRoot ? propertyAttribute.PropertyName : string.Format("{0}.{1}", attribute.ClassName, propertyAttribute.PropertyName);

                    engine.AddNativeProperty(name, (ScriptEngine.ScriptCallbackCB)Delegate.CreateDelegate(typeof(ScriptEngine.ScriptCallbackCB), method), engine);
                }
            }
        }