DLL 问题.C#无法识别DLL文件,或者只是我



我被分配了一个任务,要在 C# 中使用 DLL 文件。我已经创建了 DLL 文件 (Prog1210.dll),并将其作为引用添加到 C# 中的解决方案资源管理器中。DLL 文件有一个变量 txtNumber1,它试图在这个主类中访问。

只是想知道为什么它以此类形式识别 DLL 中的 ValidateTextbox,但说它无法识别 using 语句中的 Prog1210,并且无法识别 txtNumber1。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Prog1210;
namespace StaticClass
{
    class Class1
    {
        private void btnValidate_Click(object sender, EventArgs e)
        {
        // Use the ValidateTexbox class that has been added to this project
            if (ValidateTextbox.IsPresent(txtNumber1) &&
            ValidateTextbox.IsDouble(txtNumber1) &&
            ValidateTextbox.IsWithinRange(txtNumber1, 1.0, 100.0))
            {
                MessageBox.Show("Textbox value is valid!", "Good Data");
            }
            else
            {
            // The ValidateTexbox methods assigns an error message to the Tag
                // property of the textbox.
                string display = (string)txtNumber1.Tag;
                MessageBox.Show(display, "Bad Data");
            txtNumber1.Focus();
        }
    }
}

}

我的 DLL 文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms; // required to work with Textboxes
public static class ValidateTextbox
{
// A class of static methods that will validate data in a textbox.
// An error message is assigned to the Tag property of the textbox.
//******** Empty Textbox Check ****************//
public static bool IsPresent(TextBox textbox)
{
if (textbox.Text == "")
{
textbox.Tag = "A value is required...";
return false;
}
return true;
}
// ******* Valid Data Type Check ***********//
public static bool IsInt(TextBox textbox)
{
try
{
Convert.ToInt32(textbox.Text);
return true;
}
catch (Exception)
{
textbox.Tag = "The value must be an integer...";
return false;
}
}
public static bool IsDouble(TextBox textbox)
{
try
{
Convert.ToDouble(textbox.Text);
return true;
}
catch (Exception)
{
textbox.Tag = "The value must be a double...";
return false;
}
}
public static bool IsDecimal(TextBox textbox)
{
try
{
Convert.ToDecimal(textbox.Text);
return true;
}
catch (Exception)
{
textbox.Tag = "The value must be a decimal...";
return false;
}
}
//*********** Valid Range Check - Overloaded Methods *************//

只是想知道为什么它以此类形式识别 DLL 中的 ValidateTextbox,但说它在 using 语句中无法识别 Prog1210,

这是因为您的 Prog1210.dll 没有使用命名空间。 如果您已将所有内容指定为 Prog1210 命名空间,它将按预期工作。

如果要更改此设置,请将 DLL 代码更改为:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms; // required to work with Textboxes
namespace Prog1210
{
    public static class ValidateTextbox
    {
        // .. your code
    }
} // Add closing brace for namespace

并且无法识别 txtNumber1。

Class1 中没有txtNumber1变量。 只能验证调用方法的作用域中存在的TextBox变量。

我想你的DLL是用C++或其他一些母语构建的。不能从托管程序集/dll 使用这些类型的 DLL。

要工作,它必须是 .NET Assembly/DLL 或托管 C++/CLI DLL。

如果你真的不能改变那个DLL,你可以用C++/CLIDLL包装它。欲了解更多信息:使用本机C++的 .NET 类使用 C++/CLI 作为"中间件"

你需要在 prog dll 中有一个命名空间,并且你需要在第一段代码中将类标记为 txtnumber1 的公共。并确保将 txtnumber1 作为表单中文本框的 ID

相关内容

最新更新