Format TimeSpan greater than 24 hour

Say I convert some seconds into the TimeSpan object like this: Dim sec = 1254234568 Dim t As TimeSpan = TimeSpan.FromSeconds(sec) How do I format the TimeSpan object into a format like the following: >105hr 56mn 47sec Is there a built-in function or do I need to write a custom function? Well, the simplest thing to do is to format this yourself, eg return string.Format("{0}hr {1}mn {2}se

格式化TimeSpan大于24小时

假设我将这些秒转换成TimeSpan对象,如下所示: Dim sec = 1254234568 Dim t As TimeSpan = TimeSpan.FromSeconds(sec) 如何将TimeSpan对象格式化为如下格式: >105hr 56mn 47sec 是否有内置函数或需要编写自定义函数? 那么,最简​​单的做法就是自己设置格式,例如 return string.Format("{0}hr {1}mn {2}sec", (int) span.TotalHours, span.Minutes,

How do I convert a TimeSpan to a formatted string?

This question already has an answer here: How can I String.Format a TimeSpan object with a custom format in .NET? 18 answers Timespan formatting [duplicate] 5 answers Would TimeSpan.ToString() do the trick for you? If not, it looks like the code sample on that page describes how to do custom formatting of a TimeSpan object. I just built a few TimeSpan Extension methods. Thought I could

如何将TimeSpan转换为格式化的字符串?

这个问题在这里已经有了答案: 如何在.NET中使用自定义格式String.Format TimeSpan对象? 18个答案 Timespan格式化[重复] 5个答案 TimeSpan.ToString()会为你做诡计吗? 如果不是,它看起来像该页面上的代码示例描述了如何执行TimeSpan对象的自定义格式。 我只是建立了几个TimeSpan扩展方法。 以为我可以分享: public static string ToReadableAgeString(this TimeSpan span) { return string.Format("{0:0}",

Timespan formatting

This question already has an answer here: How can I String.Format a TimeSpan object with a custom format in .NET? 18 answers 没有内置功能,您需要使用自定义方法,如下所示: TimeSpan ts = new TimeSpan(0, 70, 0); String.Format("{0} hour{1} {2} minute{3}", ts.Hours, ts.Hours == 1 ? "" : "s", ts.Minutes, ts.Minutes == 1 ? "" : "s") public

Timespan格式

这个问题在这里已经有了答案: 如何在.NET中使用自定义格式String.Format TimeSpan对象? 18个答案 没有内置功能,您需要使用自定义方法,如下所示: TimeSpan ts = new TimeSpan(0, 70, 0); String.Format("{0} hour{1} {2} minute{3}", ts.Hours, ts.Hours == 1 ? "" : "s", ts.Minutes, ts.Minutes == 1 ? "" : "s") public static string Pluralize(int n, s

How to serialize a TimeSpan to XML

I am trying to serialize a .NET TimeSpan object to XML and it is not working. A quick google has suggested that while TimeSpan is serializable, the XmlCustomFormatter does not provide methods to convert TimeSpan objects to and from XML. One suggested approach was to ignore the TimeSpan for serialization, and instead serialize the result of TimeSpan.Ticks (and use new TimeSpan(ticks) for deseri

如何将TimeSpan序列化为XML

我在尝试将.NET TimeSpan对象序列化为XML,但它不起作用。 快速谷歌已经建议,尽管TimeSpan是可序列化的,但XmlCustomFormatter不提供将TimeSpan对象与XML进行转换的方法。 一种建议的方法是忽略用于序列化的TimeSpan ,而是序列化TimeSpan.Ticks的结果(并使用new TimeSpan(ticks)进行反序列化)。 下面是一个例子: [Serializable] public class MyClass { // Local Variable private TimeSpan m_TimeSinceLastEve

How do you convert epoch time in C#?

How do you convert Unix epoch time into real time in C#? (Epoch beginning 1/1/1970) 我认为你的意思是Unix时间,它定义为自1970年1月1日午夜(UTC)以来的秒数。 public static DateTime FromUnixTime(long unixTime) { return epoch.AddSeconds(unixTime); } private static readonly DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); With all credit to LukeH, I've put together some

你如何在C#中转换时代?

你如何将Unix纪元时间转换为C#中的实时? (从1970年1月1日开始) 我认为你的意思是Unix时间,它定义为自1970年1月1日午夜(UTC)以来的秒数。 public static DateTime FromUnixTime(long unixTime) { return epoch.AddSeconds(unixTime); } private static readonly DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); 凭借对LukeH的所有荣誉,我已经将一些扩展方法放在一起以便于使用: public s

How can I find the method that called the current method?

When logging in C#, how can I learn the name of the method that called the current method? I know all about System.Reflection.MethodBase.GetCurrentMethod() , but I want to go one step beneath this in the stack trace. I've considered parsing the stack trace, but I am hoping to find a cleaner more explicit way, something like Assembly.GetCallingAssembly() but for methods. Try this: using S

我如何找到调用当前方法的方法?

在使用C#登录时,如何才能知道调用当前方法的方法的名称? 我知道所有关于System.Reflection.MethodBase.GetCurrentMethod() ,但我想在堆栈跟踪中的这一步之下进行一步。 我已经考虑解析堆栈跟踪,但我希望找到一个更清晰的方法,比如Assembly.GetCallingAssembly()但是对于方法。 尝试这个: using System.Diagnostics; // Get call stack StackTrace stackTrace = new StackTrace(); // Get calling method name Consol

Order of items in classes: Fields, Properties, Constructors, Methods

Is there an official C# guideline for the order of items in terms of class structure? Does it go: Public Fields Private Fields Properties Constructors Methods ? I'm curious if there is a hard and fast rule about the order of items? I'm kind of all over the place. I want to stick with a particular standard so I can do it everywhere. The real problem is my more complex p

类中项目的顺序:字段,属性,构造函数,方法

在类结构方面,是否有正式的C#指导项目顺序? 它会去: 公共领域 私人领域 属性 构造函数 方法 ? 我很好奇,如果有关于物品顺序的硬性规定? 我有点太过分了。 我想坚持一个特定的标准,所以我可以在任何地方做到这一点。 真正的问题是我的更复杂的属性最终看起来很像方法,并且在构造函数之前他们感觉不在顶部。 任何提示/建议? 根据StyleCop规则文档的顺序如下。 在类,结构或接口内:(SA1201和

Mapping Complex Types to results form SqlQuery in Entity Framework

I am calling a stored proc using SqlQuery, the result is supposed to be a graph of objects, ie public class Person { public int Id{get;set;} public string FullName {get;set;} public Address HomeAddress {get;set;} public Vehicle PrivateVehicle {get;set;} } But SqlQuery wont map the Address and Vehicle. It only maps the column names to the properties. Is there a work around this?

在Entity Framework中将复杂类型映射到结果形式SqlQuery

我使用SqlQuery调用存储过程,结果应该是对象的图形,即 public class Person { public int Id{get;set;} public string FullName {get;set;} public Address HomeAddress {get;set;} public Vehicle PrivateVehicle {get;set;} } 但SqlQuery不会映射地址和车辆。 它只将列名映射到属性。 有没有解决这个问题的方法? 我还能怎样做映射? 我找到了这个: ..你不能做的是返回对象的图形,例如包含复杂类

urlencoded binding to different property names

I am expecting a POST request with content type set to: Content-Type: application/x-www-form-urlencoded Request body looks like this: first_name=john&last_name=banana My action on controller has this signature: [HttpPost] public HttpResponseMessage Save(Actor actor) { .... } Where Actor class is given as: public class Actor { public string FirstName {get;set;} public string Las

urlencoded绑定到不同的属性名称

我期待内容类型设置为的POST请求: 内容类型:application / x-www-form-urlencoded 请求正文如下所示: 如first_name =约翰&姓氏=香蕉 我对控制器的行为有这样的签名: [HttpPost] public HttpResponseMessage Save(Actor actor) { .... } Actor类的给定为: public class Actor { public string FirstName {get;set;} public string LastName {get;set;} } 有没有办法强制Web API绑定: first_name =>名

Is it possible to draw an image in the console using C#?

This question already has an answer here: Display a Image in a console application 7 answers No. It is not possible to draw to the console window. It supports text only. The closest you will get is ASCII art! I would recommend WPF app for graphics Actually, the answer is "Yes it is, sort of". Take a look at Drawing in a Win32 Console on C++? for native code for drawing on top

是否有可能使用C#在控制台中绘制图像?

这个问题在这里已经有了答案: 在控制台应用程序中显示图像7个答案 不可以。不能绘制到控制台窗口。 它仅支持文本。 你会得到最接近的ASCII艺术! 我会推荐WPF应用程序的图形 其实答案是“是的,有点”。 看看在C ++的Win32控制台中绘图? 用于在控制台窗口顶部绘制的本机代码。 要用C#做到这一点,你很可能需要调用本地代码来完成实际的工作。 而且你不会真的在控制台窗口本身中画图,只能绘制在它上面的一个窗口