<commit_before>﻿using System;
using System.Linq;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.Remoting;
using System.Text;
using System.Threading;
using System.Collections.Generic;

// TODO: Localize exceptions and trace messages
namespace Zyan.Communication.Toolbox
{
	/// <summary>
	/// Various extension methods.
	/// </summary>
	public static partial class Extensions
	{
		/// <summary>
		/// Converts exception and all of its inner exceptions to string
		/// </summary>
		public static string Dump(this Exception ex, string title)
		{
			var sb = new StringBuilder(title);

			while (ex != null)
			{
				sb.AppendFormat("{0} ({1})", ex.Message, ex.GetType());
				sb.AppendLine();
				sb.AppendLine("Stack trace:");
				sb.AppendLine(ex.StackTrace);

				ex = ex.InnerException;
				if (ex != null)
				{
					sb.AppendLine();
					sb.Append("Inner exception: ");
				}
			}

			return sb.ToString();
		}

		/// <summary>
		/// Returns true if method in one-way
		/// </summary>
		public static bool IsOneWay(this MethodInfo mi)
		{
			// check for OneWay attribute
			if (!RemotingServices.IsOneWay(mi))
			{
				return false;
			}

			// return type should be void
			if (mi.ReturnType != typeof(void))
			{
				Trace.WriteLine("Warning: non-void method is decorated with OneWay attribute: " + mi);
				return false;
			}

			// ref and out parameters are not allowed
			foreach (var arg in mi.GetParameters())
			{
				if (arg.IsOut || arg.ParameterType.IsByRef)
				{
					Trace.WriteLine("Warning: ret and out parameters are not allowed in OneWay method: " + mi);
					return false;
				}
			}

			return true;
		}

		/// <summary>
		/// Invokes the method represented by current instance using the supplied parameters in either one-way or normal mode
		/// </summary>
		public static object Invoke(this MethodInfo mi, object instance, object[] args, bool oneWay)
		{
			if (oneWay)
			{
				ThreadPool.QueueUserWorkItem(t =>
				{
					try
					{
						// one-way method cannot throw any exceptions, so we ignore any
						mi.Invoke(instance, args);
					}
					catch (Exception ex)
					{
						Trace.WriteLine(ex.Dump("Unhandled exception in one-way method: "));
					}
				});

				return null;
			}

			return mi.Invoke(instance, args);
		}

		/// <summary>
		/// Creates invocation delegate for the static method represented by the given MethodInfo.
		/// </summary>
		public static T CreateDelegate<T>(this MethodInfo method) where T : class<commit_msg>[en] Fixed CurrentSession in OneWay methods, work item #1363.<commit_after>﻿using System;
using System.Linq;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.Remoting;
using System.Text;
using System.Threading;
using System.Collections.Generic;

// TODO: Localize exceptions and trace messages
namespace Zyan.Communication.Toolbox
{
	/// <summary>
	/// Various extension methods.
	/// </summary>
	public static partial class Extensions
	{
		/// <summary>
		/// Converts exception and all of its inner exceptions to string
		/// </summary>
		public static string Dump(this Exception ex, string title)
		{
			var sb = new StringBuilder(title);

			while (ex != null)
			{
				sb.AppendFormat("{0} ({1})", ex.Message, ex.GetType());
				sb.AppendLine();
				sb.AppendLine("Stack trace:");
				sb.AppendLine(ex.StackTrace);

				ex = ex.InnerException;
				if (ex != null)
				{
					sb.AppendLine();
					sb.Append("Inner exception: ");
				}
			}

			return sb.ToString();
		}

		/// <summary>
		/// Returns true if method in one-way
		/// </summary>
		public static bool IsOneWay(this MethodInfo mi)
		{
			// check for OneWay attribute
			if (!RemotingServices.IsOneWay(mi))
			{
				return false;
			}

			// return type should be void
			if (mi.ReturnType != typeof(void))
			{
				Trace.WriteLine("Warning: non-void method is decorated with OneWay attribute: " + mi);
				return false;
			}

			// ref and out parameters are not allowed
			foreach (var arg in mi.GetParameters())
			{
				if (arg.IsOut || arg.ParameterType.IsByRef)
				{
					Trace.WriteLine("Warning: ret and out parameters are not allowed in OneWay method: " + mi);
					return false;
				}
			}

			return true;
		}

		/// <summary>
		/// Invokes the method represented by current instance using the supplied parameters in either one-way or normal mode
		/// </summary>
		public static object Invoke(this MethodInfo mi, object instance, object[] args, bool oneWay)
		{
			if (oneWay)
			{
				// save thread-static CurrentSession variable
				var savedSession = ServerSession.CurrentSession;

				ThreadPool.QueueUserWorkItem(t =>
				{
					// restore current session in a new worker thread
					ServerSession.CurrentSession = savedSession;

					try
					{
						// one-way method cannot throw any exceptions, so we ignore any
						mi.Invoke(instance, args);
					}
					catch (Exception ex)
					{
						Trace.WriteLine(ex.Dump("Unhandled exception in one-way method: "));
					}
				});

				return null;
			}

			return mi.Invoke(instance, args);
		}

		/// <summary>
		/// Creates invocation delegate for the static method represented by the given MethodInfo.
		/// </summary>
		public static T CreateDelegate<T>(this MethodInfo method) where T : class
{'fraction_non_alphanumeric': 0.11686334221545489, 'fraction_numerical': 0.0015226494099733537, 'mean_word_length': 2.7914862914862915, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3393258', 'n_tokens_mistral': 1932, 'n_tokens_neox': 1524, 'n_words': 494}