/// <summary> /// Obtain method info based on methodId. /// </summary> public GeneratedMethodInfo GetMethodInfoById(int methodId) { GeneratedMethodInfo value = null; if (_methods.TryGetValue(methodId, ref value)) { return(value); } return(null); }
/// <summary> /// Obtain a proxy method delegate for a dynamic method. /// Obtain only once, when we attach it to the event. /// Generate a new dynamic method; we can *NOT REUSE THEM*, since they work on handling events /// and each event handler must be traceable to the instance that we subscribed it for. /// </summary> public GeneratedMethodInfo GenerateDynamicMethodProxyDelegate(Type delegateType) { GeneratedMethodInfo result = null; try { // Establish delegate parameters. MethodInfo delegateMethodInfo = delegateType.GetMethod("Invoke"); Type[] parameterTypes = ProxyTypeBuilder.GetMethodParametersTypes(delegateMethodInfo); List <Type> parameterTypesFull = new List <Type>(); // First parameter is the sink. parameterTypesFull.Add(typeof(IDynamicProxyMethodSink)); // Remaining parameters. parameterTypesFull.AddRange(parameterTypes); int methodId = PendingDynamicId; DynamicMethod standaloneMethod = new DynamicMethod("DynamicMethod_" + methodId.ToString(), delegateMethodInfo.ReturnType, parameterTypesFull.ToArray(), _moduleBuilder); result = new GeneratedMethodInfo(methodId, standaloneMethod, delegateType); ILGenerator generator = result.StandaloneDynamicMethod.GetILGenerator(); GenerateProxyMethod(generator, result.Id, null, parameterTypes, delegateMethodInfo.ReturnType, _receiveDynamicMethodCall, _receiveDynamicMethodCallAndReturn); lock (_methods) {// We lock the hot swap, since we also use it to identify items, safer this way. _methods[result.Id] = result; } } catch (Exception ex) { #if Matrix_Diagnostics SystemMonitor.OperationError("Failed to generate proxy type for dynamic method.", ex); #endif return(null); } return(result); }