Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undecompilable static constructor involving string field #111

Closed
Twinside opened this issue Sep 17, 2019 · 0 comments
Closed

Undecompilable static constructor involving string field #111

Twinside opened this issue Sep 17, 2019 · 0 comments
Labels
bug Something isn't working

Comments

@Twinside
Copy link
Collaborator

Twinside commented Sep 17, 2019

The following program:

using System;
using System.Reflection;
using System.Reflection.Emit;
using Lokad.ILPack;

namespace ilpack_repro
{
    sealed class Program
    {
        static void Main(string[] args)
        {
            var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(
                new AssemblyName { Name = "Jot" },
                AssemblyBuilderAccess.Run);

            var moduleBuilder = assemblyBuilder.DefineDynamicModule("JotModule");

            var myType = moduleBuilder.DefineType(
                "Test.Type",
                TypeAttributes.Public | TypeAttributes.BeforeFieldInit,
                typeof(object),
                Array.Empty<Type>());

            var originalNameField = myType.DefineField(
                fieldName: "StringField",
                type: typeof(string),
                attributes: FieldAttributes.Public
                            | FieldAttributes.Static
                            | FieldAttributes.InitOnly);

            var ctorBuilder = myType.DefineConstructor(
                attributes: MethodAttributes.Public | MethodAttributes.Static,
                callingConvention: CallingConventions.Standard,
                parameterTypes: Array.Empty<Type>());

            var il = ctorBuilder.GetILGenerator();
            il.Emit(OpCodes.Ldstr, "foobar");
            il.Emit(OpCodes.Stfld, originalNameField);
            il.Emit(OpCodes.Ret);


            var finalType = myType.CreateType();

            new AssemblyGenerator().GenerateAssembly(moduleBuilder.Assembly, "C:\\LokadData\\test.dll");
        }
    }
}

generates an assembly where various disassembler (dotnetpeek & ilspy) choke on, there is likely something weird behind it.

@Twinside Twinside added the bug Something isn't working label Sep 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant