小编Svi*_*ish的帖子

C#:如何使用Type Converter来本地化枚举

我正在尝试了解如何在阅读我的其他问题的答案后使用类型转换器.但我不确定我是否完全明白了......

在我的特定情况下,我想通过根据枚举成员获取资源字符串来将枚举成员"转换"为本地化字符串.所以,例如,如果我有这个枚举:

public enum Severity
{
    Critical,
    High,
    Medium,
    Low
}
Run Code Online (Sandbox Code Playgroud)

或这个:

public enum Color
{
    Black = 0x0,
    Red = 0x1,
    Green = 0x2,
    Blue = 0x4,
    Cyan = Green | Blue,
    Magenta = Red | Blue,
    Yellow = Red | Green,
    White = Red | Green | Blue,
}
Run Code Online (Sandbox Code Playgroud)

如何创建可以将这些成员转换为本地化字符串的类型转换器?我将如何使用它?目前我需要在WinForms应用程序中使用它,但也欢迎更一般的例子.

c# enums localization typeconverter

7
推荐指数
1
解决办法
1万
查看次数

C#:你会为IEnumerable类命名什么?

在阅读这个问题时,我开始怀疑.说你有这两个:

class ProductCollection : ICollection<Product>
class ProductList : IList<Product>
Run Code Online (Sandbox Code Playgroud)

你会称之为什么IEnumerable<Product>

class Product--- : IEnumerable<Product>
Run Code Online (Sandbox Code Playgroud)

在我读到其他问题之前,我可能已经将其称为ProductCollection实际,但考虑到新信息,这可能有点误导,因为它没有实现ICollection<Product>.你能打电话Products吗?

var products = new Products(); // products is/are products
Run Code Online (Sandbox Code Playgroud)

几乎可以工作,但听起来有点奇怪...你会怎么称呼它?

.net naming-conventions

7
推荐指数
3
解决办法
1921
查看次数

C#:是否可以将表达式或函数用作字典中的键?

是否可以使用Expression<Func<T>>Func<T>作为字典中的键?例如,缓存重度计算的结果.

例如,从我的一个不同的问题改变我的基本缓存:

public static class Cache<T>
{
    // Alternatively using Expression<Func<T>> instead
    private static Dictionary<Func<T>, T> cache;
    static Cache()
    {
        cache = new Dictionary<Func<T>, T>();
    }
    public static T GetResult(Func<T> f)
    {
        if (cache.ContainsKey(f))
            return cache[f];

       return cache[f] = f();
    }
}
Run Code Online (Sandbox Code Playgroud)

这甚至会起作用吗?

编辑:经过快速测试后,它似乎确实有效.但我发现它可能更通用,因为它现在每个返回类型一个缓存...不知道如何更改它以便不会发生...嗯

编辑2: Noo,等等......实际上并没有.嗯,对于常规方法,它确实如此.但不适用于lambdas.即使它们看起来相同,它们也会获得各种随机方法名称.哦,好吧c",)

c# lambda caching dictionary

7
推荐指数
1
解决办法
1494
查看次数

C#:当你枚举它时,SortedDictionary是否排序?

SorteDictionary是根据MSDN按键排序的.这是否意味着当您在foreach中枚举它时,您可以确定它将被排序?或者它只是意味着SortedDictionary在内部以这种方式工作以在各种情况下具有更好的性能?

c# sorting dictionary enumeration

7
推荐指数
2
解决办法
3479
查看次数

我在哪里可以找到Microsoft.VisualStudio.DebuggerVisualizers?

由于缺少程序集,项目将无法编译.导致错误的文件有

using Microsoft.VisualStudio.DebuggerVisualizers;
Run Code Online (Sandbox Code Playgroud)

VisualStudio部分被标记为红色.我需要安装什么来修复它?对我而言,这听起来像是Visual Studio带来的东西,但这就是我正在使用的东西,所以它被安装......

澄清:我知道它存在于哪个程序集中,并且引用会在之前添加到项目引用中.但我怎么得到它?我需要安装什么SDK?或者我忘记在安装Visual Studio时检查一些东西?

c# assembly-resolution visual-studio

7
推荐指数
2
解决办法
6774
查看次数

在WinForms UserControl中取消订阅事件的位置

你在哪里取消订阅UserControl?我在Load活动中订阅了它,就像我在表格中所做的那样.在表格中,我通常会在Closing活动中取消订阅,但我在UserControl...中找不到类似的东西.

c# events user-controls winforms

7
推荐指数
2
解决办法
3290
查看次数

C#:Atkin筛选的实施

我想知道是否有人在这里有一个很好的实施他们想要分享的阿特金筛选.

我正在尝试实现它,但不能完全包围它.这是我到目前为止所拥有的.

public class Atkin : IEnumerable<ulong>
{
    private readonly List<ulong> primes;
    private readonly ulong limit;

    public Atkin(ulong limit)
    {
        this.limit = limit;
        primes = new List<ulong>();
    }

    private void FindPrimes()
    {
        var isPrime = new bool[limit + 1];
        var sqrt = Math.Sqrt(limit);

        for (ulong x = 1; x <= sqrt; x++)
            for (ulong y = 1; y <= sqrt; y++)
            {
                var n = 4*x*x + y*y;
                if (n <= limit && (n % 12 == 1 || n …
Run Code Online (Sandbox Code Playgroud)

c# algorithm primes sieve-of-atkin

7
推荐指数
2
解决办法
4895
查看次数

为什么System.Math和例如MathNet.Numerics基于double?

System.Math中的所有方法都double作为参数并返回参数.常量也是类型double.我检查了MathNet.Numerics,在那里似乎也是如此.

为什么是这样?特别是对于常数.不decimal应该更准确吗?在进行计算时,这通常不会有用吗?

c# math double decimal

7
推荐指数
2
解决办法
2060
查看次数

C#,NUnit:测试ArgumentException具有正确的ParamName的明确方法

为了测试某些东西抛出,例如ArgumentException我可以这样做:

Assert.Throws<ArgumentException>(() => dog.BarkAt(deafDog));
Run Code Online (Sandbox Code Playgroud)

如何ParamName以清晰的方式检查是否正确?还有奖金问题:或许您可能会建议不要对此进行测试?

c# nunit properties exception

7
推荐指数
1
解决办法
809
查看次数

Applescript:在没有扩展名的文件夹中获取文件名

我可以通过这样做获取文件夹中所有文件的名称:

tell application "Finder"
    set myFiles to name of every file of somePath
end tell
Run Code Online (Sandbox Code Playgroud)

如何更改字符串以myFiles使它们不包含文件扩展名?

我可以举个例子{"foo.mov", "bar.mov"},但是想拥有 {"foo", "bar"}.


当前解决方案

根据接受的答案,我想出了下面的代码.让我知道它是否可以以某种方式变得更清洁或更有效.

-- Gets a list of filenames from the
on filenames from _folder

    -- Get filenames and extensions
    tell application "Finder"
        set _filenames to name of every file of _folder
        set _extensions to name extension of every file of _folder
    end tell

    -- Collect names (filename - dot and extension)
    set _names to {} …
Run Code Online (Sandbox Code Playgroud)

filenames applescript finder

7
推荐指数
4
解决办法
2万
查看次数