无法访问 DLL 中的方法 [英] Can't access methods in DLL
问题描述
使用 asp.net/vb.net.创建了一个包含 2 个项目的解决方案,MainProject"和MyCommonStuff".第二个项目(MyCommonStuff")实际上是一个通用的实用程序类,我希望生成的 dll 也用于其他项目.
Using asp.net/vb.net. Created a solution with 2 projects, "MainProject" and "MyCommonStuff". The 2nd project ("MyCommonStuff") is really a common utilities class, the resulting dll I hope to use for other projects as well.
MyCommonStuff 的定义非常简单......
MyCommonStuff is defined very simply....
Public Class MyCommonStuff Stuff
Public Shared Function GetInfo() as string
:
:
End Function
Public Shared Sub Test
:
:
End Sub
:
End Class
在 MainProject 中,我设置了对此 MyCommonStuff 项目的引用.
In MainProject I set a reference to this MyCommonStuff project.
我想在我的代码中访问一些 MyCommonStuff 方法.但由于某种原因,这些方法未被识别.
I want to access some of the MyCommonStuff methods in my code. But for some reason the methods are not being recognized.
例如,在 MainProject 中的一个按钮中,我尝试了这个....
For example, in a button in the MainProject I tried this....
dim m as new MyCommonStuff
x = m.GetInfo()
Intellisense 不会为 m 获取任何子/功能.我究竟做错了什么?谢谢!
Intellisense doesn't pick up any of the subs/functions for m. What am I doing wrong? Thanks!
推荐答案
问题是您已将方法设为静态(在 VB 中为共享").您需要要么删除Shared
关键字:
The thing is that you've made your methods static ("Shared" in VB). You need to either remove the Shared
keywords:
Public Function GetInfo() As String
':
':
End Function
Public Sub Test()
':
':
End Sub
或保留 Shared
关键字并像这样使用它:
or keep the Shared
keywords and use it like this:
x = MyCommonStuff.GetInfo()
这里有一些关于共享
成员.最值得注意的是:
Here is some information about Shared
members. Most notably:
指定一个或多个声明的编程元素是与整个类或结构相关联,而不是与特定的类或结构的实例.
Specifies that one or more declared programming elements are associated with a class or structure at large, and not with a specific instance of the class or structure.
换句话说,如果您想从 MyCommonStuff
类的 实例 中使用您的方法,例如m
在 m.GetInfo()
中,您需要关闭 Shared
关键字.另一方面,如果您有一个在您的类的所有实例中通用的方法,或者您甚至不需要实例,您将使用Shared
关键字并像我上面说的那样访问方法,例如MyCommonStuff.GetInfo()
.
In other words, if you want to use your methods from an instance of your MyCommonStuff
class, e.g. m
in m.GetInfo()
, you need to leave the Shared
keyword off. If, on the other hand, you have a method that is common across all instances of your class or for which you don't even need an instance, you would use the Shared
keyword and access the method like I said above, e.g. MyCommonStuff.GetInfo()
.
这篇关于无法访问 DLL 中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!