/// <summary>
 /// Registers public static
 /// </summary>
 /// <param name="type">the type on which accessible methods are registered</param>
 public static void RegisterClass(Type type)
 {
     string[]     names;
     MethodInfo[] methods = ExternalMethodAttribute.GetMethods(type, out names);
     for (int i = 0; i < names.Length; i++)
     {
         if (!expressionMethods.TryAdd(names[i], methods[i]))
         {
             LogEnvironment.LogDebugEvent(null,
                                          string.Format(
                                              "Failed to register method {0} because the method has already been registered with the same signature",
                                              names[i]), (int)LogSeverity.Warning, "Scripting");
         }
     }
 }
示例#2
0
        /// <summary>
        /// Gets methods marked with the ExternalMethodAttribute  from a specified type
        /// </summary>
        /// <param name="type">the type on which methods are marked as ExternalMethods</param>
        /// <returns>an array containing usable methods</returns>
        public static MethodInfo[] GetMethods(Type type, out string[] methodNames)
        {
            List <MethodInfo> retVal = new List <MethodInfo>();
            List <string>     names  = new List <string>();

            MethodInfo[] methods = type.GetMethods(BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public);
            (from t in methods where t.GetCustomAttributes(typeof(ExternalMethodAttribute), true).Length != 0 select t)
            .ToList().ForEach(
                (n) =>
            {
                Type[] t = n.GetGenericArguments();
                if (t.Length != 0)
                {
                    throw new InvalidOperationException("Generic methods are not supported!");
                }

                ExternalMethodAttribute ema =
                    n.GetCustomAttributes(typeof(ExternalMethodAttribute), true)[0] as ExternalMethodAttribute;
                retVal.Add(n);
                names.Add(string.IsNullOrEmpty(ema.MappedMethodName) ? n.Name : ema.MappedMethodName);
            });
            methodNames = names.ToArray();
            return(retVal.ToArray());
        }