/// <summary> /// Creates the IPC request. /// </summary> /// <param name="methodName">Name of the method.</param> /// <param name="args">The arguments to the method.</param> /// <returns>IpcRequest object</returns> protected static IpcRequest CreateIpcRequest(string methodName, params object[] args) { MethodBase method = null; // Try to find the matching method based on name and args try { if (args.All(x => x != null)) { method = typeof(FactoryOrchestratorClient).GetMethod(methodName, args.Select(x => x.GetType()).ToArray()); } if (method == null) { method = typeof(FactoryOrchestratorClient).GetMethod(methodName); } } catch (Exception) { method = null; } if (method == null) { // Multiple methods with the same name were found or no method was found, try to find the unique method via stack trace var frame = new StackTrace().GetFrames().Where(x => x.GetMethod()?.Name == methodName); if (!frame.Any()) { throw new Exception(string.Format(CultureInfo.CurrentCulture, Resources.NoMethodFound, methodName)); } if (frame.Count() > 1) { throw new Exception(string.Format(CultureInfo.CurrentCulture, Resources.TooManyMethodsFound, methodName)); } method = frame.First().GetMethod(); } var methodParams = method.GetParameters(); var parameterTypes = new IpcRequestParameterType[methodParams.Length]; for (int i = 0; i < methodParams.Length; i++) { parameterTypes[i] = new IpcRequestParameterType(methodParams[i].ParameterType); } var request = new IpcRequest() { MethodName = methodName, Parameters = args, ParameterTypesByName = parameterTypes }; return(request); }
private static MethodBase GetUserCallingMethod(bool captureFilenames = false) { var currentAssembly = typeof(ReactView).Assembly; var callstack = new StackTrace(captureFilenames).GetFrames().Select(f => f.GetMethod()).Where(m => m.ReflectedType.Assembly != currentAssembly); var userMethod = callstack.First(m => !IsFrameworkAssemblyName(m.ReflectedType.Assembly.GetName().Name)); if (userMethod == null) { throw new InvalidOperationException("Unable to find calling method"); } return(userMethod); }
private string GetAssemblyName() { var currentAssembly = Assembly.GetExecutingAssembly(); var callerAssemblies = new StackTrace().GetFrames() .Select(x => x.GetMethod().ReflectedType.Assembly).Distinct() .Where(x => x.GetReferencedAssemblies().Any(y => y.FullName == currentAssembly.FullName)); var initialAssembly = callerAssemblies.First(); return(initialAssembly.Location); }