如何NodaTime.LocalDateTime
使用ServiceStack.Text 控制自定义类型(例如)的序列化/反序列化?
Json.NET Converters
为此提供了这一功能,因此每次类包含自定义类型时,都将使用相应的自定义序列化器/反序列化器.例如,可以使用自定义方法将字符串NodaTime.LocalDateTime的属性转换为字符串.
在db4o中,IObjectConstructor
将作为类似的转换层在保持类型之前将类型转换为另一个类型(LocalDateTime可以转换为DateTime).
在下面的代码中,我试图访问Debugger
一个有效DTE2
对象的属性(通过类似于此方法的方法获得),以检查它是否正在调试当前进程(可能会打开多个Visual Studio实例)。
它工作正常,但当一个Visual Studio(2015)实例显示一个模式对话框时(例如,询问是否应由于后台代码更改而重新加载当前加载的项目的源代码),访问dte.Debugger
将一直挂起,直到出现对话框由用户处理。尝试访问该dte
对象的其他成员也将挂起。
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Threading;
using EnvDTE;
using EnvDTE80;
using EnvDTE90a;
public static class VSAutomation {
public static int? GetCurrentDebuggerPid() {
var taskCompletion = new TaskCompletionSource<int?>();
var staThread = new System.Threading.Thread(() => {
try {
int? debuggerPid = null;
using (new MessageFilter()) {
using (var currentProcess = System.Diagnostics.Process.GetCurrentProcess())
using (var vsInstances = System.Diagnostics.Process.GetProcessesByName("devenv").AsDisposable()) {
foreach (var p in vsInstances.Enumerable) {
DTE2 dte;
if (TryGetVSInstance(p.Id, out …
Run Code Online (Sandbox Code Playgroud) 指定变量应该是函数(相当于委托Func<T>
还是Action
C#)的适当类型提示是什么?
是否也可以以通用方式指定函数参数类型(例如Func<int, int>
)?
我在文档中找不到任何相关细节.
以下代码目前输出:
12.1
12.100
12.1000
12.00
12
12.0000
Run Code Online (Sandbox Code Playgroud)
如何更改它以便输出:
12.1
12.1
12.1
12
12
12
Run Code Online (Sandbox Code Playgroud)
Math.Round似乎是事情,但它让我定义了我想要的小数位数,但我希望它们如上所述变量.
如果没有数学方法可以做到这一点,我只会从字符串右侧删除零和小数点,但会认为有一种数学方法来处理它.
using System;
using System.Collections.Generic;
namespace Test8834234
{
public class Program
{
static void Main(string[] args)
{
List<string> decimalsAsStrings = new List<string>
{
"12.1",
"12.100",
"12.1000",
"12.00",
"12",
"12.0000"
};
foreach (var decimalAsString in decimalsAsStrings)
{
decimal dec = decimal.Parse(decimalAsString);
Console.WriteLine(dec);
}
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud) 任何人都知道为什么以下不会编译?ID的setter应该是两个类的私有,那么为什么我们可以实例化ClassA而不是ClassB?
public class ClassA {
public string ID { get; private set; }
public void test() {
var instanceA = new ClassA() { ID = "42" };
var instanceB = new ClassB() { ID = "43" };
}
public class ClassB {
public string ID { get; private set; }
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
i + = a应该等于i = i + a.在a == 1的情况下,这被认为效率低于++ i,因为它涉及更多的内存访问; 或者编译器会使它与++ i完全相同吗?
我正在寻找一种与泛型字典的Keys属性(类型为KeyCollection)一样高效的方法.
使用Linq select语句可以工作,但每次请求密钥时它都会迭代整个集合,而我相信密钥可能已经在内部存储.
目前我的GenericKeyedCollection类看起来像这样:
public class GenericKeyedCollection<TKey, TItem> : KeyedCollection<TKey, TItem> {
private Func<TItem, TKey> getKeyFunc;
protected override TKey GetKeyForItem(TItem item) {
return getKeyFunc(item);
}
public GenericKeyedCollection(Func<TItem, TKey> getKeyFunc) {
this.getKeyFunc = getKeyFunc;
}
public List<TKey> Keys {
get {
return this.Select(i => this.GetKeyForItem(i)).ToList();
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新:感谢您的回答,我将使用以下属性而不是使用Linq进行迭代.
public ICollection<TKey> Keys {
get {
if (this.Dictionary != null) {
return this.Dictionary.Keys;
}
else {
return new Collection<TKey>(this.Select(this.GetKeyForItem).ToArray());
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想用一个RichTextBox WPF控件打印日志输出,但我想知道什么是有经过例如显示超过10000行,如果能够真正实现以删除"最老的"行最有效的方法队列行为,尤其是因为没有简单的"Text"属性可供使用.
不幸的是,我不能够达到这个结果与n日志或者可能是由于错误或限制.
我需要确定MySQL中<=>运算符的 SQLite的等价物.
任何的想法?
考虑以下基类中与事件相关的基本代码:
public event EventHandler Updated;
public void OnUpdated() {
if (Updated != null) Updated(sender: this, e: null)
}
Run Code Online (Sandbox Code Playgroud)
如果没有人订阅了更新的事件,我不希望第四行导致任何重大的性能拖累(想法是让订阅者选择要订阅的最细粒度的事件,只发起最少数量的事件并防止消息队列超载).
我应该关心并跟踪订户的存在(例如,使用if (Updated != null && OnUpdateSubscribed) Updated(sender: this, e: null)
或信任编译器/运行时?
c# ×8
.net ×1
database ×1
debugging ×1
decimal ×1
dictionary ×1
envdte ×1
events ×1
hang ×1
hashtable ×1
increment ×1
logging ×1
mysql ×1
nodatime ×1
null ×1
performance ×1
python ×1
python-3.x ×1
queue ×1
richtextbox ×1
rounding ×1
servicestack ×1
sqlite ×1
type-hinting ×1
wpf ×1