C# 需要实例化dll中的类以便在其他项目中使用
我已经创建了一个C# 需要实例化dll中的类以便在其他项目中使用,c#,visual-studio,C#,Visual Studio,我已经创建了一个dll,让我们称之为exampleheloper.dll 我编译到dll的Visual Studio类库的结构如下所示: namespace ExampleHelper { public class Example { public string GetExamples(string input) { // stuff } } } 因此,我在我的另一个项目中引用了它,在该项目中,我想
dll
,让我们称之为exampleheloper.dll
我编译到dll
的Visual Studio类库的结构如下所示:
namespace ExampleHelper
{
public class Example
{
public string GetExamples(string input)
{
// stuff
}
}
}
因此,我在我的另一个项目中引用了它,在该项目中,我想使用这些exampleheloper
类,方法是在相关文件的顶部添加一行using
:
使用ExampleHelper代码>
现在,我可以看到我可以从exampleheloper
访问这个类,它被称为Example
。但是,我无法访问该类中的方法,这意味着我无法编写Example.GetExamples(“hello”)
,因为它说GetExamples
不存在
我注意到我可以做到这一点:
Example e = new Example();
e.GetExamples("hello");
我当然可以使用它,但每次我想使用助手方法时,实例化一个新对象感觉不太对
我做错什么了吗?我的猜测是肯定的,但我找不到我错在哪里。感谢您的帮助 您需要一个示例对象的实例来调用此方法。
要在没有对象的情况下调用方法,方法必须是静态的
public static string GetExamples(string input)
应该是方法的声明。您需要一个示例对象的实例来调用此方法。
要在没有对象的情况下调用方法,方法必须是静态的
public static string GetExamples(string input)
应该是方法的声明。请将您的类和方法设置为静态,您可以在任何地方使用它
请将您的类和方法设置为静态,您可以在任何地方使用它
MakeGetExamples(字符串输入)
astatic
方法
public static string GetExamples(string input)
静态方法不需要类的实例。MakeGetExamples(字符串输入)
aStatic
方法
public static string GetExamples(string input)
静态方法不需要类的实例。如果不想创建实例,请将方法(可能还有类)设置为静态…另请查看如果不想创建实例,请将方法(可能还有类)设置为静态…另请参阅