草庐IT

TYPE_CLASS_DATETIME

全部标签

c# - DateTime.AddDays() 未按预期工作

我有这个简单的程序:DateTimeaux=newDateTime(2012,6,12,12,24,0);DateTimeaux2=newDateTime(2012,6,12,13,24,0);aux2.AddDays(1);Console.WriteLine((aux2-aux).TotalHours.ToString());Console.ReadLine();我对此进行了调试,发现aux2.AddDays(1);似乎不起作用,我在这里缺少什么?它应该返回25,但答案是1。问题是什么?AddHours也不起作用,我猜其他人也不起作用。 最佳答案

c# - 为什么 typeA == typeB 比 type == typeof(Type B) 慢?

我最近一直在优化/对一些代码进行基准测试并遇到了这个方法:publicvoidSomeMethod(TypemessageType){if(messageType==typeof(BroadcastMessage)){//...}elseif(messageType==typeof(DirectMessage)){//...}elseif(messageType==typeof(ClientListRequest)){//...}}这是从其他地方的性能关键循环调用的,所以我很自然地假设所有这些typeof(...)调用都增加了不必要的开销(我知道这是一种微优化)并且可以移动到类中的私有

c# - 为什么 GetType() 为 Nullable<DateTime> 返回 DateTime 类型

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Nullabletypeisnotanullabletype?在下面的代码中:DateTime?dt=DateTime.Now;MessageBox.Show(dt.GetType().ToString());消息框显示“System.DateTime”,而不是Nullable.下面也返回false(因为GetType错误):if(dt.GetType().IsAssignableFrom(typeof(DateTime?)))...(顺便说一下,使用DateTime?或Nullable没有区别)在监wind

c# - 简易喷油器 : Registering a type with constructor argument that's based on its parent

我目前正在从我的项目中删除Ninject,并转而使用SimpleInjector,但有一件事我无法正常工作。对于我的日志记录,在注册服务时,我以前能够将参数传递到我的日志记录类中_kernel.Bind().To().WithConstructorArgument("name",x=>x.Request.ParentContext.Request.Service.FullName);我正在寻找一种在SimpleInjector中重新创建它的方法。到目前为止,除了这个,我还有其他所有工作。通过执行以下操作,我可以使日志记录正常工作,尽管没有显示正确的记录器名称:_container.Re

c# - 如何在 DateTime 值数组中查找平均日期/时间

如果我有一组DateTime值:ListarrayDateTimes;如何找到其中的平均DateTime?例如,如果我有:2003-May-2115:00:002003-May-2119:00:002003-May-2120:00:00平均值应该是:2003-May-2118:00:00 最佳答案 如果你有大列表你可以使用下面的方法varcount=dates.Count;doubletemp=0D;for(inti=0;i 关于c#-如何在DateTime值数组中查找平均日期/时间,我

c# - 如何修复 'T' 是 'type parameter' 但被用作 'variable' 编译错误

我需要检查泛型类型参数T是MyEntity还是它的子类。下面的代码会导致这个编译器错误:'T'isa'typeparameter'butisusedlikea'variable'如何修复?publicclassMyEntity{}staticvoidTest(){//Error34'T'isa'typeparameter'butisusedlikea'variable'if(TisMyEntity){}} 最佳答案 您可以使用IsAssignableFromType上的方法检查是否有一个Type可以分配给另一个。if(typeof(

使用 Json.Net : Error converting value to type 的 C# 枚举反序列化

我正在使用Json.NET序列化/反序列化一些JSONAPI。API响应有一些整数值映射到应用程序中定义的枚举。枚举是这样的:publicenumMyEnum{Type1,Type2,Type3}并且JSONAPI响应具有以下内容:{"Name":"abc","MyEnumValue":"Type1"}有时,API会为我的枚举中未定义的MyEnumValue字段返回一个值,如下所示:{"Name":"abc","MyEnumValue":"Type4"}抛出异常:Errorconvertingvalue"Type4"totype'MyEnum'有没有办法通过分配默认值或其他方法来避免应

c# - 通用扩展方法 : Type argument cannot be inferred from the usage

我正在尝试创建一个适用于类型化数据表的通用扩展方法:publicstaticclassExtensions{publicstaticTableTypeDoSomething(thisTableTypetable,paramExpression>[]predicates)whereTableType:TypedTableBasewhereRowType:DataRow{//dosomethingtoeachrowofthetablewheretherowmatchesthepredicatesreturntable;}[STAThread]publicstaticvoidmain(){M

c# - 在关注性能时,为 DateTime.Today 使用变量有多重要?

我刚看到thisupvotedcommentIIRCDateTime.Todayisaquiteexpensivecall,soyoubetterstorethevalueinavariablefirst.这是对包含代码的帖子的回应:varfirst=newDateTime(DateTime.Today.Year,DateTime.Today.Month,1).AddMonths(-1);varlast=newDateTime(DateTime.Today.Year,DateTime.Today.Month,1).AddDays(-1);如果我希望提高性能,将DateTime.Toda

c# - "Possible multiple enumeration of IEnumerable"与 "Parameter can be declared with base type"

在Resharper5中,以下代码导致list出现警告“Parametercanbedeclaredwithbasetype”:publicvoidDoSomething(Listlist){if(list.Any()){//...}foreach(variteminlist){//...}}在Resharper6中,情况并非如此。但是,如果我将方法更改为以下内容,我仍然会收到该警告:publicvoidDoSomething(Listlist){foreach(variteminlist){//...}}原因是,在这个版本中,list只枚举一次,所以改成IEnumerable不会自动