public static class A
{ private static readonly MethodInfo GetServiceInfo; public static IApplicationBuilder My_UseStaticFiles(this IApplicationBuilder app, StaticFileOptions options)
{
//检查参数
if (app == null)
{
throw new ArgumentNullException("app");
}
if (options == null)
{
throw new ArgumentNullException("options");
} var t = Microsoft.Extensions.Options.Options.Create<StaticFileOptions>(options); object[] args = new object[] {
t
};
//执行该方法 会自动注入 该类中所需要的对象 RequestDelegate next
//return builder.UseMiddleware<RequestCultureMiddleware>(); return app.UseMiddleware<StaticFileMiddleware>(args);
} public static IApplicationBuilder My_UseMiddleware<T>(this IApplicationBuilder app, params object[] args)
{
return app.My_UseMiddleware(typeof(T), args);
}
public static IApplicationBuilder My_UseMiddleware(this IApplicationBuilder app, Type middleware, params object[] args)
{
if (typeof(IMiddleware).GetTypeInfo().IsAssignableFrom(middleware.GetTypeInfo()))
{
if (args.Length != )
{
throw new NotSupportedException(My_Resources.FormatException_UseMiddlewareExplicitArgumentsNotSupported(typeof(IMiddleware)));
}
return My_UseMiddlewareInterface(app, middleware);
}
IServiceProvider applicationServices = app.ApplicationServices;
return app.Use
(
delegate (RequestDelegate next)
{
MethodInfo[] array = (from m in middleware.GetMethods(BindingFlags.Instance | BindingFlags.Public)
where string.Equals(m.Name, "Invoke", StringComparison.Ordinal) || string.Equals(m.Name, "InvokeAsync", StringComparison.Ordinal)
select m).ToArray();
if (array.Length > )
{
throw new InvalidOperationException(My_Resources.FormatException_UseMiddleMutlipleInvokes("Invoke", "InvokeAsync"));
}
if (array.Length == )
{
throw new InvalidOperationException(My_Resources.FormatException_UseMiddlewareNoInvokeMethod("Invoke", "InvokeAsync", middleware));
}
MethodInfo methodInfo = array[];
if (!typeof(Task).IsAssignableFrom(methodInfo.ReturnType))
{
throw new InvalidOperationException(My_Resources.FormatException_UseMiddlewareNonTaskReturnType("Invoke", "InvokeAsync", "Task"));
}
ParameterInfo[] parameters = methodInfo.GetParameters();
if (parameters.Length == || parameters[].ParameterType != typeof(HttpContext))
{
throw new InvalidOperationException(My_Resources.FormatException_UseMiddlewareNoParameters("Invoke", "InvokeAsync", "HttpContext"));
}
object[] array2 = new object[args.Length + ];
array2[] = next;
Array.Copy(args, , array2, , args.Length);
object instance = ActivatorUtilities.CreateInstance(app.ApplicationServices, middleware, array2);
if (parameters.Length == )
{
return (RequestDelegate)methodInfo.CreateDelegate(typeof(RequestDelegate), instance);
}
Func<object, HttpContext, IServiceProvider, Task> factory = My_Compile<object>(methodInfo, parameters);
return delegate (HttpContext context)
{
IServiceProvider serviceProvider = context.RequestServices ?? applicationServices;
if (serviceProvider == null)
{
throw new InvalidOperationException(My_Resources.FormatException_UseMiddlewareIServiceProviderNotAvailable("IServiceProvider"));
}
return factory(instance, context, serviceProvider);
};
} );
} private static Func<T, HttpContext, IServiceProvider, Task> My_Compile<T>(MethodInfo methodInfo, ParameterInfo[] parameters)
{
ParameterExpression expression = Expression.Parameter((Type)typeof(HttpContext), "httpContext");
ParameterExpression expression2 = Expression.Parameter((Type)typeof(IServiceProvider), "serviceProvider");
ParameterExpression expression3 = Expression.Parameter((Type)typeof(T), "middleware");
Expression[] expressionArray = new Expression[] { expression };
for (int i = ; i < parameters.Length; i++)
{
Type type = parameters[i].ParameterType;
if (type.IsByRef)
{
throw new NotSupportedException(My_Resources.FormatException_InvokeDoesNotSupportRefOrOutParams("Invoke"));
}
Expression[] expressionArray2 = new Expression[] { (Expression)expression2, (Expression)Expression.Constant(type, (Type)typeof(Type)), (Expression)Expression.Constant(methodInfo.DeclaringType, (Type)typeof(Type)) };
expressionArray[i] = (Expression)Expression.Convert((Expression)Expression.Call(GetServiceInfo, expressionArray2), type);
}
Expression expression4 = (Expression)expression3;
if (methodInfo.DeclaringType != typeof(T))
{
expression4 = (Expression)Expression.Convert(expression4, methodInfo.DeclaringType);
}
ParameterExpression[] expressionArray3 = new ParameterExpression[] { expression3, expression, expression2 };
return Expression.Lambda<Func<T, HttpContext, IServiceProvider, Task>>((Expression)Expression.Call(expression4, methodInfo, expressionArray), expressionArray3).Compile();
} private static IApplicationBuilder My_UseMiddlewareInterface(IApplicationBuilder app, Type middlewareType)
{
return app.Use
(
(RequestDelegate next)
=>
async delegate
(HttpContext context)
{
IMiddlewareFactory middlewareFactory = (IMiddlewareFactory)context.RequestServices.GetService(typeof(IMiddlewareFactory));
if (middlewareFactory == null)
{
throw new InvalidOperationException(My_Resources.FormatException_UseMiddlewareNoMiddlewareFactory(typeof(IMiddlewareFactory)));
}
IMiddleware middleware = middlewareFactory.Create(middlewareType);
if (middleware == null)
{
throw new InvalidOperationException(My_Resources.FormatException_UseMiddlewareUnableToCreateMiddleware(middlewareFactory.GetType(), middlewareType));
}
try
{
await middleware.InvokeAsync(context, next);
}
finally
{
middlewareFactory.Release(middleware);
}
}
);
} } internal static class My_Resources
{
// Fields
private static readonly ResourceManager _resourceManager; internal static string FormatException_UseMiddlewareNoMiddlewareFactory(object p0)
{
return string.Format(CultureInfo.CurrentCulture, GetString("Exception_UseMiddlewareNoMiddlewareFactory"), p0);
} internal static string FormatException_UseMiddlewareUnableToCreateMiddleware(object p0, object p1)
{
return string.Format(CultureInfo.CurrentCulture, GetString("Exception_UseMiddlewareUnableToCreateMiddleware"), p0, p1);
} // Methods
static My_Resources()
{
_resourceManager = new ResourceManager(
"Microsoft.AspNetCore.Http.Abstractions.Resources",
IntrospectionExtensions.GetTypeInfo((Type)typeof(My_Resources)).Assembly);
}
private static string GetString(string name, params string[] formatterNames)
{
string str = _resourceManager.GetString(name);
if (formatterNames != null)
{
for (int i = ; i < formatterNames.Length; i++)
{
str = str.Replace("{" + formatterNames[i] + "}", "{" + ((int)i) + "}");
}
}
return str;
}
internal static string FormatException_UseMiddleMutlipleInvokes(object p0, object p1) =>
string.Format((IFormatProvider)CultureInfo.CurrentCulture, GetString("Exception_UseMiddleMutlipleInvokes", Array.Empty<string>()), p0, p1); internal static string FormatException_UseMiddlewareNoInvokeMethod(object p0, object p1, object p2) =>
string.Format((IFormatProvider)CultureInfo.CurrentCulture, GetString("Exception_UseMiddlewareNoInvokeMethod", Array.Empty<string>()), p0, p1, p2); internal static string FormatException_UseMiddlewareNonTaskReturnType(object p0, object p1, object p2) =>
string.Format((IFormatProvider)CultureInfo.CurrentCulture, GetString("Exception_UseMiddlewareNonTaskReturnType", Array.Empty<string>()), p0, p1, p2); internal static string FormatException_UseMiddlewareNoParameters(object p0, object p1, object p2) =>
string.Format((IFormatProvider)CultureInfo.CurrentCulture, GetString("Exception_UseMiddlewareNoParameters", Array.Empty<string>()), p0, p1, p2); internal static string FormatException_UseMiddlewareIServiceProviderNotAvailable(object p0) =>
string.Format((IFormatProvider)CultureInfo.CurrentCulture, GetString("Exception_UseMiddlewareIServiceProviderNotAvailable", Array.Empty<string>()), p0); internal static string FormatException_UseMiddlewareExplicitArgumentsNotSupported(object p0) =>
string.Format((IFormatProvider)CultureInfo.CurrentCulture, GetString("Exception_UseMiddlewareExplicitArgumentsNotSupported", Array.Empty<string>()), p0); //--
internal static string FormatException_InvokeDoesNotSupportRefOrOutParams(object p0) =>
string.Format((IFormatProvider)CultureInfo.CurrentCulture, GetString("Exception_InvokeDoesNotSupportRefOrOutParams", Array.Empty<string>()), p0);
}

