Dynamically create the delegate type to reduce complexity in the caller

avinationmerge
Melanie 2012-03-26 01:21:44 +01:00
parent 4b76c52c41
commit 5e8d64c943
2 changed files with 23 additions and 2 deletions

View File

@ -26,6 +26,7 @@
*/
using System;
using System.Reflection;
using OpenMetaverse;
namespace OpenSim.Region.Framework.Interfaces
@ -45,7 +46,7 @@ namespace OpenSim.Region.Framework.Interfaces
/// </summary>
event ScriptCommand OnScriptCommand;
void RegisterScriptInvocation(Delegate fn);
void RegisterScriptInvocation(object target, MethodInfo mi);
Delegate[] GetScriptInvocationList();
Delegate LookupScriptInvocation(string fname);

View File

@ -35,6 +35,8 @@ using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Framework.Scenes;
using Mono.Addins;
using OpenMetaverse;
using System.Linq;
using System.Linq.Expressions;
namespace OpenSim.Region.CoreModules.Scripting.ScriptModuleComms
{
@ -126,8 +128,26 @@ namespace OpenSim.Region.CoreModules.Scripting.ScriptModuleComms
m_scriptModule.PostScriptEvent(script, "link_message", args);
}
public void RegisterScriptInvocation(Delegate fcall)
public void RegisterScriptInvocation(object target, MethodInfo mi)
{
Type delegateType;
var typeArgs = mi.GetParameters()
.Select(p => p.ParameterType)
.ToList();
if (mi.ReturnType == typeof(void))
{
delegateType = Expression.GetActionType(typeArgs.ToArray());
}
else
{
typeArgs.Add(mi.ReturnType);
delegateType = Expression.GetFuncType(typeArgs.ToArray());
}
Delegate fcall = Delegate.CreateDelegate(delegateType, target, mi);
lock (m_scriptInvocation)
{
ParameterInfo[] parameters = fcall.Method.GetParameters ();