小编Erw*_*yer的帖子

什么是JSON.NET转换器的ServiceStack.Text等价物,例如当应用于NodaTime类型时?

如何NodaTime.LocalDateTime使用ServiceStack.Text 控制自定义类型(例如)的序列化/反序列化?

Json.NET Converters 为此提供了这一功能,因此每次类包含自定义类型时,都将使用相应的自定义序列化器/反序列化器.例如,可以使用自定义方法将字符串NodaTime.LocalDateTime的属性转换为字符串.

在db4o中,IObjectConstructor将作为类似的转换层在保持类型之前将类型转换为另一个类型(LocalDateTime可以转换为DateTime).

c# serialization servicestack nodatime servicestack-text

5
推荐指数
1
解决办法
308
查看次数

在IDE中打开模式对话框时,访问Visual Studio DTE成员将挂起。

在下面的代码中,我试图访问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)

c# debugging hang visual-studio envdte

5
推荐指数
0
解决办法
207
查看次数

在Python 3.5中,如何将函数指定为类型提示?

指定变量应该是函数(相当于委托Func<T>还是ActionC#)的适当类型提示是什么?

是否也可以以通用方式指定函数参数类型(例如Func<int, int>)?

我在文档中找不到任何相关细节.

python static-typing type-hinting python-3.x

5
推荐指数
1
解决办法
391
查看次数

如何从十进制字符串中删除零和小数点?

以下代码目前输出:

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)

c# decimal rounding

4
推荐指数
1
解决办法
9380
查看次数

在实例化时使用{}大括号设置属性

任何人都知道为什么以下不会编译?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)

谢谢

c# instantiation

4
推荐指数
1
解决办法
4604
查看次数

在C#中,++ i与i + = 1有任何性能差异吗?

i + = a应该等于i = i + a.在a == 1的情况下,这被认为效率低于++ i,因为它涉及更多的内存访问; 或者编译器会使它与++ i完全相同吗?

c# performance increment

4
推荐指数
1
解决办法
3407
查看次数

从KeyedCollection获取密钥列表的最有效方法是什么?

我正在寻找一种与泛型字典的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)

c# dictionary hashtable keyedcollection data-structures

3
推荐指数
1
解决办法
1504
查看次数

WPF RichTextBox作为用于记录目的的文本队列

我想用一个RichTextBox WPF控件打印日志输出,但我想知道什么是有经过例如显示超过10000行,如果能够真正实现以删除"最老的"行最有效的方法队列行为,尤其是因为没有简单的"Text"属性可供使用.

不幸的是,我不能够达到这个结果与n日志或者可能是由于错误或限制.

c# queue wpf logging richtextbox

3
推荐指数
1
解决办法
4011
查看次数

3
推荐指数
1
解决办法
698
查看次数

没有听众的事件会对性能产生相当大的影响吗?

考虑以下基类中与事件相关的基本代码:

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)或信任编译器/运行时?

.net c# events

3
推荐指数
1
解决办法
121
查看次数