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

C#-EF代码首先实现的接口属性【代码】

我有以下模型.interface IKeywordedEntity {IEntityCollection<Keyword> Keywords { get; } } class Foo : EntityBase, IKeywordedEntity {public virtual IEntityCollection<Keyword> Keywords { get { ... } } } class Bar : EntityBase, IKeywordedEntity {public virtual IEntityCollection<Keyword> Keywords { get { ... } } }我想编写一个扩展方法,该方法自动在OnModelCreating中为每个关键字处理关键字.public static void...

c#-如果接口位于不同的程序集中,则SimpleIoc.Default.Register在IsInDesignModeStatic处失败【代码】

在ViewModelLocator类的静态构造函数中,SimpleIoc.Default.Register< T>();如果接口在其他项目中,则在IsInDesignModeStatic模式下失败.结果,MainWindow.xaml设计器在设计时为空. 我提出了一个简单的解决方案进行测试.我所做的唯一更改是将DataItem类和IDataService接口移到Domain项目中. Download here 我找到了一种解决方法:在ClientWpf项目中添加指向IDataService.cs的链接.public class ViewModelLocator {static ViewModelLoc...

首页> C#>如何序列化包含接口的不可修改的类?【代码】

我正在尝试序列化包含接口的对象.但是,接口无法序列化.通常,我会使用类似NonSerialized标记的名称,但是我无法弄清楚如何将此属性应用于无法修改的类,例如预定义的.NET类之一(例如:System.Diagnostics.Process). 例如,考虑以下代码:using System.Diagnostics using System.Xml.Serialization;class Program {static void Main(string[] args){try{XmlSerializer x = new XmlSerializer(typeof(Process));}catch (Exception e){Con...

在C#中,接口可以实例化吗?【代码】

我正在阅读here中的代码.我发现私有ITreeModel _model;在TreeList.cs中:namespace Aga.Controls.Tree {public class TreeList: ListView{#region Properties//...private ITreeModel _model;public ITreeModel Model{//...}//...} }ITreeModel是ITreeModel.cs中的接口:namespace Aga.Controls.Tree {public interface ITreeModel{/// <summary>/// Get list of children of the specified parent/// </summary>IEnumerable GetCh...

具有通用接口的C#转换【代码】

我试图理解为什么在这种情况下强制转换不起作用. 假定以下接口:public interface IDomainEvent { }public interface IHandler<T> where T: IDomainEvent {void Handle(T args); }这些简单的实现:public class SomeEvent : IDomainEvent {public string Value { get; set; } }public class SomeEventHandler : IHandler<SomeEvent> {public void Handle(SomeEvent args) { } }我不明白为什么我不能这样做:var handler = new Some...

在C#中继承接口【代码】

这个问题已经在这里有了答案: > Interface vs Abstract Class (general OO) 33个可以说有一个IRun接口:public interface IRun {public bool IsRunComplete(); }作者不是直接从此接口继承,而是将这个接口的方法继承到一个类,并在需要时从该类继承.Public Class RUN : IRun {public abstract bool IsRunComplete() }通过这种方式而不是直接从接口继承可以带来什么好...

CAD关于文字样式当前文字样式操作(com接口c#语言)

当前文字样式表示向DWG数据库添加一个文字对象时,使用的默认的文字样式,通过系统变量TEXTSTYLE操作,系统变量设置参考: http://www.mxdraw.com/help_2_48.html 设置当前文字样式C#// 添加文字样式 axMxDrawX1.AddTextStyle1("MyTextStyle", "txt.shx", "hztxt.shx", 1); // 得到当前数据库 MxDrawDatabase database = (MxDrawDatabase)axMxDrawX1.GetDatabase(); // 设置当前文字样式 database.CurrentlyTextStyle = "MyTextStyl...

如何使用C#在mongo DB驱动程序2.0版中使用IMongoDatabase接口实现“ FindOne”和“ EnsureIndex”?【代码】

当我使用FindOne时,它说“’MongoClientExtensions.GetServer(MongoClient)’已过时:请改用新API.” Observer.Client”作为警告. 这是我的代码collection.EnsureIndex(IndexKeys.Ascending("Username", "Type"), IndexOptions.SetUnique(true));var userlog = collection.FindOne(Query<UserLog>.Where(ul => ul.Username == username && ul.Type == type));解决方法:我希望这可以为您的FindOne功能提供帮助public async static T...

c#-验证Moq方法调用用作基类/接口时【代码】

我正在尝试验证实现以下接口的Moq上的方法调用,但未匹配调用. 我的单元测试(简体):[Test] public void ShouldDeleteComponent() {var mockDao = new Mock<IComponentDataAccess>();Target.ComponentDao = mockDao.Object;Target.Execute();mockDao.Verify(x => x.Delete(It.IsAny<Component>()), Times.Once); }我的模拟对象的接口:public interface IComponentDataAccess : IDataAccess<Component> {int Delete(Component entit...

首页> C#> Unity:当前类型是一个接口,不能被构造【代码】

下面的代码开始public interface IDataContextAsync : IDataContext {Task<int> SaveChangesAsync(CancellationToken cancellationToken);Task<int> SaveChangesAsync(); }public partial class DB1Context : DataContext{ }public partial class DB2Context : DataContext{ }以下是UnityConfig文件.注意:我正在为ASP.Net MVC使用Nuget引导程序,以下是我的UnityConfig文件public static void RegisterTypes(IUnityContainer conta...

c#-简单注入器-将接口的特定实现注入到特定控制器【代码】

我有两个实现相同接口的搜索服务.其中一个仅用于搜索一小组内部Umbraco内容,另一个用于同时搜索Umbraco和外部内容,并且使用默认搜索引擎以外的其他内容.我希望保留它们两者,因为它们各自的性能都很好,它们都使用了它所使用的数据集. 是否可以在Simple Injector中指定何时注入哪个服务?我想为大多数控制器注册第一个控制器,基本上将其作为默认实现.然后,我只需要为两个特定的控制器注册第二个. 在浏览时,我找到了解决此问题的方法,...

c#-具有接口的实体框架代码优先迁移【代码】

我正在尝试使用属性/注释方法(以接口作为属性类型)运行EF代码优先迁移.我们已经建立了带有接口的完整基础架构,并正在使用这些接口来实现具体的类并希望启用迁移. EF似乎并没有正确地关联外键关系.有什么办法可以纠正这个问题? 这是一个例子: 我有一个IStateProvince接口,如下所示:public interface IStateProvince {string Abbreviation { get; set; }string Name { get; set; }ICountry Country { get; set; }}我也有一个ICoun...

c#-模拟一个接口{ }(Moq)【代码】

我有一个IUnitOfWork接口,其中包含到我们所有存储库的映射,如下所示:public interface IUnitOfWork : IDisposable {IRepository<Client> ClientsRepo { get; }IRepository<ConfigValue> ConfigValuesRepo { get; }IRepository<TestRun> TestRunsRepo { get; }//Etc... }我们的IRepository类如下所示:public interface IRepository<T> {T getByID(int id);void Add(T Item);void Delete(T Item);void Attach(T Item);void Update(...

如何在VB6和C#之间共享接口?

我希望能够编写一个可以在C#和VB6类中实现的类接口,以便可以在VB6代码中以相同的方式处理这些类,但我无法完成这项工作. 在VB6中,我希望使用Implements关键字实现类VB6Class来实现某些接口ISharedInterface. 在C#中,我希望有一些其他的类C#Class,可以在实现ISharedInterface的同时向COM公开. 目的是使VB6代码将能够通过ISharedInterface在VB6Class和C#Class上进行操作,而无需关心类所使用的语言. 我想将此技术用作通过依次重写VB6端...

c#-为什么可以通过接口进行处理【代码】

根据MSDN,我已经在C#中进行接口研究一段时间了 “接口更适合于您的应用程序需要许多可能不相关的对象类型来提供某些功能的情况.” https://msdn.microsoft.com/en-in/library/3b5b8ezk(v=vs.90).aspx 当实现Dispose()而不是使用接口IDisposable时,我可以简单地定义3Dispose()方法给我的用户.我的问题是“ Microsoft为什么创建IDisposable接口,以及使用Interface来实现Dispose()的目的是什么”. 这就是我的意思//This method is use...