简体   繁体   English

C#中的类库

[英]Class Library in C#

I tried to create a class library that is being used in a winforms application in C#. 我试图创建在C#中的winforms应用程序中使用的类库。

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....) (这在逻辑上可能不起作用,因为当我引用类库时-我实际上是在原始程序集之外-也许...。)

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. 如果新的类库位于单独的C#项目中,则需要在WinForms应用程序中设置对该项目的引用,然后才能使用该类。

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. 如果我误解了,请提供更多信息。

Not enough site experience to upvote or comment myself yet, but DRapp's answer fixed my problem. 尚无足够的网站经验来支持自己或发表评论,但DRapp的回答解决了我的问题。 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#. 我正在用C ++编写一个Metro应用程序,该应用程序引用了用C#创建的类库。 Creating objects exported from the C# module was working fine, unless their constructors had parameters. 创建从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

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. 尝试从C ++中的C#模块创建类型为B的对象将引发异常,并在输出日志中显示一些隐秘的“ WinRT转换错误”。

To fix this, I was able to do what DRapp suggested and add a default constructor to B: 为了解决这个问题,我能够执行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...
    }
}

No more exception. 没有更多的例外。 :) :)

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...
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM