/// <summary> /// Initializes a new instance of the <see cref="WeakAction"/> class. /// </summary> /// <param name="target">The target.</param> /// <param name="action">The action.</param> /// <exception cref="ArgumentNullException">The <paramref name="action"/> is <c>null</c>.</exception> /// <exception cref="NotSupportedException">The <paramref name="action"/> is an anonymous delegate.</exception> public WeakAction(object target, Action action) : base(target) { Argument.IsNotNull("action", action); #if NETFX_CORE || PCL Log.Warning("Since WinRT won't allow specific reflection, the WeakAction is implemented as regular action"); _action = action; #else MethodName = action.Method.ToString(); if (MethodName.Contains("_AnonymousDelegate>")) { const string error = "Anonymous delegates are not supported because they are located in a private class"; Log.Error(error); throw new NotSupportedException(error); } var targetType = (target != null) ? target.GetType() : typeof(object); var delegateType = typeof(OpenInstanceAction <>).MakeGenericType(targetType); _action = DelegateHelper.CreateDelegate(delegateType, action.Method); #endif }
/// <summary> /// Initializes a new instance of the <see cref="WeakAction"/> class. /// </summary> /// <param name="target">The target.</param> /// <param name="func">The action.</param> /// <exception cref="ArgumentNullException">The <paramref name="func"/> is <c>null</c>.</exception> /// <exception cref="NotSupportedException">The <paramref name="func"/> is an anonymous delegate.</exception> public WeakFunc(object target, Func <TResult> func) : base(target) { Argument.IsNotNull("action", func); var methodInfo = func.GetMethodInfoEx(); MethodName = methodInfo.ToString(); if (MethodName.Contains("_AnonymousDelegate>")) { throw Log.ErrorAndCreateException <NotSupportedException>("Anonymous delegates are not supported because they are located in a private class"); } var targetType = target?.GetType() ?? typeof(object); var delegateType = typeof(OpenInstanceAction <>).MakeGenericType(typeof(TResult), targetType); _action = DelegateHelper.CreateDelegate(delegateType, methodInfo); }
public async Task <T> PostAsync <T>(params object[] parameters) { MethodName = parameters[0] as string; RestRequest = new RestRequest(MethodName, DataFormat.Json); if (parameters.Length > 1) { RestRequest.AddJsonBody(parameters[1]); } var postResponse = await RestClient.ExecuteAsync <T>(RestRequest, Method.POST); if (MethodName.Contains("Login")) { LoginCookies = postResponse.Cookies; } var jsonResult = JsonConvert.DeserializeObject <T>(postResponse.Content); return(jsonResult); }
internal MethodInfo ResolveMethod() { if (String.IsNullOrEmpty(MethodName)) { throw new InvalidOperationException(AtlasWeb.MethodExpression_MethodNameMustBeSpecified); } MethodInfo methodInfo = null; // We allow the format string {0} in the method name IDynamicDataSource dataSource = DataSource as IDynamicDataSource; if (dataSource != null) { MethodName = String.Format(CultureInfo.CurrentCulture, MethodName, dataSource.EntitySetName); } else if (MethodName.Contains("{0}")) { // If method has a format string but no IDynamicDataSource then throw an exception throw new InvalidOperationException(AtlasWeb.MethodExpression_DataSourceMustBeIDynamicDataSource); } foreach (Func <Type> typeGetter in typeGetters) { Type type = typeGetter(); // If the type is null continue to next fall back function if (type == null) { continue; } methodInfo = type.GetMethod(MethodName, MethodFlags); if (methodInfo != null) { break; } } return(methodInfo); }