.net core 反编译一小段的更多相关文章

  1. .NET Core 反编译dll源码查看

    一.可以通过JetBrains dotPeek进行反编译 二.可以通过.NET Reflector和VS自带的反编译工具查看

  2. odex反编译dex异常 Cannot locate boot class path file /system/framework/core.odex

      为了将ROM中system/app下的CertInstaller.odex反编译为CertInstaller.dex,输入命令: "java -jar baksmali.jar -x C ...

  3. 嗅探、中间人sql注入、反编译--例说桌面软件安全性问题

    嗅探.中间人sql注入.反编译--例说桌面软件安全性问题 今天这篇文章不准备讲太多理论,讲我最近遇到的一个案例.从技术上讲,这个例子没什么高深的,还有一点狗屎运的成分,但是它又足够典型,典型到我可以讲 ...

  4. 反编译pyinstaller打包的exe安装包

    PyInstaller将Python文件打包为exe后如何反编译(破解源码)以及防止反编译 在这里分享一些技巧和经验给大家.辛苦撰文分享,转载或引用请保留本文作者信息及文章链接. 作者的环境: win ...

  5. aardio + .NET 快速开发独立 EXE 程序,可防 ILSpy 反编译

    简介 aardio 可以非常方便地调用 .NET( 不需要任何复杂的步骤 ). .NET 在 aardio 中很好用,系统自带 .NET 组件以及各种开源 .NET 组件在 aardio 用户中也很受 ...

  6. Android安全攻防战,反编译与混淆技术完全解析(下)

    在上一篇文章当中,我们学习了Android程序反编译方面的知识,包括反编译代码.反编译资源.以及重新打包等内容.通过这些内容我们也能看出来,其实我们的程序并没有那么的安全.可能资源被反编译影响还不是很 ...

  7. Android安全攻防战,反编译与混淆技术完全解析(上)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/49738023 之前一直有犹豫过要不要写这篇文章,毕竟去反编译人家的程序并不是什么值 ...

  8. javap反编译解释外部类直接使用内部类private字段的原理

    2016-07-04 15:56:39 我们都知道: 1.内部类可以直接访问外部类的private字段和方法: 2.非静态内部类持有外部类的引用: 3.外部类可以直接访问内部类的private字段和方 ...

  9. C#防止反编译

    http://blog.csdn.net/wangpei421/article/details/42393095 http://www.cnblogs.com/tianguook/archive/20 ...

