【问题标题】:C# Runtime Compilation - Add new interface to namespaceC# 运行时编译 - 将新接口添加到命名空间
【发布时间】:2015-03-25 20:49:00
【问题描述】:

我目前很困惑,是否可以在运行时编译接口并将其添加到“硬编码”命名空间。

一些代码解释:

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);
    }
}

这是运行时编译的接口,我想在运行时添加到EngineCore命名空间中。

编译似乎按预期工作,如您所见,我使用 EngineCore 命名空间编译它。

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)
        {

        }

    }
}

这是第二个运行时编译的代码。如您所见,我正在尝试使用我之前编译的 iMobilePhones 界面。

但是编译失败,因为 iMobilePhones 不是 EngineCore 的接口

实际问题:

所以我想知道是否有办法将之前编译的接口 iMobilePhones “注册”到 EngineCore 命名空间?

非常感谢您的阅读。 欢迎提出任何建议。

【问题讨论】:

    标签: c# namespaces runtime-compilation


    【解决方案1】:

    您只需在编译使用该接口的代码时添加对包含该接口的程序集的引用。

    【讨论】:

    • 感谢您的快速回复。运行运行时编译器时,我当前添加了以下代码: CompilerParameters parameters = new CompilerParameters();参数.ReferencedAssemblies.Add(程序集);如果我是对的,这就是我告诉编译器正确使用哪些程序集的地方?但是我在编译接口和使用接口的类时已经使用了EngineCore.dll...
    • 否;您需要传递 包含 接口的程序集。就像任何其他类型一样,只有在引用定义它的程序集时才能使用它。如果您切换到 Roslyn 编译器,这会容易得多。
    • 好的,但是接口也是运行时编译的,所以我应该在某种临时程序集中。所以我必须存储这些临时程序集名称。并在运行第二次编译时包含它?
    • 好吧,你说得对。非常感谢。在编译第二个类的时候,我不得不添加编译接口的程序集。问题已回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 2016-07-08
    相关资源
    最近更新 更多