简体   繁体   English

可以在一个项目中引用两个具有相同名称的不同程序集吗? 不同的二进制文件

[英]Possible to reference two different assemblies with the same name in a project? different binaries

this is a strange one...maybe. 这是一个奇怪的...也许。

Summary: I'm using the 32feet libraries for bluetooth communication. 简介:我正在使用32feet库进行蓝牙通信。 We have shared code which runs on both .Net compact (for mobile) and standards. 我们共享了可在.Net Compact(移动版)和标准上运行的代码。 Net. 净。

I'm searching for a suggestive solution or some design pattern that may exist that I'm not aware of. 我正在寻找可能不存在的建议性解决方案或某些设计模式。

32feet provides one assembly for compact framework and one for standard. 32feet提供了一种用于紧凑型框架的组件,一种用于标准框架。 Assemblies have the same name, same class names. 程序集具有相同的名称,相同的类名称。

I need to come up with a solution where I have a generic class which anyone can consume that will know how is connecting. 我需要提出一个解决方案,其中有一个通用类,任何人都可以使用它,它将知道如何进行连接。 I am not even sure if this is possible. 我什至不确定这是否可行。 The solution im leaning towards so far is having two separate assemblies... One for mobile. 到目前为止,我倾向于的解决方案是有两个单独的组件...一个用于移动。 .. One for standard. ..一为标准。 It doesn't feel right and I'm wondering if there's a better route to go. 感觉不对,我想知道是否还有更好的方法。 Is it possible to use so sort of DI to choose the correct binary at runtime? 是否可以使用此类DI在运行时选择正确的二进制文件?

Example: 例:

Public abstract BluetoothBase
{
     Public BluetoothBase ()
     {
     }

     Public int Connect ()
     {
          // this is where problem lies.  Need to tell class which assembly to use based on OS.  So far I'm finding this is not possible. ..
     }
}

This would be adding an interface and 2 implementations that need to be maintained but this I think would accomplish what you are going for. 这将添加一个接口和2个需要维护的实现,但是我认为这可以满足您的需求。

Define a interface in the shared code. 在共享代码中定义一个接口。

public interface IBluetooth
{
    //methods you need from the base class.
}

In the mobile platform specific code 在移动平台上的特定代码

public class MobileBluetooth : IBluetooth
{
//Implement
}

In the desktop platform specific code 在桌面平台上的特定代码

public class DesktopBlueooth : IBluetooth
{
    //Implement
}

And for your framework or bluetooth base class have an initialize method where you inject in your interface. 对于您的框架或蓝牙基类,您可以在接口中注入一个initialize方法。

public abstract BluetoothBase
{
    private IBluetooth _connection;

    public Init(IBluetooth connection)
    {
        _connection = connection;
    }

    public int Connect()
    {
         //use the interface to connect.
    }
}

In short, the easiest way to do this is the following: 简而言之,最简单的方法如下:

You have two assemblies with the exact same name, same class types, same everything. 您有两个名称完全相同,类类型相同且所有内容相同的程序集。 Only difference? 唯一的区别? I my case one was compiled for desktop while the other was compiled for compact framework 我的情况是,一个是为桌面编译的,另一个是为紧凑框架编译的

I this case the assemblies are called InTheHand.Personal.Net 在这种情况下,程序集称为InTheHand.Personal.Net

The easiest approach is to do the following: 最简单的方法是执行以下操作:

  1. Copy each respective assembly to its own folder. 将每个各自的程序集复制到其自己的文件夹中。
  2. In visual studio, right click on your project and select "unload project". 在Visual Studio中,右键单击您的项目,然后选择“卸载项目”。 This is necessary because the reference manager will not allow you to add two assemblies with the same name. 这是必需的,因为引用管理器不允许您添加两个具有相同名称的程序集。 To get around this you manually edit the project file. 为了解决这个问题,您可以手动编辑项目文件。

  3. Add two reference elements to three reference section. 在三个参考部分中添加两个参考元素。 Each one pointing to the path location of the dll. 每个指向dll的路径位置。

  4. Give each. 给每个。 Dll is own alias. DLL是自己的别名。

The in you're code, just use the extern alias directive to reference each assembly. 在您的代码中,只需使用extern别名指令来引用每个程序集。

If you're doing mobile versus nonmobile like me, wrap each one in a #if PocketPC directive 如果您要像我一样进行移动与非移动,请将每个包装在#if PocketPC指令中

Then create a using alias to each 然后为每个创建一个使用别名

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 两个具有相同名称,相同版本但具有不同公钥的程序集 - Two assemblies with same name ,same version but different public key 在两个不同的appdomain中运行两个相同的程序集 - Running two same assemblies in two different appdomains 在两个不同的程序集中,两个具有相同名称的类可以位于相同的名称空间中吗? - Can two classes with the same name be in the same namespace across two different assemblies? 引用两个程序集,每个引用另一个程序集,但版本不同 - Reference to two assemblies, each one with reference to the another assembly but with different version 是否可以将同一装配的不同版本引用到单个项目中? - Is it possible to reference different version of the same assembly into a single project? 使用Prism,两个模块是否可以引用同一装配体的不同版本? - With Prism, is it possible for two modules to reference different versions of the same assembly? 向C#项目添加依赖项-不同版本的相同程序集 - Adding dependencies to C# project - same assemblies of different versions 如何在一个项目中使用两个不同的Microsoft Interop程序集? - How to use two different Microsoft Interop assemblies in one project? Visual Studio-为具有相同名称的程序集构建不同的发行版 - Visual Studio - building different releases for assemblies with the same name 一个进程是否可以加载两个同名但版本不同的dll? - Is it possible for a process to load two dll with same name but different versions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM