简体   繁体   English

C#:使用引用的DLL

[英]C#: Using a referenced DLL

I've created a DLL and referenced it to my project, but I cannot figure out how to actually use it. 我已经创建了一个DLL并将其引用到我的项目中,但是我无法弄清楚如何实际使用它。 It appears that to make it work, I have to use some code like 看来,要使其正常工作,我必须使用一些代码,例如

MyClass class = new MyClass;  

But I still can't get it to work. 但是我仍然无法正常工作。 Here's my code: 这是我的代码:

using MyClass;  
namespace NoName  
{  
    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
        }  

        private void Form1_Load(object sender, EventArgs e)
        {
            MyClass MyClass = new MyClass();
            Bitmap bmp = new Bitmap(MainImage.Image);
        }
    }
}

Make sure you have referenced the namespace that holds the types you want to use from your class library in the dependent program. 确保已从依赖程序的类库中引用了包含要使用的类型的名称空间。

using <Namespace of MyClass>;

public static void Main()
{
   MyClass blah = new MyClass();
}

In most cases the root namespace you are looking for should match the name of your referenced dll. 在大多数情况下,您要查找的根名称空间应与引用的dll的名称匹配。 eg DLLName.xxx ... where "DLLName" would be your root namespace and the anything after the period would signify child folders in a continuing hierarchy. 例如DLLName.xxx ...,其中“ DLLName”将是您的根名称空间,句点之后的所有内容将表示连续层次结构中的子文件夹。

If you don't wan to add a reference to your class namespace (by means of the using reserved word) then you can completely qualified your class when you create an instance of it: 如果不想(通过使用保留字)添加对类名称空间的引用,则可以在创建类实例时完全限定类:

private void Form1_Load(object sender, EventArgs e) 
{ 
    MyClassNameSpace.MyClass MyClass = new MyClassNameSpace.MyClass(); 
    Bitmap bmp = new Bitmap(MainImage.Image); 
}

Actually when you create your instance of your class your variable name needs to not be the same case as your class. 实际上,当您创建类的实例时,变量名不必与类相同。

MyClass myClass = new MyClass();

now you can use 现在您可以使用

myClass.MyMethod(x, y)

In your code posting your were using 在您的代码发布中,您正在使用

MyClass MyClass = new MyClass();

If this is not the actual problem then give us an example of how you want to use MyClass (what methods or properties does it have?) 如果这不是实际问题,请给我们提供一个示例,说明如何使用MyClass(它具有什么方法或属性?)

You should have using MyClassNamespace; 您应该using MyClassNamespace; where MyClassNamespace is the namespace of MyClass . 其中MyClassNamespaceMyClass的命名空间。

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

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