0

I am currently confused, if it is possible to compile an interface at runtime and add it to a "hardcoded" namespace.

Some code to explain:

using System;
using System.Runtime.CompilerServices;
using EngineCore;

namespace EngineCore
{
    public interface iMobilePhones
    {
        string phoneNumber {get; set;}
        string phoneRingtone {get; set;}

        void CallNumber(string phoneNumber);
    }
}

This is the runtime compiled interface, I want to add to the EngineCore namespace at runtime.

The compilation seems to work as expected and as you can see I compiled it using the EngineCore namespace.

using System;
using System.Runtime.CompilerServices;
using EngineCore;

namespace EngineCore
{
    public class EQMobilePhoneCheap : iMobilePhones
    {
        //iMobilePhones interface members
        private string PhoneNumber;
        private string PhoneRingtone = "Default";

        public string phoneNumber
        {
            get
            {
                return PhoneNumber;
            }
            set
            {
                PhoneNumber = value;
            }
        }
        public string phoneRingtone
        {
            get
            {
                return PhoneRingtone;
            }
            set
            {
                PhoneRingtone = value;
            }
        }

        //iMobilePhones interface functions
        public void CallNumber(string phoneNumber)
        {

        }

    }
}

This is the second runtime compiled code. As you can see, I am trying to use the iMobilePhones interface I compiled before.

But the compilation fails because iMobilePhones is not an interface of EngineCore

The actual question:

So I am wondering if there is a way to "register" the previously compiled interface iMobilePhones to the EngineCore namespace?

Thank you very much for reading. Any suggestions are welcome.

1 Answers1

2

You simply need to add a reference to the assembly containing the interface when you compile code that uses it.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Thank you for your quick responce. When running the runtime compiler I currently add this Code: CompilerParameters parameters = new CompilerParameters(); parameters.ReferencedAssemblies.Add(assembly); If I am right, this is where I tell the compiler what assemblies to use right? But I already use EngineCore.dll when compiling both the interface and the class using the interface... – AquilaAbriel Mar 25 '15 at 20:54
  • No; you need to pass the assembly that _contains_ the interface. Just like any other type, you can only use it if you reference the assembly that defines it. This will be much easier if you switch to the Roslyn compiler. – SLaks Mar 25 '15 at 21:02
  • OK, but the interface is also runtime compiled, so I should be in some kind of temporary assembly. So I have to store these temporary assembly names. And include it when running the second compilation? – AquilaAbriel Mar 25 '15 at 21:05
  • OK, you were absolutely right. Thank you very much. I had to add the assembly of the compiled interface when compiling the second class. Question answered. – AquilaAbriel Mar 25 '15 at 21:36