0
我有一個自定義接口類型(IMyInterface)的ReadOnlyCollection。我想將擴展方法添加到自定義接口類型。但是,當我訪問ReadOnlyCollection中的項目時,我的擴展方法沒有顯示出來。 這裏是我的代碼:對ReadOnlyCollection項目的擴展方法
public static class MyClass
{
public static string GetValueByName(this IMyInterface myInterface, string myName)
{
foreach (var name in myInterface.names) //
{
if (myName == name)
return name;
}
throw new ArgumentException(myName + " not found.");
}
}
當我嘗試做這樣的事情來訪問它,
MyMethod[0].<extension method> //MyMethod returns a ReadOnlyCollection<IMyInterface>
擴展方法沒有顯示在後可用彈出了的MyMethod [0](在Visual Studio中)意味着它不可用。我可能會錯過什麼?是否可以在ReadOnlyCollection中添加項目的擴展方法?
聽起來像你沒有在擴展名的命名空間中包含'using'。 – 2014-09-19 19:40:44
哇!我以爲Visual Studio會自動檢測並提示我添加它。沒有意識到它錯過了命名空間!謝謝@NeilSmith。這工作! – Setafire 2014-09-19 19:44:01
我想知道爲什麼人們爲自己的類/接口使用擴展方法! – Yogee 2014-09-19 19:44:13