在 C# 中使用两个同名的静态类
我有一个 DLL,已包含在我的 C# 项目中。我们称之为“one.dll” 这个DLL包含一个名为“staticclass”的静态类,
我有另一个DLL,我也将其包含在同一个项目中。我们称之为“two.dll” 该 DLL 还包含一个名为“staticclass”的静态类。
现在,当我在项目中同时包含两个 DLL 并尝试访问“staticclass”时,自然会出现错误。有没有办法可以更改类的名称或为其指定某种别名,因此假设“one.dll”中的“staticclass”将保持原样,并且我可以为“two”中的“staticclassTwo”指定别名.dll”
请注意,我无权访问“one.dll”和“two.dll”的源编解码器
I have a DLL which I have included in my C# project. Let's call it "one.dll"
This DLL contain a static class named "staticclass"
I have another DLL which I have also included in same project. Let's call it "two.dll"
This DLL also contain a static class named "staticclass"
Now when I include both DLLs at the same time in my project and try to access "staticclass" then naturally it gives error. Is there a way I can change the name of class or give it some kind of alias so let's say "staticclass" in "one.dll" will remain as it is, and I can give alias to "staticclassTwo" which is in "two.dll"
Please note I do not have access to source codec of both "one.dll" and "two.dll"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(我假设这两个类也位于同一命名空间中。如果不是,也很简单 - 只需使用简单的
using
指令作为别名,或在代码中使用完全限定名称。)确实可以提供别名 - 外部别名。实际上,这将“程序集”添加为命名空间差异化的另一个级别。
显然,您应该尽可能避免这种情况,但很高兴 C# 提供了一种在您绝对需要时非常明确的方法。
Anson Horton 有一个很好的演练,告诉您如何在实践中使用它们。
(I'm assuming the two classes are also in the same namespace. If they're not, it's easy - just use simple
using
directives for aliases, or the fully qualified name in the code.)You can indeed give an alias - an extern alias. Effectively this adds "assembly" as another level of namespace differentiation.
Obviously you should avoid this situation when you can, but it's nice that C# provides a way of being very explicit when you absolutely have to.
Anson Horton has a good walkthrough for how you use them in practice.
您只需使用别名即可做到这一点。
在您的代码中,位于 namespace 行的下方; 使用下面给出的别名:
希望这会有所帮助!
You can do it by simply using Alias.
In your code, just below to the namespace line; use alias as given below:
Hope this helps!