Tag: 界面

什么是C#中的界面强制转换?

我确实理解如何编写一个接口在C#中工作,例如这里描述的: codeguru解释 interface IIntelligence { bool intelligent_behavior(); } class Human: IIntelligence { public Human() { //…………. } /// Interface method definition in the class that implements it public bool intelligent_behavior() { Console.WriteLine(“……..”); return true } } 但是我对以下接口转换过程感到困惑: Human human = new Human(); // The human object is casted to the interface type IIntelligence humanIQ = […]

实现接口但将成员更改为私有

默认情况下,接口的所有成员都是公共的。 但是我的界面中有一些属性我想用作实现我的界面的一些子类的私有成员。 这是可以而且已经完成的事情,还是我在这里的基础。 这些天我正在我的架构中使用更多接口,所以我还不是很精通。

无法将List 传递给期望List 的方法,其中Foo:IFoo

我有一个实现IFoo接口的类Foo 。 我有一个方法将List作为参数。 但是,它无法从List转换为List – 这让我感到惊讶,因为Foo实现了IFoo接口。 我怎样才能解决这个问题,为什么会出现这种情况? (总是很好地从错误中吸取教训)

接口属于自己的文件

按照经验,我通常将类放在自己的文件中。 Visual Studio似乎鼓励这一点,但接口方面的内容是什么? 例如 我有Class Foo实现接口Bar public interface IBar { } public class Foo : IBar { } 将这些组合在同一个文件中似乎很自然,直到另一个类实现接口,但是将文件专用于2行代码似乎过多但正确。 什么是合适的?

C#如何使用接口

这是一个相对直截了当的问题。 但我想知道通过使用接口访问单独项目中的方法的正确用法是什么。 项目 : Test.ClassLibrary 界面 : public interface ITest { string TestMethod(); } 分类 : public class Test : ITest { public string TestMethod() { return “Test”; } } 项目: Test.Web 控制器 : public class HomeController : Controller { private ITest test; public ActionResult Index() { return Content(test.TestMethod()); } } 以上返回NullReferenceException 。 我假设它是因为控制器到达接口,并且不知道接下来要去哪里。 解决这个问题的最佳方法是什么? […]

可索引的界面

如果我只想索引一个类型的实例,应该使用什么C#接口? 我不需要(或想要)添加/删除/编辑元素的能力。 枚举是可以的。 这是否需要自定义IIndexable类型? 在这种情况下, IList是过度杀伤,因为它强制实施我不想拥有的成员。

IEnumerable 作为WCF方法的返回类型

如果我定义一个Test对象,使用string和Datetime属性并使用它来返回WCF中的IEnumerable(T)集合 [OperationContract] IEnumerable GetTestNotes(); 当从客户端调用服务时,我看到它正在将IEnumerable转换为Test []: public Test[] GetTestNotes() { return base.Channel.GetTestNotes(); } 我能够获得数据。 问题是:使用IEnumerable(T)接口而不是具体的List(T)有多可靠? 使用这些WCF服务的客户端不仅在.NET中,而且在JAVA中。

我如何知道何时在忽略inheritance的接口的类型中直接实现接口?

出现的问题是当我有一个实现接口的类,并扩展实现接口的类时: class Some : SomeBase, ISome {} class SomeBase : ISomeBase {} interface ISome{} interface ISomeBase{} 由于typeof(Some).GetInterfaces()返回带有ISome和ISomeBase的数组,我无法区分ISome是实现还是inheritance(如ISomeBase)。 作为MSDN我不能假设数组中接口的顺序,因此我迷路了。 方法typeof(Some).GetInterfaceMap()也不区分它们。

通用接口列表

如果我有一个带有几个实现类的通用接口,例如: public interface IDataElement { int DataElement { get; set; } T Value { get; set; } } public class IntegerDataElement : IDataElement { public int DataElement { get; set; } public int Value { get; set; } } public class StringDataElement : IDataElement { public int DataElement { get; set; } public String Value […]

如何使用reflection来查找实现特定接口的属性?

考虑这个例子: public interface IAnimal { } public class Cat: IAnimal { } public class DoStuff { private Object catList = new List(); public void Go() { // I want to do this, but using reflection instead: if (catList is IEnumerable) MessageBox.Show(“animal list found”); // now to try and do the above using reflection… PropertyInfo[] properties […]