コード例 #1
0
 protected override object OnCalc(IList <object> operands)
 {
     if (operands.Count > 2)
     {
         string module = operands[0] as string;
         string type   = operands[1] as string;
         string method = operands[2] as string;
         if (!string.IsNullOrEmpty(module) && !string.IsNullOrEmpty(type) && !string.IsNullOrEmpty(method))
         {
             TypeDefinition td = null;
             TypeReference  tr;
             if (ScriptProcessor.GetModuleDefinition().TryGetTypeReference(type, out tr))
             {
                 try {
                     td = tr.Resolve();
                 } catch (Exception ex) {
                     ScriptProcessor.ErrorTxts.Add(string.Format("inject '{0}.{1}' exception:{2}\n{3}", type, method, ex.Message, ex.StackTrace));
                 }
             }
             else
             {
                 var md = ModuleDefinition.ReadModule(module);
                 if (null != md)
                 {
                     td = md.GetType(type);
                 }
                 else
                 {
                     ScriptProcessor.ErrorTxts.Add(string.Format("inject can't read module '{0}'", module));
                 }
             }
             if (null != td)
             {
                 bool handled = false;
                 foreach (var m in td.Methods)
                 {
                     if (m.Name == method)
                     {
                         bool memoryLog  = 2 == m.Parameters.Count && 0 == string.Compare(m.Parameters[0].ParameterType.FullName, "System.Int32", true) && 0 == string.Compare(m.Parameters[1].ParameterType.FullName, "System.String", true);
                         bool callHook   = 2 == m.Parameters.Count && 0 == string.Compare(m.Parameters[0].ParameterType.FullName, "System.String", true) && 0 == string.Compare(m.Parameters[1].ParameterType.FullName, "System.Object[]", true);
                         bool returnHook = 3 == m.Parameters.Count && 0 == string.Compare(m.Parameters[0].ParameterType.FullName, "System.Object", true) && 0 == string.Compare(m.Parameters[1].ParameterType.FullName, "System.String", true) && 0 == string.Compare(m.Parameters[2].ParameterType.FullName, "System.Object[]", true);
                         if (memoryLog || callHook || returnHook)
                         {
                             handled = true;
                             try {
                                 var mr = new MethodReference(m.Name, m.ReturnType, m.DeclaringType);
                                 foreach (var p in m.Parameters)
                                 {
                                     mr.Parameters.Add(p);
                                 }
                                 var methodRef = ScriptProcessor.GetModuleDefinition().ImportReference(mr);
                                 ScriptProcessor.InjectPrologue(methodRef);
                             } catch (Exception ex) {
                                 ScriptProcessor.ErrorTxts.Add(string.Format("inject '{0}.{1}' exception:{2}\n{3}", type, method, ex.Message, ex.StackTrace));
                             }
                             break;
                         }
                     }
                 }
                 if (!handled)
                 {
                     ScriptProcessor.ErrorTxts.Add(string.Format("inject can't find method '{0}' from {1}", method, type));
                 }
             }
             else
             {
                 ScriptProcessor.ErrorTxts.Add(string.Format("inject can't find type '{0}' from {1}", type, module));
             }
         }
     }
     else if (operands.Count == 2)
     {
         var    type   = operands[0] as Type;
         string method = operands[1] as string;
         if (null != type && !string.IsNullOrEmpty(method))
         {
             var m = type.GetMethod(method, new Type[] { typeof(string) });
             if (null != m)
             {
                 try {
                     var methodRef = ScriptProcessor.GetModuleDefinition().ImportReference(m);
                     ScriptProcessor.InjectPrologue(methodRef);
                 } catch (Exception ex) {
                     ScriptProcessor.ErrorTxts.Add(string.Format("inject '{0}.{1}' exception:{2}\n{3}", type.FullName, method, ex.Message, ex.StackTrace));
                 }
             }
             else
             {
                 ScriptProcessor.ErrorTxts.Add(string.Format("inject can't find method '{0}' from {1}", method, type.FullName));
             }
         }
     }
     return(0);
 }