C# 中的类库
我尝试创建一个在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的新类库位于单独的 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.
还没有足够的网站经验来给自己点赞或评论,但 DRapp 的回答解决了我的问题。由于最初的问题有点模糊,我想我应该详细说明一下我所看到的内容:
我正在用 C++ 编写一个 Metro 应用程序,它引用了用 C# 创建的类库。创建从 C# 模块导出的对象工作正常,除非它们的构造函数有参数。
尝试从 C++ 中的 C# 模块创建 B 类型的对象会引发异常,并在输出日志中显示有点神秘的“WinRT 转换错误”。
为了解决这个问题,我能够按照 DRapp 的建议进行操作,并向 B 添加默认构造函数:
不再有异常。 :)
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.
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:
No more exception. :)
听起来你的类没有两个构造函数...(重载),例如
it sounds like you don't have two constructors... (overloaded) for your class such as