public static IJsonFormatter CreateFromDynamicMethods(DynamicMethod serialize, DynamicMethod deserialize) { var targetType = deserialize.ReturnType; var length1 = TypeArrayHolder.Length1; length1[0] = targetType; if (targetType.IsValueType) { var serializeType = typeof(JsonSerializeValueTypeAction <>).MakeGenericType(length1); var deserializeType = typeof(JsonDeserializeValueTypeFunc <>).MakeGenericType(length1); var formatterType = typeof(DynamicMethodValueTypeFormatter <>).MakeGenericType(length1); var serializeDelegate = serialize.CreateDelegate(serializeType); var deserializeDelegate = deserialize.CreateDelegate(deserializeType); var answer = ActivatorUtility.CreateInstance(formatterType, serializeDelegate, deserializeDelegate) as IJsonFormatter; if (answer is null) { throw new NullReferenceException(targetType.FullName); } return(answer); } else { var serializeType = typeof(JsonSerializeReferenceTypeAction <>).MakeGenericType(length1); var deserializeType = typeof(JsonDeserializeReferenceTypeFunc <>).MakeGenericType(length1); var formatterType = typeof(DynamicMethodReferenceTypeFormatter <>).MakeGenericType(length1); var serializeDelegate = serialize.CreateDelegate(serializeType); var deserializeDelegate = deserialize.CreateDelegate(deserializeType); var answer = ActivatorUtility.CreateInstance(formatterType, serializeDelegate, deserializeDelegate) as IJsonFormatter; if (answer is null) { throw new NullReferenceException(targetType.FullName); } return(answer); } }
/// <summary> /// Creates an instance of <typeparamref name="TResult" /> that implements the <see cref="IDependency"/> interface using a constructor of five parameters. /// </summary> /// <typeparam name="T1">The type of the first parameter of the constructor.</typeparam> /// <typeparam name="T2">The type of the second parameter of the constructor.</typeparam> /// <typeparam name="T3">The type of the third parameter of the constructor.</typeparam> /// <typeparam name="TResult">The type to create that implements the <see cref="IDependency"/> interface.</typeparam> /// <param name="arg1">The first parameter of the constructor.</param> /// <param name="arg2">The second parameter of the constructor.</param> /// <param name="arg3">The third parameter of the constructor.</param> /// <returns>A reference to the newly created object.</returns> public static TResult Create <T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3) where TResult : IDependency { return(ActivatorUtility.CreateInstance <T1, T2, T3, TResult>(arg1, arg2, arg3)); }
/// <summary> /// Creates an instance of <typeparamref name="TResult" /> that implements the <see cref="IDependency"/> interface using a constructor of five parameters. /// </summary> /// <typeparam name="T">The type of the parameter of the constructor.</typeparam> /// <typeparam name="TResult">The type to create that implements the <see cref="IDependency"/> interface.</typeparam> /// <param name="arg">The parameter of the constructor.</param> /// <returns>A reference to the newly created object.</returns> public static TResult Create <T, TResult>(T arg) where TResult : IDependency { return(ActivatorUtility.CreateInstance <T, TResult>(arg)); }
/// <summary> /// Inserts a HTTP related filter to the list at the specified <paramref name="index" />. /// </summary> /// <typeparam name="T">The type of the <see cref="ICacheableAsyncResultFilter"/>.</typeparam> /// <typeparam name="TOptions">The type of delegate setup to configure <typeparamref name="T"/>.</typeparam> /// <param name="filters">The list of cache related HTTP head filters.</param> /// <param name="index">The zero-based index at which a HTTP related filter should be inserted.</param> /// <param name="setup">The <see cref="Action{TOptions}"/> which need to be configured.</param> public static void InsertFilter <T, TOptions>(this IList <ICacheableAsyncResultFilter> filters, int index, Action <TOptions> setup) where T : ICacheableAsyncResultFilter where TOptions : class, new() { filters.Insert(index, ActivatorUtility.CreateInstance <Action <TOptions>, T>(setup)); }