Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: add function to scriptengine

0
0
publicstaticclass ModuleLoader
    {
        publicdelegateobject MethodFunc(object target, paramsobject[] args);

        publicstaticvoid Load(WindowsScriptEngine se, Type t)
        {
            var ca = t.GetCustomAttribute<ScriptModuleAttribute>();

            var tmp = Activator.CreateInstance(t);

            if (ca != null)
            {
                if (ca.AsType)
                {
                    se.AddHostType(ca.Name != null ? ca.Name : t.Name, t);
                }
                foreach (var me in t.GetMethods())
                {
                    var meca = me.GetCustomAttribute<ScriptFunctionAttribute>();
                    if (meca != null)
                    {
                        var mf = new MethodFunc(me.Invoke);
                        se.AddHostObject(meca.Name != null ? meca.Name : me.Name, mf);
                    }
                }
            }
            foreach (var me in t.GetProperties())
            {
                var meca = me.GetCustomAttribute<ScriptMemberAttribute>();
                if (meca != null)
                {
                    se.AddHostObject(meca.Name != null ? meca.Name : me.Name, me.GetValue(tmp, null));
                }
            }
        }

        publicstaticvoid Load(WindowsScriptEngine se, Assembly ass)
        {
            foreach (var t in ass.GetTypes())
            {
                Load(se, t);
            }
        }
    }

Viewing all articles
Browse latest Browse all 2297

Latest Images

Trending Articles





Latest Images