添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

When using CSharpCodeProvider.CompileAssemblyFromSource how reference an interface in the running project?

Ask Question

I have some code that is running and generates an implementation of an interface at runtime. I am trying to use CSharpCodeProvider but when I try to compile code that has a class implementing an interface in the same code from the running application (which is running in debug mode in Visual Studio), it throws an exception:

"The type or namespace name 'TestCodeGen' could not be found (are you missing a using directive or an assembly reference?)"

My code:

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Web.Http;
using Microsoft.CodeDom.Providers.DotNetCompilerPlatform;
namespace TestCodeGen
    public class TestApp
        public static void Main(string[] args)
            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateExecutable = false;
            parameters.GenerateInMemory = true;
            parameters.OutputAssembly = "MyImpl";
            CompilerResults results = provider.CompileAssemblyFromSource(
                parameters,
                    using TestCodeGen;
                    public class MyImpl : IInterface
                        public string GetName()
                            return ""test"";
            IInterface impl = (IInterface) Activator.CreateInstance(null, "MyImpl");
        System.Diagnostics.Debug.WriteLine(impl.GetName());
    public interface IInterface
        string GetName();

How would I add a reference to my interface and its namespace? Is there some way to use parameters.ReferencedAssemblies.Add("WHAT_GOES_HERE?");?

Probably something like Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 You should look for the exact name/version – Jeroen van Langen Apr 30, 2019 at 18:20 @J.vanLangen I think this might work (looking into it): parameters.ReferencedAssemblies.Add(typeof(TestApp).Assembly.Location); – Don Rhummy Apr 30, 2019 at 18:25 Some assemblies should be loaded from the GAC, so a fullname which includes a version, will fix this aswell. – Jeroen van Langen May 1, 2019 at 7:49

You need to add a reference to the assembly that contains IInterface. You can use typeof(IInterface).Assembly.Location.

To create an instance of MyImpl, you first need to get the type using results.CompiledAssembly.GetType("MyImpl")

using System;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
public class TestApp
    public static void Main(string[] args)
        CSharpCodeProvider provider = new CSharpCodeProvider();
        CompilerParameters parameters = new CompilerParameters();
        parameters.GenerateExecutable = false;
        parameters.GenerateInMemory = true;
        parameters.OutputAssembly = "MyImpl";
        parameters.ReferencedAssemblies.Add(typeof(IInterface).Assembly.Location);
        CompilerResults results = provider.CompileAssemblyFromSource(
            parameters,
                    public class MyImpl : IInterface
                        public string GetName()
                            return ""test"";
        var myImplType = results.CompiledAssembly.GetType("MyImpl");
        IInterface impl = (IInterface)Activator.CreateInstance(myImplType);
        System.Diagnostics.Debug.WriteLine(impl.GetName());
public interface IInterface
    string GetName();
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.