随机推荐

  1. HDU 3047 带权并查集 入门题

    Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...

  2. Block Breaker HDU - 6699(深搜,水,写下涨涨记性)

    Problem Description Given a rectangle frame of size n×m. Initially, the frame is strewn with n×m squ ...

  3. python中sort和sorted用法的区别

    Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列 一,最简单的排序 1.使用sort排序 my_list = [3 ...

  4. 监控 Kubernetes 集群应用

    Prometheus的数据指标是通过一个公开的 HTTP(S) 数据接口获取到的,我们不需要单独安装监控的 agent,只需要暴露一个 metrics 接口,Prometheus 就会定期去拉取数据: ...

  5. 怎样通过id属性值获取元素节点

    方法1: 使用document.getElementById(); 方法2: 使用document.querySelector(); document.getElementById("app ...

  6. java方法可变参数研究

    1 问题引出 (1)缘由 最近在研究如何在项目中引入Redis缓存,于是遇到可变参数这个疑惑点,之前没有好好研究过,为了避免项目后期出现问题. (2)项目相关技术 SpringBoot Redis K ...

  7. 使用js输出1000以内的水仙花数

    什么是水仙花数 水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI).自恋数.自幂数.阿姆斯壮数或阿姆斯特 ...

  8. Scala语言面向对象

    apply1. 面向对象的基本概念: 把数据及对数据的操作方法放在一起,作为一个相互依存的整体-----对象,面向对象的三大特征:封装.多态.继承 2. scala类的定义 · class Emplo ...

  9. Delphi 类成员的可见性

  10. 异常-Data truncation: Truncated incorrect DOUBLE value: '-9370.3530-'

    1详细异常日志 9/11/04 17:36:09 ERROR base.SQLHelper: Data truncation: Truncated incorrect DOUBLE value: '- ...