【c#-为什么可以通过接口进行处理】教程文章相关的互联网学习教程文章

需要帮助以C#定义接口

我有一个数据驱动的映射应用程序,需要在其中实现自定义功能作为插件.我需要执行的自定义方法的名称也将出现在映射数据中.我知道我可以使用invoke命令来调用该方法.但是,如何确保每种方法都具有适当的签名?解决方法:首先,我认为您不应为覆盖的方法允许使用任意名称-只需定义一个接口并使其简单即可. 您可以使用一个方法定义接口,该方法将委托返回给执行工作的方法. 否则,您将只需要使用反射来获取映射方法的MethodInfo并在运行时检...

c#-将接口数组转换为结构数组时,隐式强制转换无效【代码】

我有一个实现某些接口的结构.直到我有一个struct实现的数组,并尝试将该数组隐式转换为接口类型的另一个数组,此方法才能正常工作. (请参见下面的代码示例)using System.Collections.Generic;namespace MainNS {public interface IStructInterface{string Name { get; }}public struct StructImplementation : IStructInterface{public string Name{get { return "Test"; }}}public class MainClass{public static void Main(){Struc...

c#-解决一个接口中的歧义,该接口扩展了其他两个接口的名称相同的方法【代码】

如果我有接口:public interface ANewThing { IKey Key { get; } }public interface AnOldThing { object Key{ get; } }public interface ACompositeThing : ANewThing , AnOldThing { }我这样写:ACompositeThing compositeThing = GetCompositeThing(); Trace.WriteLine(compositeThing.Key);它无法编译,抱怨对Key的调用是模棱两可的(如果Key属性返回的类型相同,则没有任何区别).我知道在实现ACompositeThing的类中我可以显式...

java调用C# webService发布的接口【代码】【图】

java调用C# webService发布的接口 java调用C# webService方式有很多种我这里只介绍一种 首先需要引入axis的jar包 axis的maven坐标如下 <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version></dependency> 也可以到这个地址搜索然后复制坐标输入即可 https://mvnrepository.com/ 下面是调用列子代码: 1 //接口地址2 String oaurl = Preferences.getInsta...

C# 后台请求接口方式【代码】

直接上代码:/// <summary> /// 后台请求接口方法 /// </summary> public class myHttpRequest {/// <summary>/// get方法调用接口获取json文件内容/// </summary>/// <param name="url"></param>/// <returns></returns>public string GetFunction(string url){string serviceAddress = url;//"http://222.111.999.444:8687/tttr/usercrd/12/b7e50cb45a?userid=9999";HttpWebRequest request = (HttpWebRequest)WebRequest.Create...

C#-将DLL加载到具有唯一已知公共接口的单独AppDomain中【代码】

我需要在另一个域中加载.dll(plugins).在主应用程序中,我对插件类型一无所知,只是它们使用某些方法实现了通用接口ICommonInterface.因此此代码无济于事,因为我无法创建具有接口类型的实例.AppDomain domain = AppDomain.CreateDomain("New domain name"); //Do other things to the domain like set the security policystring pathToDll = @"C:\myDll.dll"; //Full path to dll you want to load Type t = typeof(TypeIWantToLoad...

c#-实体框架代码优先-接口【代码】

我想为餐厅制作应用程序.关于计算配方. 我有域类:-成分– 食谱-RecipeItem 配方具有RecipeItem列表. RecipeItem可以是成分,也可以是配方. 所以我正在使用具有2个属性(Id,名称)的Interface IItem.如果我在课堂上使用Interface,则db generator会忽略此字段. 在这里查看我的课程:public class Ingredient : IItem{public int Id { get; set; }public string Name { get; set; }public double Price { get; set; }}public class Reci...

C#-Shared.CellItem’没有实现接口成员’System.IDisposable.Dispose()【代码】

我是C#的新手,我正在处理内存占用大量对象的对象,因为我已经执行了内存分析,并且需要处理一些资源并为GC调用finalize方法.但是IDisposable无法实现我的类,这是为什么?我应该如何在班上实施IDispose?public class CellItem: IDisposable{public int MedicationDispenseId { get; set; }public Enumerations.Timeslot Timeslot { get; set; }public DateTime DateAdministered { get; set; }public void dispose() {if (this.Medic...

c#-使用简单注入器解析通用接口【代码】

想知道是否有可能实现以下目标:container.GetInstance<IWordFacade<,,,>>();到目前为止,我还没有做到.以下是一些代码示例:IWordFacade<T1,T2,T3,T4>{T1 DoSomething(T2); }public class ConcreteFacade1 : IWordFacade<int,long,double,decimal>{int DoSomething(long param){//....} }public class ConcreteFacade2 : IWordFacade<short,string,float,char>{short DoSomething(string param){// ...} }…考虑到这些类型/类,我试...

在c#中的每个派生类中都有一个更具体的接口【代码】

我试图给出一个尽可能简单的例子,但是这个问题的本质至少使我感到困惑. 为了重用代码而不重复我的自我,我有一个实现另一个接口IPerson的接口IStudent.interface IPerson {string FirstName { get; set; }string LastName { get; set; } }interface IStudent : IPerson {int StudentNumber { get; set; } }我有一个实现IStudent的类class Student : IStudent {public string FirstName { get; set; }public string LastName { get; ...

将接口映射到(动态地)不实现接口C#的Class【代码】

这是我在Stackoverflow中的第一篇文章.多亏了Stackoverflow. 我的问题: 我在类库中有一个接口.namespace TestClient {public interface IMobileStore{List<string> GetMobileDetails();int AddMobile(string mobile);} }我在另一个类库中有一个名为MobileLibrary的类,并实现了IMobileStore接口(在开发时)namespace MobileLibrary {public class MobileLibrary : IMobileStore{List<string> GetMobileDetails(){return new List<s...

c#-将IOC容器用作MVC5的依赖项解析器会抛出“无法创建接口实例”错误【代码】

我试图简单地使用IOC容器(目前为ninject)作为MVC5的依赖项解析器.以前在MVC4,Visual Studio 2012中可以正常工作,但是现在使用VS2013和MVC5,我只是无法让解析器在我的控制器中注入依赖项.这不是ninject特有的,我也尝试过SimpleInjector和Unity -同样的错误 我只希望能够将此类插入我的家庭控制器中.public interface ITest{void dummyMethod();}public class Test : ITest{public void dummyMethod(){};}这是依赖解析器public class...

c#-有什么方法可以创建无法在程序集之外实现的公共.NET接口?【代码】

为了维护.NET中的二进制向后兼容性,通常不能向公共类和接口添加新的抽象方法.如果这样做,则针对扩展/实现您的类/接口的程序集的旧版本构建的代码将在运行时失败,因为它无法完全扩展/实现新版本.但是,对于类,有一个方便的解决方法:public abstract class Foo {internal Foo() { } }因为Foo的构造函数是内部的,所以我程序集中的任何人都不能扩展Foo.因此,我可以向Foo添加新的抽象方法而不必担心向后兼容性,因为我知道另一个程序集中...

c#-获取在Unity中实现接口的所有类型【代码】

如果您只想知道解决方案,请跳至UPDATE: 我有一个使用以下代码来获取和运行许多辅助方法的应用程序var type = typeof(IJob); var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).Where(x => x.IsClass && type.IsAssignableFrom(x));foreach (Type t in types) {IJob obj = Activator.CreateInstance(t) as IJob;obj.Run(); }此代码按原样完美工作.但是,一些较新的作业利用依赖项注入来填充其构造...

c#-FakeItEasy-伪造的接口继承自抽象,而两者共享相同的接口继承【代码】

我有一个界面public interface IInterface { void DoSomething(); }另一个界面public interface IOtherInterface : IInterface { }抽象类public abstract class AbstractClass : IInterface {public void DoSomething(){Console.WriteLine("Got here");} }我正在编写单元测试和伪造的IOtherInterface.抽象类已经包含了一些我想在单元测试中利用的有用方法.我如何制作A.Fake< IOtherInterface>();从AbstractClass继承? 到目前为止,...