Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8530541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:16:17+00:00 2026-06-11T09:16:17+00:00

Toying with making a compiler for my own language, I’m trying to generate some

  • 0

Toying with making a compiler for my own language, I’m trying to generate some MSIL code using the Reflection.Emit framework. It works fine when using int when I declare local variables. However, when I want to declare a local variable of a type I have not yet compiled I get into trouble since the DeclareLocal() takes a Type as argument. That is my uncompiled class, say A, still needs to be defined using

 assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemName, AssemblyBuilderAccess.RunAndSave);
 module = assemblyBuilder.DefineDynamicModule(Filename); 
 module.DefineType(name, TypeAttributes.Public | TypeAttributes.Class)

So how will I ever be able to compile the following program

class A {
    void M() { B b = new B(); }
}
class B
    void M() { A a = new A(); }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:16:19+00:00Added an answer on June 11, 2026 at 9:16 am

    The primary insight you need here is that TypeBuilder derives from Type. So, even if you didn’t finalize a type yet (by calling CreateType()), you can use it to declare a local variable in another type.

    One more barrier I encountered is that GetConstructor() on an unfinished TypeBuilder doesn’t work (it throws an exception). But if you create the default constructor explicitly, you can call it through the ConstructorBuilder.

    static void Main()
    {
        var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
            new AssemblyName("foo"), AssemblyBuilderAccess.RunAndSave);
        var module = assemblyBuilder.DefineDynamicModule("foo.dll");
        var aType = module.DefineType(
            "A", TypeAttributes.Public | TypeAttributes.Class);
        var bType = module.DefineType(
            "B", TypeAttributes.Public | TypeAttributes.Class);
        var aCtor = aType.DefineDefaultConstructor(MethodAttributes.Public);
        var bCtor = bType.DefineDefaultConstructor(MethodAttributes.Public);
        CreateMethodM(aType, bType, bCtor);
        CreateMethodM(bType, aType, aCtor);
        aType.CreateType();
        bType.CreateType();
        assemblyBuilder.Save("foo.dll");
    }
    
    static void CreateMethodM(
        TypeBuilder thisType, Type otherType, ConstructorInfo otherCtor)
    {
        var method = thisType.DefineMethod(
            "M", MethodAttributes.Private, typeof(void), Type.EmptyTypes);
        var il = method.GetILGenerator();
        var local = il.DeclareLocal(otherType);
        il.Emit(OpCodes.Newobj, otherCtor);
        il.Emit(OpCodes.Stloc, local);
        il.Emit(OpCodes.Ret);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making an MVC-framework in PHP and am trying to recreate a beautiful feature
Context I'm making a game engine using SDL and OpenGL. I'm trying to find
I am learning Java generics and I am trying to adapt some code I
I am trying to start making my own libraries avaialble as packages prior to
I'm trying to write some Pascal script for a installer I'm making with Inno
I'm now making the transition towards writing all my javascript code using Coffeescript, But
I've been trying to compile some F# code with Mono 2.8 in OpenSuSe 11.3.
Iam trying to compile a file of the following format by making a parser
I'm making a 'post text' app and trying to update my database with name
I'm making this game where I'm trying to pair people. So I have this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.