【c# – 左侧接口】教程文章相关的互联网学习教程文章

c# – 在View中使用接口类型作为模型并使用实际类型属性和验证的最佳实践【代码】

我有这样的界面:public interface IFoo {decimal Amount { get; set; } }我有一些视图模型实现它:public class Foo1 : IFoo {[Display(Name = "Foo1 Amount")][Range(6, 11)]public decimal Amount { get; set; } }public class Foo2 : IFoo { [Display(Name = "Foo2 Amount")][Range(1, 5)]public decimal Amount { get; set; } }我不想为Foo1和Foo2中的每一个创建新视图. 所以,我创建了一个具有IFoo类型模型的视图.@mode...

c#-泛型-接口协变、抗变【代码】【图】

代码 using System; // 协变 public interface IA<out T> where T:new() {T fun();//void fun(T t);//err } // 抗变 public interface IB<in T>{void fun(T t);//T fun();//err } class A:IA<C>{public C fun(){return new C();} } class B:IB<C>{public C c;public void fun(C c){this.c = c; } } class C{public int a = 5; } public class Fanxing{public static void Main(){Console.WriteLine("hello word");A a = new A()...

c# – 在没有警告的情况下隐藏基类的显式接口实现的目的?【代码】

想象一下这些规范来自外部dll.一个显式实现接口的类:public interface IDebug {string GetImportantInfo(); } public class ExternalClass : IDebug {public void DoSomethingImportant(){System.Diagnostics.Debug.WriteLine("Something important was done...");}string IDebug.GetImportantInfo() //Explicit implementation{DoSomethingImportant();return nameof(ExternalClass);} }然后这个来自内部代码,您知道需要实现该接...

c# – 在接口上拦截【代码】

我试图创建类似IAuditable接口的东西,它充当Ninject拦截调用的标记. 假设我有以下内容:public interface IAuditable {}public interface IProcessor {void Process(object o); }public class Processor : IProcessor, IAuditable {public void Process(object o){Console.WriteLine("Processor called with argument " + o.ToString());} }使用此设置:NinjectSettings settings = new NinjectSettings() { LoadExtensions = true...

C# 排序ICompare<>接口的使用【代码】【图】

IComparer<T> 这是一个接口,是用来给对象中的某一个属性经行排序 接口的使用 - 文章图片" />接口的使用 - 文章图片" /> 1 class NameDESC:IComparer<StudentExt>2 {3 public int Compare(StudentExt x,StudentExt y)4 {5 return y.StudentName.CompareTo(x.StudentName);6 }7 }8 //按照学号降序排列9 class StudentIdDESC:ICompaner<StudentExt> 10 { 11 public int Compare(StudentExt x,StudentExt ...

c# – 具有多个NIC的UDP多播仅在一个接口处于活动状态时有效【代码】

我看了一遍,找不到解决这个问题的方法.我已经尝试过我能看到的所有组合而没有运气. 基本上,我想选择一个接口,在两台机器上启动UDP客户端和发送/接收消息.当只有一个NIC处于活动状态时,一切正常,但当两个处于活动状态时,它将停止工作.我看过Wireshark并且有一个NIC可以看到进出的数据包. 现在,当我使用两个网卡时,我只能从第一个枚举的网卡中接收TX,并且无法接收任何网卡.当两个NIC都处于活动状态时,WireShark不会在端口上显示任何收...

c# – 如何使用逆变参数将通用接口转换为基类型?【代码】

我正在尝试开发一个通用命令处理器.我想创建实现给定接口的命令处理程序类.我将使用控制反转来根据接收到的命令类型动态创建相应类的实例.然后我想以通用的方式调用类的“Execute”方法. 我能够使用协变类型参数来完成这项工作,但在这种情况下,我不能使用泛型类型参数作为方法参数. 似乎逆变方法应该起作用,因为它允许我根据需要声明方法参数,但不幸的是,类的实例无法转换为基本接口. 下面的代码举例说明了这个问题:using System;...

c# – 在不知道coclass的情况下发布COM接口【代码】

我有一个C#客户端,它使用本机C COM服务器DLL的接口.该DLL实现了4个接口.这4个接口由DLL中的4个coclasses实现.但是只有1个coclass暴露给客户端.接口2,3,4通过接口1中的一种方法返回给客户端. C COM服务器:interface IFace1: IUnknown{ HRESULT CreateOtherInterface([in] REFIID iidFace, [out, iid_is(iidFace)] void** ppOut); };coclass ClassIFace1 {[default] interface IFace1; };C#客户端:ClassIFace1 Face1Obj = new Clas...

c# – 是否可以强制ReSharper实现ICloneable接口?

是否有可能使用ReSharper 6.1让我的类实现ICloneable接口? 怎么样?解决方法:不幸的是,Resharper中没有这样的功能.还没.你最想要的是Extract Interface选项.它增加了一个不存在的新界面.

c# – 关于类或对象的接口隔离原则是什么?【代码】

提醒(从wiki):The interface-segregation principle (ISP) states that no client shouldbe forced to depend on methods it does not use.现在看看我的例子. 这是我可变的实体.它是从某个地方编辑的,它能够通过只读接口通知有关更改:interface ICounter {event Action<int> NewNumber; }class Counter : ICounter {public event Action<int> NewNumber;int number = 0;public void IncrementAndSend(int x){number += x;if (New...

CAD返回曲线组成的闭合区域面积(com接口c#语言)

IMxDrawCurve::GetArea 返回曲线组成的闭合区域面积,具体详细如下: 参数说明[out] DOUBLE* pArea返回闭合区域面积 C#代码计算闭合区域面积: ????????MxDrawPolyline?pl?=?new MxDrawPolyline(); ????????MxDrawPoint?pt1?=?new MxDrawPoint(); ????????pt1.x?=?10; ????????pt1.y?=?10; ????????MxDrawPoint?pt2?=?new MxDrawPoint(); ????????pt2.x?=?20; ????????pt2.y?=?20; ????????MxDrawPoint?pt3?=?new MxDrawPoint...

CAD绘制椭圆填充(com接口c#语言)

c#代码实现如下:????????????????MxDrawEllipse?ellipse?=?new MxDrawEllipse(); ????????????????MxDrawPoint?cen?=?new MxDrawPoint(); ????????????????cen.x?=?100; ????????????????cen.y?=?200; ????????????????ellipse.Center?=?cen;???????????????? ????????????????MxDrawVector3d?majAxis?=?new MxDrawVector3d(); ????????????????majAxis.x?=?100; ????????????????majAxis.y?=?0; ????????????????ellipse.Maj...

c# – 将接口传递给RESTFUL API调用【代码】

最近,我有一个面试问题,显示的问题是传递参数作为其界面.现在,我一直认为你必须通过混凝土,因为没有办法知道实现哪个实例化.另外,我总是认为你“可以”返回接口……但你应该返回混凝土(同样). 问:参数化接口是个坏主意吗?问:返回界面“没问题”?问:如果存在多个派生,您将如何知道实例化哪个派生? 更新 – 让它更清晰很抱歉澄清…… 如果我将其发送到服务器: – 它如何知道实例化哪个派生? (这应该失败……对吧?)var cust...

c# – 在Ninject中找到通用接口的具体实现【代码】

我有一个具有特定实现的通用接口,如下所示:public class Animal { } public class Horse : Animal { } public class Dog : Animal { }public interface Vet<in T> where T : Animal {void Heal(T animal); }public class HorseVet : Vet<Horse> {public void Heal(Horse animal){Console.WriteLine("Healing a horse");} }public class DogVet : Vet<Dog> {public void Heal(Dog animal){Console.WriteLine("Healing a dog");} }现...

c# – 在接口级别强制执行getter setter访问【代码】

我想在接口级别对getter或setter强制访问属性,以便在实现它的类中遵循相同的访问权限.我想做类似下面的事情:public interface IExample {string Name{get;internal set;} }public class Example : IExample {private string _name = String.Empty;string Name{get{return _name;}internal set{_name = value;}} }但不幸的是,据我所知,C#中不允许这样做.我认为这是因为界面只是为了揭示公共访问的内容(我没有丝毫想法!). 我需要的...