两个具有相同命名空间的不同 DLL [英] two different DLL with same namespace

查看:41
本文介绍了两个具有相同命名空间的不同 DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有相同命名空间的 DLL 文件,但它们具有不同的方法和类型.如何在我的项目中引用这两个 DLL 并使用它们的方法和类型?

I have two DLL files that have the same namespace but they have different methods and types. How can I reference both DLLs in my project and use their methods and types?

顺便说一下,这两个 DLL 有一些名称相同但实现不同的方法和类型以及一些独特的方法和类型.

By the way, these two DLLs have some methods and types with the same name but different implementation and some unique methods and types.

推荐答案

您无需做任何特别的事情 - 只需引用它们并使用类型即可.命名空间可以毫无问题地跨越多个程序集,因为它们并不是真正的不透明类型.命名空间只是为它包含的所有类型添加一个公共前缀的一种方式,允许您在不同的命名空间下拥有多个同名类型.(框架不会将它们视为具有相同的名称,因为它会看到所有内容的完全限定"名称——它的前面有一个别名和一个命名空间.)

There's nothing special you need to do - just reference them and use the types. Namespaces can span accross several assemblies without problems, because they're not really opaque types. A namespace is just a way of adding a common prefix to all the types it contains, allowing you to have multiple types of the same name under different namespaces. (The framework doesn't see them as having the same names, because it sees the "fully qualified" name of everything - which has an alias and a namespace attached to the front of it.)

在极少数情况下,您引用了 2 个具有相同类型名称相同命名空间的程序集(例如同一 dll 的 2 个不同版本)-您可以区分哪个程序集用于使用别名的给定类型.所有引用的默认别名是 global,但您可以在引用它时为任何程序集指定自己的别名(使用编译器开关 - 或仅使用 Visual Studio 中的属性框) - 并有一个extern alias 子句位于您使用它的代码文件顶部 - 您可以使用 ::MyNamespace.Type

In the rare event that you reference 2 assemblies which have the same type names and the same namespaces (such as 2 different versions of the same dll) - you can distinguish which assembly to use for a given type using an alias. The default alias for all references is global, but you can specify your own alias for any assembly when you reference it (using a compiler switch - or just use the properties box in Visual Studio) - and have an extern alias <name> clause at the top of your code file where you use it - you would access the types from different assemblies with <name>::MyNamespace.Type

这篇关于两个具有相同命名空间的不同 DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