C# 中的类库

发布于 2024-09-08 13:06:36 字数 251 浏览 6 评论 0原文

我尝试创建一个在 C# 的 winforms 应用程序中使用的类库。

在我的应用程序中,我从文本框输入并通过按钮单击我正在实例化 我的事件只有一个参数(来自文本框)。我尝试创建一个构造函数 这一个参数——但无济于事。看来我只是添加一个现有的类 项目我可以做到这一点,但在引用类库时不能。

只是想找到一种在类库中使用单参数构造函数的方法 如果可能的话。请帮忙。 (这可能不符合逻辑,因为当我引用 类库 - 我实际上是在原始程序集之外 - 但也许......)

I tried to create a class library that is being used in a winforms application in C#.

In my application I have input from a textbox and through a button click I'm instantiating
my event with one parameter (from the textbox). I tried to create a constructor with
this one parameter - but to no avail. It seems if I just add a class to be existing
project I can do this but not when referencing a class library.

Just wanted to find a way to use a one parameter constructor within a class library
if possible. Please help. (this may not work logically because when I reference the
class library - I am actually going outside the original assembly - but maybe....)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

秋风の叶未落 2024-09-15 13:06:36

如果您的新类库位于单独的 C# 项目中,则需要先从 WinForms 应用程序设置对该项目的引用,然后才能使用该类。

当然,我正在尝试阅读您原始帖子的字里行间。听起来您知道如何使其工作,但当该类在单独的项目中定义时则不知道。如果我误解了,请提供更多信息。

If your new class library is in a separate C# project you need to set a reference to that project from your WinForms app before you can use the class.

Of course I'm trying to read between the lines of your original post. It sounds like you know how to make it work, just not when the class is defined in a seperate project. If I've misunderstood, please give more info.

弃爱 2024-09-15 13:06:36

还没有足够的网站经验来给自己点赞或评论,但 DRapp 的回答解决了我的问题。由于最初的问题有点模糊,我想我应该详细说明一下我所看到的内容:

我正在用 C++ 编写一个 Metro 应用程序,它引用了用 C# 创建的类库。创建从 C# 模块导出的对象工作正常,除非它们的构造函数有参数。

// C# file exported to .winmd class library for use in metro app
namespace A
{
    public sealed class B
    {
        public B(bool bTest)
        {}

        // Other methods/members...
    }
}

// C++ metro app referencing .winmd created from C# above
...

A::B^ spB = ref new A::B(bTest);  // Throws an exception

尝试从 C++ 中的 C# 模块创建 B 类型的对象会引发异常,并在输出日志中显示有点神秘的“WinRT 转换错误”。

为了解决这个问题,我能够按照 DRapp 的建议进行操作,并向 B 添加默认构造函数:

// C# file exported to .winmd class library for use in metro app
namespace A
{
    public sealed class B
    {
        public B()
        {}
        public B(bool bTest)
        {}

        // Other methods/members...
    }
}

不再有异常。 :)

Not enough site experience to upvote or comment myself yet, but DRapp's answer fixed my problem. Since the original question is a bit vague I thought I'd detail what I was seeing a bit more:

I am writing a metro application in C++ which references a class library created in C#. Creating objects exported from the C# module was working fine, unless their constructors had parameters.

// C# file exported to .winmd class library for use in metro app
namespace A
{
    public sealed class B
    {
        public B(bool bTest)
        {}

        // Other methods/members...
    }
}

// C++ metro app referencing .winmd created from C# above
...

A::B^ spB = ref new A::B(bTest);  // Throws an exception

Attempting to create an object of type B from the C# module in C++ would throw an exception, with a somewhat cryptic "WinRT transform error" show in the output log.

To fix this, I was able to do what DRapp suggested and add a default constructor to B:

// C# file exported to .winmd class library for use in metro app
namespace A
{
    public sealed class B
    {
        public B()
        {}
        public B(bool bTest)
        {}

        // Other methods/members...
    }
}

No more exception. :)

心意如水 2024-09-15 13:06:36

听起来你的类没有两个构造函数...(重载),例如

public class YourClass
{
   public YourClass()
   {
   }


   public YourClass(String OneParameter)  // this OVERLOADS the default No parameter one
   {
      DoWhatever with your OneParameter...
   }
}

it sounds like you don't have two constructors... (overloaded) for your class such as

public class YourClass
{
   public YourClass()
   {
   }


   public YourClass(String OneParameter)  // this OVERLOADS the default No parameter one
   {
      DoWhatever with your OneParameter...
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文