代码改变世界

【备忘】ASP.NET MVC 2 是怎么样支持 ASP.NET 4 的自动 HTML 转移的 —— TypeBuilder 的利用

  Nana's Lich  阅读(596)  评论(0编辑  收藏  举报

http://msdn.microsoft.com/en-us/library/dd394709.aspx

http://aspnet.codeplex.com/releases/view/41742

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
        // in .NET 4, we dynamically create a type that subclasses MvcHtmlString and implements IHtmlString
        private static MvcHtmlStringCreator GetCreator() {
            Type iHtmlStringType = typeof(HttpContext).Assembly.GetType("System.Web.IHtmlString");
            if (iHtmlStringType != null) {
                // first, create the dynamic type
                Type dynamicType = DynamicTypeGenerator.GenerateType("DynamicMvcHtmlString", typeof(MvcHtmlString), new Type[] { iHtmlStringType });
 
                // then, create the delegate to instantiate the dynamic type
                ParameterExpression valueParamExpr = Expression.Parameter(typeof(string), "value");
                NewExpression newObjExpr = Expression.New(dynamicType.GetConstructor(new Type[] { typeof(string) }), valueParamExpr);
                Expression<MvcHtmlStringCreator> lambdaExpr = Expression.Lambda<MvcHtmlStringCreator>(newObjExpr, valueParamExpr);
                return lambdaExpr.Compile();
            }
            else {
                // disabling 0618 allows us to call the MvcHtmlString() constructor
#pragma warning disable 0618
                return value => new MvcHtmlString(value);
#pragma warning restore 0618
            }
        }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
namespace System.Web.Mvc {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Reflection.Emit;
    using System.Security;
 
    internal static class DynamicTypeGenerator {
 
        private static readonly ModuleBuilder _dynamicModule = CreateDynamicModule();
 
        private static ModuleBuilder CreateDynamicModule() {
            // DDB 226615 - since MVC is [SecurityTransparent], the dynamic assembly must declare itself likewise
            CustomAttributeBuilder builder = new CustomAttributeBuilder(
                typeof(SecurityTransparentAttribute).GetConstructor(Type.EmptyTypes), new object[0]);
            CustomAttributeBuilder[] assemblyAttributes = new CustomAttributeBuilder[] { builder };
            AssemblyBuilder dynamicAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(
                new AssemblyName("System.Web.Mvc.{Dynamic}"), AssemblyBuilderAccess.Run, assemblyAttributes);
            ModuleBuilder dynamicModule = dynamicAssembly.DefineDynamicModule("System.Web.Mvc.{Dynamic}.dll");
            return dynamicModule;
        }
 
        // Creates a new dynamic type that is a subclassed type of baseType and also implements methods of the specified
        // interfaces. The base type must already have method signatures that implicitly implement the given
        // interfaces. The signatures of all public (e.g. not private / internal) constructors from the baseType
        // will be duplicated for the subclassed type and the new constructors made public.
        public static Type GenerateType(string dynamicTypeName, Type baseType, IEnumerable<Type> interfaceTypes) {
            TypeBuilder newType = _dynamicModule.DefineType(
                "System.Web.Mvc.{Dynamic}." + dynamicTypeName,
                TypeAttributes.AutoLayout | TypeAttributes.Public | TypeAttributes.Class,
                baseType);
 
            foreach (Type interfaceType in interfaceTypes) {
                newType.AddInterfaceImplementation(interfaceType);
                foreach (MethodInfo interfaceMethod in interfaceType.GetMethods()) {
                    ImplementInterfaceMethod(newType, interfaceMethod);
                }
            }
 
            // generate new constructors for each accessible base constructor
            foreach (ConstructorInfo ctor in baseType.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
                switch (ctor.Attributes & MethodAttributes.MemberAccessMask) {
                    case MethodAttributes.Family:
                    case MethodAttributes.Public:
                    case MethodAttributes.FamORAssem:
                        ImplementConstructor(newType, ctor);
                        break;
                }
            }
 
            Type bakedType = newType.CreateType();
            return bakedType;
        }
 
        // generates this constructor:
        // public NewType(param0, param1, ...) : base(param0, param1, ...) { }
        private static void ImplementConstructor(TypeBuilder newType, ConstructorInfo baseCtor) {
            ParameterInfo[] parameters = baseCtor.GetParameters();
            Type[] parameterTypes = (from p in parameters select p.ParameterType).ToArray();
 
            ConstructorBuilder newCtor = newType.DefineConstructor(
                (baseCtor.Attributes & ~MethodAttributes.MemberAccessMask) | MethodAttributes.Public /* force public constructor */,
                baseCtor.CallingConvention, parameterTypes);
 
            // parameter 0 is 'this', so we start at index 1
            for (int i = 0; i < parameters.Length; i++) {
                newCtor.DefineParameter(i + 1, parameters[i].Attributes, parameters[i].Name);
            }
 
            // load all arguments (including 'this') in proper order, then call and return
            ILGenerator ilGen = newCtor.GetILGenerator();
            for (int i = 0; i <= parameterTypes.Length; i++) {
                ilGen.Emit(OpCodes.Ldarg_S, (byte)i);
            }
            ilGen.Emit(OpCodes.Call, baseCtor);
            ilGen.Emit(OpCodes.Ret);
        }
 
        // generates this explicit interface method:
        // public new Interface.Method(param0, param1, ...) {
        //   return base.Method(param0, param1, ...);
        // }
        private static void ImplementInterfaceMethod(TypeBuilder newType, MethodInfo interfaceMethod) {
            ParameterInfo[] parameters = interfaceMethod.GetParameters();
            Type[] parameterTypes = (from p in parameters select p.ParameterType).ToArray();
 
            // based on http://msdn.microsoft.com/en-us/library/system.reflection.emit.typebuilder.definemethodoverride.aspx
            MethodBuilder newMethod = newType.DefineMethod(interfaceMethod.DeclaringType.Name + "." + interfaceMethod.Name,
                MethodAttributes.Private | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual | MethodAttributes.Final,
                interfaceMethod.ReturnType, parameterTypes);
 
            MethodInfo baseMethod = newType.BaseType.GetMethod(interfaceMethod.Name, parameterTypes);
 
            // parameter 0 is 'this', so we start at index 1
            for (int i = 0; i < parameters.Length; i++) {
                newMethod.DefineParameter(i + 1, parameters[i].Attributes, parameters[i].Name);
            }
 
            // load all arguments (including 'this') in proper order, then call and return
            ILGenerator ilGen = newMethod.GetILGenerator();
            for (int i = 0; i <= parameterTypes.Length; i++) {
                ilGen.Emit(OpCodes.Ldarg_S, (byte)i);
            }
            ilGen.Emit(OpCodes.Call, baseMethod);
            ilGen.Emit(OpCodes.Ret);
 
            // finally, hook the new method up to the interface mapping
            newType.DefineMethodOverride(newMethod, interfaceMethod);
        }
 
    }
}

 

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
点击右上角即可分享
微信分享提示