我正在尝试了解如何在阅读我的其他问题的答案后使用类型转换器.但我不确定我是否完全明白了......
在我的特定情况下,我想通过根据枚举成员获取资源字符串来将枚举成员"转换"为本地化字符串.所以,例如,如果我有这个枚举:
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应用程序中使用它,但也欢迎更一般的例子.
在阅读这个问题时,我开始怀疑.说你有这两个:
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)
几乎可以工作,但听起来有点奇怪...你会怎么称呼它?
是否可以使用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",)
SorteDictionary是根据MSDN按键排序的.这是否意味着当您在foreach中枚举它时,您可以确定它将被排序?或者它只是意味着SortedDictionary在内部以这种方式工作以在各种情况下具有更好的性能?
由于缺少程序集,项目将无法编译.导致错误的文件有
using Microsoft.VisualStudio.DebuggerVisualizers;
Run Code Online (Sandbox Code Playgroud)
该VisualStudio
部分被标记为红色.我需要安装什么来修复它?对我而言,这听起来像是Visual Studio带来的东西,但这就是我正在使用的东西,所以它被安装......
澄清:我知道它存在于哪个程序集中,并且引用会在之前添加到项目引用中.但我怎么得到它?我需要安装什么SDK?或者我忘记在安装Visual Studio时检查一些东西?
你在哪里取消订阅UserControl
?我在Load
活动中订阅了它,就像我在表格中所做的那样.在表格中,我通常会在Closing
活动中取消订阅,但我在UserControl
...中找不到类似的东西.
我想知道是否有人在这里有一个很好的实施他们想要分享的阿特金筛选.
我正在尝试实现它,但不能完全包围它.这是我到目前为止所拥有的.
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) System.Math中的所有方法都double
作为参数并返回参数.常量也是类型double
.我检查了MathNet.Numerics,在那里似乎也是如此.
为什么是这样?特别是对于常数.不decimal
应该更准确吗?在进行计算时,这通常不会有用吗?
为了测试某些东西抛出,例如ArgumentException
我可以这样做:
Assert.Throws<ArgumentException>(() => dog.BarkAt(deafDog));
Run Code Online (Sandbox Code Playgroud)
如何ParamName
以清晰的方式检查是否正确?还有奖金问题:或许您可能会建议不要对此进行测试?
我可以通过这样做获取文件夹中所有文件的名称:
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) c# ×8
dictionary ×2
.net ×1
algorithm ×1
applescript ×1
caching ×1
decimal ×1
double ×1
enumeration ×1
enums ×1
events ×1
exception ×1
filenames ×1
finder ×1
lambda ×1
localization ×1
math ×1
nunit ×1
primes ×1
properties ×1
sorting ×1
winforms ×1