C#unit testing – Thread.Sleep(x) – 如何模拟系统时钟

我必须测试一个间隔后做一定工作量的方法。 while (running) { … // Work … Thread.Sleep(Interval); } Interval作为参数传递给类,所以我可以传入0或1,但我对如何模拟系统时钟感兴趣,如果不是这样的话。 在我的测试中,我希望能够通过TimeSpan Interval简单地设置时间并让线程唤醒。 我从来没有编写过代码,这些代码之前会对执行的线程起作用,我确信有一些缺陷需要避免 – 请随意详细说明你使用的方法。 谢谢!

如何在Windows 10 UWP中复制和调整图像大小

我使用http://www.codeproject.com/Tips/552141/Csharp-Image-resize-convert-and-save中的代码以编程方式调整图像大小。 但是,该项目使用System.Drawing库,这些库不适用于Windows 10应用程序。 我尝试使用Windows.UI.Xaml.Media.Imaging的BitmapImage类,但它似乎没有提供在System.Drawing中找到的function。 有没有人能够在Windows 10中resize(缩小)图像? 我的应用程序将处理来自多个源,不同格式/大小的图像,我试图调整实际图像的大小以节省空间,而不是让应用程序resize以适应显示它的图像。 编辑 我已经修改了上面提到的链接中的代码,并且有一个hack可以满足我的特定需求。 这里是: public static BitmapImage ResizedImage(BitmapImage sourceImage, int maxWidth, int maxHeight) { var origHeight = sourceImage.PixelHeight; var origWidth = sourceImage.PixelWidth; var ratioX = maxWidth/(float) origWidth; var ratioY = maxHeight/(float) origHeight; var ratio = Math.Min(ratioX, ratioY); var newHeight = (int) (origHeight * ratio); var newWidth = (int) (origWidth […]

Gridview使用通用列表作为DataSource和自动生成列

我正在寻找一个带有通用列表的GridView,并自动生成列。 我得到一个例外,它没有正确的属性来允许它自动生成列。 例外 The data source for GridView with id ‘GV1’ did not have any properties or attributes from which to generate columns. Ensure that your data source has content. 网格视图 页面加载 //LINQ query to populate list List su = new List(); dbDataContext db = new dbDataContext(); var q = from c in db.data_table where […]

该类型出现在单个LINQ to Entities查询中的两个结构不兼容的初始化中

我正在尝试构建像条件查询之类的东西来从底层数据库中获取所需的数据。 目前我有以下查询(工作正常) var eventData = dbContext.Event.Select(t => new { Address = true ? new AnonymousEventGetAddress { AddressLine1 = t.Address.AddressLine1, CityName = t.Address.AddressCityName } : new AnonymousEventGetAddress(), }); 如果我改成它 var includeAddress = true; // this will normally be passed as param var eventData = dbContext.Event.Select(t => new { Address = includeAddress ? new AnonymousEventGetAddress { AddressLine1 […]

如果打开自定义错误,是否会触发global.asax Application_Error事件?

如果在Web配置中将自定义错误设置为RemoteOnly – 这是否意味着global.asax中的MVC应用程序级别错误事件 – Application_Error不会因错误而触发? 我刚刚注意到,当我的应用程序中发生某个错误,并且我正在远程查看该站点时,不会记录任何错误。 但是,当我访问服务器上的应用程序并发生相同的错误时,将记录错误。 这是自定义错误配置设置: 编辑 只是出于对人们的兴趣 – 我最终完全关闭了自定义错误并在Application_Error处理重定向,如下所示: protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); // … log error here var httpEx = exception as HttpException; if (httpEx != null && httpEx.GetHttpCode() == 403) { Response.Redirect(“/youraccount/error/forbidden”, true); } else if (httpEx != null && httpEx.GetHttpCode() == 404) […]

DbContext已经处理完毕

我使用ASP.NET MVC 4和SQL Server 2008开发了一个Web应用程序,我创建了ContextManager类,在所有页面中只有一个数据库上下文。 public static class ContextManager { public static HotelContext Current { get { var key = “Hotel_” + HttpContext.Current.GetHashCode().ToString(“x”) + Thread.CurrentContext.ContextID.ToString(); var context = HttpContext.Current.Items[key] as HotelContext; if (context == null) { context = new HotelContext(); HttpContext.Current.Items[key] = context; } return context; } } } 它在大多数页面中都能正常工作,但是在注册页面出现了问题,我的上下文因以下错误而被废弃: 由于已经处理了DbContext,因此无法完成操作。 public ActionResult Register ( […]

无法更新数据库,因为它是只读的

我已经设置了我的网站以使用ASP.NET成员资格。 尝试在我的开发机器上使用它时一切正常但是当我把它放在Web服务器上并尝试登录时,我收到此错误: “Failed to update database “C:\INETPUB\WWWROOT\APP_DATA\ASPNETDB.MDF” because the database is read-only. “

将C#对象转为json字符串,如何处理双引号

我正在使用Newtonsoft.Json将对象解析为json字符串。 它会像这样返回一些思考: {\”code\”:-1,\”idName\”:\”empty\”,\”idValue\”:0,\”message\”:\”Failed,can not read object from body\”} 它不是一个有效的json字符串我认为,任何人都可以帮我工作? 我想要的是这样的: {“code”:-1,”idName”:”empty\”,\”idValue\”:0,\”message\”:\”Failed,can not read object from body\”} public static class CommonUtility { // format response string public static string FormatResponseString(int code, string idName, long idValue, string message) { ResponseString rs = new ResponseString(); rs.code = code; rs.idName = idName; rs.idValue = idValue; rs.message = message; string […]

以编程方式将属性添加到方法或参数

我可以使用TypeDescriptor.AddAttributes在运行时向属性添加属性。 如何为方法和参数执行相同操作? (也许是2个单独的问题……)

MS C#编译器和非优化代码

注意:我发现我发布的示例中有一些错误 – 编辑修复它 如果你不启用优化,官方的C#编译器会做一些有趣的事情。 例如,一个简单的if语句: int x; // … // if (x == 10) // do something 如果优化,会变成以下内容: ldloc.0 ldc.i4.s 10 ceq bne.un.s do_not_do_something // do something do_not_do_something: 但如果我们禁用优化,它会变成这样: ldloc.0 ldc.i4.s 10 ceq ldc.i4.0 ceq stloc.1 ldloc.1 brtrue.s do_not_do_something // do something do_not_do_something: 我无法理解这一点。 为什么所有额外的代码,似乎在源中不存在? 在C#中,这相当于: int x, y; // … // y = x […]