使用从ICode编译器生成的内存程序集来构建另一个源代码

时间:2015-12-18 23:39:31

标签: c#

我有一个接口和一个实现它的类。我正在尝试创建一个单元测试,它将编译接口和类,并查看是否有任何错误(如果没有那么那将意味着该类确实实现了接口)。我能够构建没有错误的接口,但是当我尝试构建实现它的类时,它表示该类没有实现接口。我已经确定该类确实实现了接口,所以我知道我在单元测试中编译它的方式有问题。我生成inMemoryAssembly.dll,然后在尝试编译实现时尝试将其添加到参数中,但这不起作用。有什么建议吗?

 using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Text;
using System.IO;

namespace UnitTestProject2
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        [TestCategory("NumberOfFiles")]
        public void TestMethod1()
        {
            var serviceReferancepath = System.IO.Path.GetFullPath(@"..\..\Service References\");
            compileIterface(serviceReferancepath);
            compileImplementation(serviceReferancepath);
        }
        static void compileIterface(string path)
        {
            CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider();
            ICodeCompiler compiler = codeProvider.CreateCompiler();
            CompilerParameters parametersForInterface = new CompilerParameters();

            parametersForInterface.GenerateInMemory = true;
            parametersForInterface.ReferencedAssemblies.Add("System.dll");
            parametersForInterface.ReferencedAssemblies.Add("System.Data.Services.Client.dll");
            parametersForInterface.ReferencedAssemblies.Add("System.ComponentModel.dll");
            parametersForInterface.OutputAssembly = "inMemoryAssembly.dll";
            StringBuilder interfaceSC = new StringBuilder();
            using (StreamReader sr = new StreamReader(path + @"alarm\IActiveEventSource.cs"))
            {
                string line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    interfaceSC.AppendLine(line);
                }
            }
            CompilerResults results = compiler.CompileAssemblyFromSource(parametersForInterface, interfaceSC.ToString());
            StringWriter sw = new StringWriter();
            foreach (CompilerError ce in results.Errors)
            {
                if (ce.IsWarning) continue;
                sw.WriteLine("{0}({1},{2}: error {3}: {4}", ce.FileName, ce.Line, ce.Column, ce.ErrorNumber, ce.ErrorText);
            }
            // If there were errors, raise an exception...
            string errorText = sw.ToString();

        }
        static void compileImplementation(string path)
        {
            Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider();
            ICodeCompiler compiler = codeProvider.CreateCompiler();
            CompilerParameters Parameters = new CompilerParameters();
            Parameters.ReferencedAssemblies.Add("inMemoryAssembly.dll");

            Parameters.GenerateExecutable = false;
            Parameters.GenerateInMemory = true;
            StringBuilder implementationSC = new StringBuilder();
            using (StreamReader sr = new StreamReader(path + @"alarm\implementation\TheActiveEventSource.cs"))
            {
                string line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    implementationSC.AppendLine(line);
                }
            }
            CompilerResults results = compiler.CompileAssemblyFromSource(Parameters, implementationSC.ToString());
            StringWriter sw = new StringWriter();
            foreach (CompilerError ce in results.Errors)
            {
                if (ce.IsWarning) continue;
                sw.WriteLine("{0}({1},{2}: error {3}: {4}", ce.FileName, ce.Line, ce.Column, ce.ErrorNumber, ce.ErrorText);
            }
            // If there were errors, raise an exception...
            string errorText = sw.ToString();
        }

    }
}

compileImplementation方法,当我用断点运行它时,我得到结果的以下错误 UnitTestProject2.alarm.ActiveEventSource'没有实现接口成员'UnitTestProject2.alarm.IActiveEventSource.EventSource

这是实现代码

namespace UnitTestProject2.alarm
{
    partial class ActiveEventSource: IActiveEventSource
    {
    }
}

编辑: 我正在查看警告,但没有查看实现编译所说的实际错误。 compileImplementatation方法的字符串errortext表示无法找到inMemoryAssembly.dll

0 个答案:

没有答案