“不包含定义...和没有扩展方法..”错误 [英] “does not contain a definition…and no extension method..” error
问题描述
我遇到一个问题,因为我不能使用对象方法,因为这个错误:
不包含定义...没有扩展方法...
它与这个非常相关问题。我在做什么作为答案在这个问题,但我仍然得到这个错误。
命名空间MyProject.ViewModel
{
public class NetworkHealthViewModel:ViewModelBase
{
private IDataService _dataService;
public ObservableCollection< NetworkBandwidthModel> NbicNetworkBandwidth
public ObservableCollection< NetworkPortalStatusModel> NbicNetworkPortalStatus
public NetworkHealthViewModel()
{
_dataService = new DataServiceNetworkHealth();
NbicNetworkBandwidth = new ObservableCollection< NetworkBandwidthModel>();
NbicNetworkPortalStatus = new ObservableCollection< NetworkPortalStatusModel>();
_dataService.LoadChartItems(NetworkBandwidthLoaded,NetworkBandwidthLoadedFailed);
_dataService.LoadPortalStatus(NetworkPortalStatusLoaded,NetworkPortalStatusLoadedFailed);
}
错误位于LoadPortalStatus LoadChartItems()很好。 NetworkBandwidthLoaded和NetworkPortalStatusLoaded是代理。
NetworkPortalStatusLoaded的布局与NetworkBandwidthLoaded基本相同:
private void NetworkPortalStatusLoaded(IEnumerable< ChartModel> portalStatItems)
{
NbicNetworkPortalStatus.Clear();
var networkPortalItems =从portalStatItems中的项目
其中((NetworkPortalStatusModel)项目).Unit ==Portal
选择项目;
foreach(networkPortalItems中的var项)
{
NbicNetworkPortalStatus.Add((NetworkPortalStatusModel)item);
}
Message =网络门户网站详细信息;
}
我的DataServiceNetworkHealth类定义为:
命名空间MyProject.DataServices
{
public class DataServiceNetworkHealth:IDataService
{
private Action< IEnumerable< ChartModel>> ; _delagateSuccess;
private Action< Exception> _delagateFail;
private String _portalHtmlResponse;
public void LoadChartItems(Action< IEnumerable< ChartModel>> success,Action< Exception> fail)
{
....
}
public void LoadPortalStatus(Action< IEnumerable< ChartModel>> success,Action< Exception> fail)
{
....
}
} $ b方法LoadChartItems()是在接口IDataService中定义的,但LoadPortalStatus不是。也许这就是问题所在。还有其他DataServiceX类不使用LoadPortalStatus。
我知道这是一个长的帖子,但我想更好地提供所有的信息。
解决方案您对 _dataService
的引用
是 IDataService
类型,这意味着您只能访问此界面定义的成员(或所述界面的扩展方法)。您提到 LoadPortalStatus
不是该接口的一部分,因此您无法通过 _dataService
引用访问它。
根据评论更新:让我们看另一个例子。如果您像这样定义 List< int>
引用:
; int> i = new List< int>(); $ <$>
您可以访问 List< T>()的所有成员和 Object
的所有成员。但是,如果您将引用更改为
IEnumerable< int& i = new List< int>();
只有通过 i
code>,其定义在 IEnumerable< T>
上,并且成员继承自 Object
。
如果您包含 System.Linq
命名空间,您还可以访问为 IEnumerable< T>
。
i
的静态类型确定通过该引用提供的成员。
I am having an issue whereby I cannot use an objects method because of this error:
Does not contain a definition...and no extension method...
It is very related to this question. I am doing what is given as the answer in that question, but I am still getting this error.
namespace MyProject.ViewModel
{
public class NetworkHealthViewModel : ViewModelBase
{
private IDataService _dataService;
public ObservableCollection<NetworkBandwidthModel> NbicNetworkBandwidth
public ObservableCollection<NetworkPortalStatusModel> NbicNetworkPortalStatus
public NetworkHealthViewModel()
{
_dataService = new DataServiceNetworkHealth();
NbicNetworkBandwidth = new ObservableCollection<NetworkBandwidthModel>();
NbicNetworkPortalStatus = new ObservableCollection<NetworkPortalStatusModel>();
_dataService.LoadChartItems(NetworkBandwidthLoaded, NetworkBandwidthLoadedFailed);
_dataService.LoadPortalStatus(NetworkPortalStatusLoaded, NetworkPortalStatusLoadedFailed);
}
The error lies at LoadPortalStatus(). LoadChartItems() is fine. NetworkBandwidthLoaded and NetworkPortalStatusLoaded are delegates.
NetworkPortalStatusLoaded is laid out pretty much the same as NetworkBandwidthLoaded:
private void NetworkPortalStatusLoaded(IEnumerable<ChartModel> portalStatItems)
{
NbicNetworkPortalStatus.Clear();
var networkPortalItems = from item in portalStatItems
where ((NetworkPortalStatusModel)item).Unit == "Portal"
select item;
foreach (var item in networkPortalItems)
{
NbicNetworkPortalStatus.Add((NetworkPortalStatusModel)item);
}
Message = "Network Portal details loaded";
}
My DataServiceNetworkHealth class is defined as:
namespace MyProject.DataServices
{
public class DataServiceNetworkHealth : IDataService
{
private Action<IEnumerable<ChartModel>> _delagateSuccess;
private Action<Exception> _delagateFail;
private String _portalHtmlResponse;
public void LoadChartItems(Action<IEnumerable<ChartModel>> success, Action<Exception> fail)
{
....
}
public void LoadPortalStatus(Action<IEnumerable<ChartModel>> success, Action<Exception> fail)
{
....
}
}
}
Method LoadChartItems() is defined in the interface IDataService, but LoadPortalStatus is not. Perhaps that's where the problem lies. There are other DataServiceX classes that do not use LoadPortalStatus.
I know this is a long post, but I thought better to give all info up front. :-) Thanks for any help!
解决方案 Your reference to _dataService
is of the type IDataService
, which means that you can only access members defined by this interface (or extension methods for said interfae). You mention that LoadPortalStatus
isn't part of that interface and thus you cannot access it through the _dataService
reference.
Updated based on comment: Let's look at another example. If you define a List<int>
reference like this:
List<int> i = new List<int>();
you can access all the members of List<T>()
and all the members of Object
. However, if you change the reference to
IEnumerable<int> i = new List<int>();
the only accessible members through i
are GetEnumerator
which is defined on IEnumerable<T>
and the members inherited from Object
.
If you include the System.Linq
namespace, you also get access to the numerous extension methods defined for IEnumerable<T>
.
The static type of i
determines which members are available through that reference.
这篇关于“不包含定义...和没有扩展方法..”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!