Here's the code: static class AsyncFinally { static async Task<int> Func( int n ) { try { Console.WriteLine( " Func: Begin #{0}", n ); await TaskEx.Delay( 100 ); Console.WriteLine( " Func: End #{0}", n ); return 0; } finally { Console.WriteLine( " Func: Finally #{0}", n
代码如下: static class AsyncFinally { static async Task<int> Func( int n ) { try { Console.WriteLine( " Func: Begin #{0}", n ); await TaskEx.Delay( 100 ); Console.WriteLine( " Func: End #{0}", n ); return 0; } finally { Console.WriteLine( " Func: Finally #{0}", n );
I'm reading through the TAP Whitepaper, and am confused by this sample given for implementing a timeout on page 22: "Consider a UI application which wants to download an image and disable the UI while the image is downloading. If the download takes too long, however, the UI should be re-enabled and the download should be discarded." public async void btnDownload_Click(object send
我正在阅读TAP白皮书,并且对第22页的实现超时所给出的示例感到困惑: “考虑一个UI应用程序,它希望在图像下载时下载图像并禁用UI,但如果下载时间过长,则应该重新启用UI,并且应该放弃下载。” public async void btnDownload_Click(object sender, EventArgs e) { btnDownload.Enabled = false; try { Task<Bitmap> download = GetBitmapAsync(url); if (download == await Task.WhenAny(
A coworker and I were recently doing the backend for a small application using Fluent NHibernate. We wrote our entities, mapping files, persistence manager, but for some reason we couldn't export the database schema to anything. Through the debugger we discovered that the FluentMappings.AddFromAssemblyOf was returning 0 mappings, even though they are clearly there, and clearly correct. We
一位同事和我最近正在做一个使用Fluent NHibernate的小应用程序的后端。 我们写了我们的实体,映射文件,持久性管理器,但出于某种原因,我们无法将数据库模式导出到任何东西。 通过调试器,我们发现FluentMappings.AddFromAssemblyOf返回0映射,尽管它们很清楚,并且显然是正确的。 我们尝试了所有我们能想到的事情,并最终不得不手动添加每个映射。 以下是不起作用的代码: return Fluently.Configure().Databas
I have been whipped into submission and have started learning Fluent NHibernate (no previous NHibernate experience). In my project, I am programming to interfaces to reduce coupling etc. That means pretty much "everything" refers to the interface instead of the concrete type (IMessage instead of Message). The thought behind this is to help make it more testable by being able to mock d
我已经被提交并已经开始学习流利NHibernate(没有以前的NHibernate经验)。 在我的项目中,我对接口进行了编程以减少耦合等。这意味着几乎“一切”是指接口而不是具体类型(即消息而不是消息)。 这背后的想法是通过能够嘲讽依赖性来帮助使其更具可测性。 然而,(流利)NHibernate不喜欢它,当我试图映射到接口,而不是具体的类。 这个问题很简单 - 根据Fluent Wiki,例如定义我的类的ID字段是明智的 int Id { get; private
Suppose I have this: using System; public class Program { public static void Main() { BaseClass bc = new DerivedClass(); bc.Method1(); bc.Method2(); Console.WriteLine(bc.GetType().FullName); // Output // Derived - Method1 (override) // Base - Method2 // DerivedClass } } public class BaseClass { public virtual void
假设我有这个: using System; public class Program { public static void Main() { BaseClass bc = new DerivedClass(); bc.Method1(); bc.Method2(); Console.WriteLine(bc.GetType().FullName); // Output // Derived - Method1 (override) // Base - Method2 // DerivedClass } } public class BaseClass { public virtual void Method
I've recently been assigned to some maintenance work on an existing application. I've come across the following code: public interface IEntityService<T, in TKey> { T GetEntityById(TKey id); IEnumerable<T> GetAll(); void Update(T entity); void Delete(TKey key); } I'm not sure what the in keyword does for the second generic argument, TKey . I
我最近被分配到现有应用程序的一些维护工作。 我遇到以下代码: public interface IEntityService<T, in TKey> { T GetEntityById(TKey id); IEnumerable<T> GetAll(); void Update(T entity); void Delete(TKey key); } 我不确定in关键字对于第二个泛型参数TKey 。 我遇到了以下MSDN文章(应该)向我完美地解释它: (通用修饰符)(C#参考) 但是,我并没有真正理解它。
I'm trying to implement a system that allows to read and interpret lines from a file. I need to manage different file formats. For that I have an abstract Importer class that is is inherited and implemented differently (based on the file format). The lines of a file can result in different object, so I created a generic interface that know how to parse the line, validate it, etc: public
我试图实现一个允许读取和解释文件中的行的系统。 我需要管理不同的文件格式。 为此,我有一个抽象的Importer类,它是继承和实现不同的(基于文件格式)。 一个文件的行可能导致不同的对象,所以我创建了一个通用接口,知道如何解析行,验证它等: public interface ILineImporter<ObjType> where ObjType : IImportableObject 具体的Importer类通过覆盖的抽象方法知道哪个LineImporter用于给定的行public abstract
I'm trying to develop a generic command processor. I would like to create command handler classes implementing a given interface. I'll use inversion of control to dinamically create an instance of the appropriate class based on the type of command received. Then I'd like to call the "Execute" method of the class in a generic way. I'm able to make this work using a c
我试图开发一个通用的命令处理器。 我想创建实现给定接口的命令处理程序类。 我将使用控制反转来根据收到的命令类型创建适当类的实例。 然后我想以通用的方式调用该类的“Execute”方法。 我能够使用协变类型参数进行这项工作,但在这种情况下,我不能使用泛型类型参数作为方法参数。 似乎反变法应该可行,因为它允许我根据需要声明方法参数,但不幸的是,该类的实例不能转换为基接口。 下面的代码举例说明了这个问题: u
Some trouble comes up for me while trying to port some code from java to c#. At first, some explanation of the key concept behind the java code: The key concept of the existing code is a class importing/using methods in an external library. This class implements an interface, which declares most of the methods from the external library. The benefit is the ability to create instances like In
尝试将一些代码从java移植到c#时,出现了一些问题。 首先,解释一下java代码背后的关键概念:现有代码的关键概念是外部库中的类导入/使用方法。 该类实现了一个接口,该接口声明了外部库中的大部分方法。 好处是能够创建像 Interface1 instance = new classImplementingInterface1(); 我试图移植实现接口并从外部库导入方法的代码。 实际上,我必须将该接口转换为抽象类,因为java接口使用包含预定义值的字段,这在.
I have a class which inherits an interface. An interface member method is implemented in my class without an access modifier (so, by default it's private ) . I am getting the error "cannot implement an interface member because it is not public". Why it is not allowed? Can't I override the accessibility? Here's an example of why it doesn't make sense to be able to
我有一个继承接口的类。 接口成员方法在我的类中实现,没有访问修饰符(因此,默认情况下它是私有的)。 我收到错误“无法实现接口成员,因为它不公开”。 为什么不允许? 我无法覆盖可访问性吗? 以下是为什么无法覆盖可见性的原因的一个例子: interface someI { void doYourWork(); } public class A : someI { public void doYourWork() { //... } } public class B : someI { private v