假设您正在编写一些与 BenchmarkDotNet 一起使用的基准测试,这些基准测试多目标为net48和net6.0,并且这些基准测试之一只能针对该net6.0目标进行编译。
显而易见的事情是使用类似的方法从构建中排除该特定基准net48:
#if NET6_0_OR_GREATER
[Benchmark]
public void UsingSpan()
{
using var stream = new MemoryStream();
writeUsingSpan(stream, _array);
}
static void writeUsingSpan(Stream output, double[] array)
{
var span = array.AsSpan();
var bytes = MemoryMarshal.AsBytes(span);
output.Write(bytes);
}
#endif // NET6_0_OR_GREATER
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用,并且它不起作用的方式取决于TargetFrameworks项目文件中的属性中指定的目标的顺序。
如果您订购框架,那么net6.0首先<TargetFrameworks>net6.0;net48</TargetFrameworks>(在上面的示例中)该UsingSpan()方法将包含在两个目标中,导致目标net48和输出出现 BenchmarkDotNet 构建错误,如下所示:
| Method | Job | Runtime | Mean | Error | StdDev |
|------------------ |------------------- |------------------- |-----------:|----------:|----------:|
| …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码.我开始执行一个什么都不做的任务,然后使用ContinueWith()开始对一个递增计数器的方法进行10次调用.
当我运行该程序时,它打印"0",表示还没有调用increment()方法.我期待它被调用10次,因为那是我调用ContinueWith()的次数.
如果我取消注释"Thread.Sleep(20)"行,则按预期打印"10".
这在发布或调试模式下发生.我的系统是运行Windows 7 x64的超级线程(8个逻辑核心)的核心2 quad.
我假设我对Task.ContinueWith()如何工作有一些基本的误解....
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Program
{
static void Main()
{
using (var task = Task.Factory.StartNew(()=>{}))
{
for (int i = 0; i < 10; ++i)
{
task.ContinueWith(_=> increment());
// Thread.Sleep(20); // Uncomment to print 10 instead of 0.
}
task.Wait();
}
// This prints 0 UNLESS you uncomment the sleep above.
Console.WriteLine(counter);
}
static void increment()
{
Interlocked.Increment(ref counter);
}
private static int counter;
}
} …Run Code Online (Sandbox Code Playgroud) 我必须在这里重新发明轮子 - 但我已经搜索过了,我找不到任何相同的东西......
这是我的代码,用于创建具有默认构造函数的零个或多个对象的序列:
public static IEnumerable<T> CreateSequence<T>(int n) where T: new()
{
for (int i = 0; i < n; ++i)
{
yield return new T();
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题很简单:我应该使用Linq等效的吗?
当我使用WiX 3.x和Visual Studio 2010创建名为"MyCustomActions"的Windows Installer XML"C#Custom Action Project"时,它会生成以下文件:
"CustomAction.config","CustomAction.cs"和项目文件"MyCustomActions.csproj".
我们使用包含公司名称的程序集命名方案,因此我们将程序集名称更改为:CompanyName.ProductName.MyCustomActions
同时,我们将CustomActions类的默认命名空间和实际命名空间更改为CompanyName.ProductName.MyCustomActions,以匹配程序集名称.
可以在不更改任何其他文件或类名的情况下执行此操作吗?
我想知道的是使用"CustomAction.config"文件 - 它的名称是否必须以某种方式与输出程序集名称相关?我不确定如何使用"CustomAction.config".
另外:"CustomAction.cs"文件包含一个名为"CustomActions"的类(注意复数),我们的命名约定说类的代码文件应该与类本身的名称相同,所以我们想重命名代码文件为"CustomActions.cs".我很确定没问题......是吗?
最后,如果我们将CustomAction类重命名为其他类,那会没关系吗?我再次关注它与"CustomAction.config"的关系.
我想所有这些问题都可以归为一类:"CustomAction.config"文件的名称与命名空间,程序集和类名的其余部分之间的关系是什么?
如果我有答案,它将回答我之前的所有问题.
当我在Visual Studio 2012中编辑C#代码时,"错误列表"窗口会不断更新当前语法错误列表等.我发现这非常分散注意力,并希望将其关闭.
有谁知道怎么关掉它?
(我真的不知道这个VS功能被正确调用了什么,所以有人可能已经回答了它......如果是这样,请道歉.)
我的一个类有一个Guid类型的属性.此属性可以由多个线程同时读取和写入.我的印象是对Guid的读写不是原子的,因此我应该锁定它们.
我选择这样做:
public Guid TestKey
{
get
{
lock (_testKeyLock)
{
return _testKey;
}
}
set
{
lock (_testKeyLock)
{
_testKey = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
(在我的课程中,对Guid的所有访问也是通过该属性完成的,而不是直接访问_testKey.)
我有两个问题:
(1)是否真的有必要像这样锁定Guid以防止撕裂读数?(我很确定它是.)
(2)这是一种合理的锁定方式吗?或者我需要像下面这样做:
get
{
Guid result;
lock (_testKeyLock)
{
result = _testKey;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
[编辑]本文确认Guids将遭受撕裂的读取:http://msdn.microsoft.com/en-us/magazine/jj863136.aspx
在下面的代码中,Resharper给了我一个警告:Cannot cast expression of type 'Color' to type 'UIntPtr'.(实际上,Resharper认为这是一个实际错误.)
但是,没有编译器警告,它工作正常.
这对我来说看起来像一个Resharper bug.是吗?或者编译器不担心它有什么不好吗?(我正在使用Resharper 7.1.1)
using System;
namespace Demo
{
internal class Program
{
public enum Color { Red, Green, Blue }
private static void Main(string[] args)
{
UIntPtr test = (UIntPtr) Color.Red; // Resharper warning, no compile warning.
}
}
}
Run Code Online (Sandbox Code Playgroud)
我可以通过首先将值转换为int来使警告消失,所以我有一个解决方法:
UIntPtr test = (UIntPtr)(int) Color.Red;
Run Code Online (Sandbox Code Playgroud) 似乎TypeConverter.IsValid()使用当前的线程文化,但TypeConverter.ConvertFrom()没有。
这使得TypeConverter.IsValid()与DateTime类型一起使用非常无用,除非您处于不变文化中。确实,这似乎是一个错误。
有谁知道如何TypeConverter.IsValid()利用当前的文化?
下面的代码演示了这个问题。
它使用两个字符串,一个是 DD/MM/YYYY 格式,一个是 MM/DD/YYYY 格式。
测试的第一部分是在不变文化中完成的。它表明 TypeConverter.IsValid()对于 MM/DD/YYYY 字符串返回 true 并且您可以使用TypeConverter.ConvertFrom()该字符串将该字符串转换为DateTime.
第一部分还演示了TypeConverter.IsValid()为 DD/MM/YYYY 字符串返回 false。
对于第二部分,我更改了使用“DD/MM/YYYY”日期的当前文化“en-GB”。
我现在希望IsValid()结果被反转,但它返回的两个字符串与以前相同。所以它说 MM/DD/YYYY 字符串是有效的。
但是 - 这是重要的一点 - 如果您尝试使用TypeConverter.ConvertFrom()实际转换表示没问题的字符串TypeConverter.IsValid(),您将收到异常。
有没有办法解决这个问题?
using System;
using System.ComponentModel;
using System.Globalization;
using System.Threading;
namespace Demo
{
class Program
{
void Run()
{
// Start off with the US culture, which has MM/DD/YYYY date …Run Code Online (Sandbox Code Playgroud) Resharper 8似乎只有一个字段和属性的颜色设置,称为Resharper Field Identifier(在Fonts and ColorsVisual Studio的环境设置部分中找到).
因此,我为此选择的颜色将用于公共属性和私有字段.
有谁知道这些项目有单独的颜色的方法?
考虑以下代码:
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Demo
{
static class Program
{
static void Main()
{
var tasks = new Task[1];
tasks[0] = Task.Run(() => throwExceptionAfterOneSecond())
.ContinueWith(task => {
Console.WriteLine("ContinueWith()"); }, TaskContinuationOptions.NotOnFaulted);
try
{
Task.WaitAll(tasks);
}
catch (AggregateException ex)
{
Console.WriteLine("Exception received: " + ex.InnerExceptions.Single().Message);
}
}
static void throwExceptionAfterOneSecond()
{
Thread.Sleep(1000);
throw new InvalidOperationException("TEST");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这会产生以下输出:
Exception received: A task was canceled.
我的问题很简单:我如何获得原件InvalidOperationException("TEST");而不是System.Threading.Tasks.TaskCanceledException?
请注意,如果您删除该.ContinueWith()部分,这将按我的预期工作,并且在这种情况下的输出为Exception received: TEST …