Ejemplo n.º 1
0
        public ILDynamicMethodDebugImpl(string name, Type delegateType, Type thisType)
        {
            _delegateType   = delegateType;
            _expectedLength = 64;
            var mi         = delegateType.GetMethod("Invoke");
            var uniqueName = ILDynamicTypeDebugImpl.UniqueName(name, 259 - (DynamicILDirectoryPath.DynamicIL.Length + 1 + 4));

            _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(uniqueName), AssemblyBuilderAccess.RunAndSave, DynamicILDirectoryPath.DynamicIL);
            _moduleBuilder   = _assemblyBuilder.DefineDynamicModule(uniqueName + ".dll", true);
            var sourceCodeFileName = Path.Combine(DynamicILDirectoryPath.DynamicIL, uniqueName + ".il");

            _symbolDocumentWriter = _moduleBuilder.DefineDocument(sourceCodeFileName, SymDocumentType.Text, SymLanguageType.ILAssembly, SymLanguageVendor.Microsoft);
            _sourceCodeWriter     = new SourceCodeWriter(sourceCodeFileName, _symbolDocumentWriter);
            Type[] parameterTypes;
            if (thisType != null)
            {
                parameterTypes = new[] { thisType }.Concat(mi.GetParameters().Select(pi => pi.ParameterType)).ToArray();
            }
            else
            {
                parameterTypes = mi.GetParameters().Select(pi => pi.ParameterType).ToArray();
            }
            _sourceCodeWriter.StartMethod(name, mi.ReturnType, parameterTypes, MethodAttributes.Static);
            _typeBuilder          = _moduleBuilder.DefineType(name, TypeAttributes.Public, typeof(object), Type.EmptyTypes);
            _forbidenInstructions = new ILGenForbidenInstructionsCheating(_typeBuilder);
            _dynamicMethod        = _typeBuilder.DefineMethod("Invoke", MethodAttributes.Public | MethodAttributes.Static, mi.ReturnType, parameterTypes);
            for (int i = 0; i < parameterTypes.Length; i++)
            {
                _dynamicMethod.DefineParameter(i + 1, ParameterAttributes.In, $"arg{i}");
            }
        }
Ejemplo n.º 2
0
        public Type NewEnum(string name, Type baseType, IEnumerable <KeyValuePair <string, object> > literals)
        {
            name = ILDynamicTypeDebugImpl.UniqueName(name);
            var ab          = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(name), AssemblyBuilderAccess.Run);
            var mb          = ab.DefineDynamicModule(name, true);
            var enumBuilder = mb.DefineEnum(name, TypeAttributes.Public, baseType);

            foreach (var pair in literals)
            {
                enumBuilder.DefineLiteral(pair.Key, pair.Value);
            }
            return(enumBuilder.CreateType());
        }