2017-10-04 81 views
0

我創建了一個dll,我們稱之爲ExampleHelper.dll需要從dll實例化類到其他項目中使用

Visual Studio的類庫我已經編譯dll的結構如下:

namespace ExampleHelper 
{ 
    public class Example 
    { 
     public string GetExamples(string input) 
     { 
      // stuff 
     } 
    } 
} 

所以,我引用它在我的其他項目中,我想用這些ExampleHelper類,由有關添加在文件的頂部using行:

using ExampleHelper;

現在,我可以看到,我可以從ExampleHelper訪問類,它是稱爲Example。但是,我無法訪問該類中的方法,這意味着我不能寫Example.GetExamples("hello"),因爲它說GetExamples不存在。

我注意到,我可以這樣做:

Example e = new Example(); 
e.GetExamples("hello"); 

,我當然可以用,但並不覺得我想使用一個輔助方法,每次實例化一個新的對象完全正確。

我做了一件完全錯誤的事情嗎?我的猜測是肯定的,但我無法找到我要去哪裏的錯誤。任何幫助感謝!

+0

如果你不想創建一個實例,使方法(也許是類)靜態... – CodeCaster

+0

也見[什麼是在C#中的「靜態方法?」](https://stackoverflow.com/問題/ 4124102/whats-a-static-method-in-c) – Liam

回答

1

您需要有一個Example對象的實例來調用此方法。 要調用沒有對象實例的方法,方法必須是靜態的。

public static string GetExamples(string input) 

應該是該方法的聲明。

1

GetExamples(string input)一個static方法

public static string GetExamples(string input) 

靜態方法不需要類的實例。

相關問題