C# 我能';无法访问c中的继承值#
我有一个如下所示的界面:C# 我能';无法访问c中的继承值#,c#,interface,C#,Interface,我有一个如下所示的界面: public interface ISelectSpace { bool ShowSpaceSelection { get; set; } IEnumerable<Space> AvailableSpaces { get; set; } } public interface ISelectSingleSpace : ISelectSpace { string Space { get; set; } string SpaceN
public interface ISelectSpace
{
bool ShowSpaceSelection { get; set; }
IEnumerable<Space> AvailableSpaces { get; set; }
}
public interface ISelectSingleSpace : ISelectSpace
{
string Space { get; set; }
string SpaceName { get; set; }
}
public static class SelectSingleSpace
{
public static void DoStuff(this ISelectSingleSpace selectSingleSpace)
{
Console.Write(selectSingleSpace.AvailableSpaces.Count());
}
}
但是,当我尝试访问变量AvailableSpaces的IEnumerables列表时,我不能像这样使用count函数:
public interface ISelectSpace
{
bool ShowSpaceSelection { get; set; }
IEnumerable<Space> AvailableSpaces { get; set; }
}
public interface ISelectSingleSpace : ISelectSpace
{
string Space { get; set; }
string SpaceName { get; set; }
}
public static class SelectSingleSpace
{
public static void DoStuff(this ISelectSingleSpace selectSingleSpace)
{
Console.Write(selectSingleSpace.AvailableSpaces.Count());
}
}
我没有正确引用变量吗
我在另一个类中像这样初始化此方法:
var selectSingleSpace = this as ISelectSingleSpace;
selectSingleSpace.DoStuff();
试试这个:
(我在派生类中使用this
关键字访问一些扩展方法时遇到了问题,可能是这样的。在下面的代码中,我尝试对此进行欺骗)
公共静态类SelectSingleSpace
{
公共静态void DoStuff(此ISelectSingleSpace selectSingleSpace)
{
IEnumerable AvailableSpaces=选择SingleSpace.AvailableSpaces;
Console.Write(AvailableSpaces.Count());
}
}
您显示的代码部分都很好。你的问题是你没有表现出来的。我已将以下代码粘贴到VS项目中,并已编译和运行:
using System;
using System.Collections.Generic;
using System.Linq;
namespace SO16390592
{
class Program
{
static void Main()
{
ISelectSingleSpace test = new Test();
test.AvailableSpaces = new List<Space>(new Space[1]);
test.DoStuff();
}
}
public class Space
{
}
public interface ISelectSpace
{
bool ShowSpaceSelection { get; set; }
IEnumerable<Space> AvailableSpaces { get; set; }
}
public interface ISelectSingleSpace : ISelectSpace
{
string Space { get; set; }
string SpaceName { get; set; }
}
public class Test : ISelectSingleSpace
{
public bool ShowSpaceSelection { get; set; }
public IEnumerable<Space> AvailableSpaces { get; set; }
public string Space { get; set; }
public string SpaceName { get; set; }
}
public static class SelectSingleSpace
{
public static void DoStuff(this ISelectSingleSpace selectSingleSpace)
{
Console.Write(selectSingleSpace.AvailableSpaces.Count());
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
名称空间SO16390592
{
班级计划
{
静态void Main()
{
ISelectSingleSpace测试=新测试();
test.AvailableSpaces=新列表(新空格[1]);
test.DoStuff();
}
}
公共类空间
{
}
公共界面选择空间
{
bool ShowSpaceSelection{get;set;}
IEnumerable可用空间{get;set;}
}
公共接口ISelectSingleSpace:ISelectSpace
{
字符串空间{get;set;}
字符串SpaceName{get;set;}
}
公共类测试:ISelectSingleSpace
{
公共bool ShowSpaceSelection{get;set;}
公共IEnumerable可用空间{get;set;}
公共字符串空间{get;set;}
公共字符串SpaceName{get;set;}
}
公共静态类SelectSingleSpace
{
公共静态void DoStuff(此ISelectSingleSpace selectSingleSpace)
{
Write(选择singlespace.AvailableSpaces.Count());
}
}
}
以下是控制台上打印的内容:
一,
以下是在线演示:
我建议您向我们展示更多的代码来说明您的问题,或者更好的是,为我们创建一个独立的可复制案例来展示您的问题。您是否
使用System.Linq代码>?我感觉是Count()
扩展方法导致了问题,而不是您的接口属性。你有错误吗?我只是将代码复制/粘贴到我的VS2012中,它编译得很好。你看到的确切错误是什么?好的。。。这一点帮助不大。这个
中有什么?让我们试试另一个方法-您确定集合中有>0个项目吗?在扩展方法中设置一个断点,并检查可枚举的内容。谢谢你的帮助,但恐怕不行