在 C# 中使用两个同名的静态类

发布于 2025-01-05 05:56:12 字数 347 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离去的眼神 2025-01-12 05:56:12

(我假设这两个类也位于同一命名空间中。如果不是,也很简单 - 只需使用简单的 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.

静水深流 2025-01-12 05:56:12

您只需使用别名即可做到这一点。

在您的代码中,位于 namespace 行的下方; 使用下面给出的别名

namespace ConsoleApp
{

    using ClassOne = Assembly.One.MyClass; /* your dll 1 class */
    using ClassTwo = Assembly.Two.MyClass; /* your dll 2 class */

    class Program
    {

        static void Main(string[] args)
        {

            ClassOne one = new ClassOne();
            // Do your stuff with ClassOne object

            ClassTwo two = new ClassTwo();
            // Do your stuff with ClassTwo object

        }
    }
}

希望这会有所帮助!

You can do it by simply using Alias.

In your code, just below to the namespace line; use alias as given below:

namespace ConsoleApp
{

    using ClassOne = Assembly.One.MyClass; /* your dll 1 class */
    using ClassTwo = Assembly.Two.MyClass; /* your dll 2 class */

    class Program
    {

        static void Main(string[] args)
        {

            ClassOne one = new ClassOne();
            // Do your stuff with ClassOne object

            ClassTwo two = new ClassTwo();
            // Do your stuff with ClassTwo object

        }
    }
}

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文