我们使用TFS 2013作为构建服务器.我已经开始了一个C#6.0项目,我正在努力建立它.我正在使用新的null条件运算符和我的构建chokes.我已经尝试在TFS服务器上安装了几个东西,包括目标包和VS 2015.我已经尝试将/tv:14.0提供给MSBuild参数.
Configuration\EntityEntityConfig.cs(270):无效的表达式术语'.'
Configuration\EntityEntityConfig.cs(283):无效的表达式术语'.'
Configuration\EntityEntityConfig.cs(283):语法错误,':'预期
......等
在这一点上,我不知道还有什么可以尝试.任何建议将不胜感激.
我正在尝试将我的Angular ui-Datepicker中的日期保存到我的SQL数据库中.日期格式为(10-27-2015 12:00 AM),但不会保存.我尝试使用以下内容将其转换为SQL DateTime格式:
DateTime? myDate = form.dteStartDate;
string sqlFormattedDate = myDate.ToString("yyyy-MM-dd HH:mm:ss");
Run Code Online (Sandbox Code Playgroud)
但是我收到错误"方法'ToString'没有重载需要1个参数.SQL中的字段是'datetime'类型.
非常感谢任何帮助.
插值字符串是C#6.0的新功能之一.
根据MSDN,嵌入式C#表达式的语法可以包含一个可选的逗号分隔值,如文档<optional-comma-field-width>中所述.
不幸的是,我没有找到这个字段的用途.
从名称可以看出,这个值设置了"插值"字段的最大大小,但是当我尝试以下表达式时:
var p = Process.GetCurrentProcess();
Console.WriteLine($"Process name is {p.ProcessName, 5}");
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
Process name is LINQPad.UserQuery
Run Code Online (Sandbox Code Playgroud) 我有一个HashSet,我试图将其转换为IReadOnlyCollection,但我收到错误:
无法将类型'System.Collections.Generic.HashSet'隐式转换为'System.Collections.Generic.IReadOnlyCollection'.存在显式转换(您是否错过了演员?)
Hashset是一个
public class HashSet<T> : ICollection<T>, ISerializable, IDeserializationCallback, ISet<T>, IReadOnlyCollection<T>
Run Code Online (Sandbox Code Playgroud)
我可以使用显式转换,但我不知道为什么我不能将它用作IReadOnlyCollection.
HashSet<DateTime> set = new HashSet<DateTime> { DateTime.Today };
ICollection<DateTime> collection = set; // OK
ISerializable serializable = set; // OK
IDeserializationCallback deserializationCallback = set; // OK
ISet<DateTime> iSet = set; // OK
IReadOnlyCollection<DateTime> castReadOnlyCollection = (IReadOnlyCollection<DateTime>)set; // OK
IReadOnlyCollection<DateTime> readOnlyCollection = set; // Error
Run Code Online (Sandbox Code Playgroud)
如果没有明确的演员,我为什么不能使用它呢?
我正在使用.NET framework 4.5
我需要读取JSON配置文件,修改值,然后再次将修改后的JSON保存回文件.JSON就像它获得的一样简单:
{
"test": "init",
"revision": 0
}
Run Code Online (Sandbox Code Playgroud)
要加载数据并修改值,我这样做:
var config = JObject.Parse(File.ReadAllText("config.json"));
config["revision"] = 1;
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好; 现在,将JSON写回文件.首先我尝试了这个:
File.WriteAllText("config.json", config.ToString(Formatting.Indented));
Run Code Online (Sandbox Code Playgroud)
哪个写文件正确,但缩进只有两个空格.
{
"test": "init",
"revision": 1
}
Run Code Online (Sandbox Code Playgroud)
从文档中看,似乎没有办法以这种方式传递任何其他选项,所以我尝试修改这个例子,这将允许我直接设置Indentation和IndentChar属性JsonTextWriter来指定缩进量:
using (FileStream fs = File.Open("config.json", FileMode.OpenOrCreate))
{
using (StreamWriter sw = new StreamWriter(fs))
{
using (JsonTextWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;
jw.IndentChar = ' ';
jw.Indentation = 4;
jw.WriteRaw(config.ToString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这似乎没有任何影响:文件仍然写有两个空格缩进.我究竟做错了什么?
我有一个这个简单的程序,可以在一个可以为空的long上切换:
class Program
{
static void Main(string[] args)
{ Console.WriteLine(Test(1)); }
private static string Test(long? orderId)
{
switch (orderId)
{
case 1:
return "1";
default:
return "default";
}
}
}
Run Code Online (Sandbox Code Playgroud)
在VS 2013中编译之后,我总是得到"1"作为输出.在VS 2015中编译后,我总是将"默认"作为输出.
只有当Test()的参数是long(不是int)时才会发生.
我已经反编译然后il代码和(在我看来)在通过"beq.s"比较值之前缺少"conv.i8"(VS 2013编译器发出它):
.method private hidebysig static string
Test(valuetype [mscorlib]System.Nullable`1<int64> orderId) cil managed
{
// Code size 46 (0x2e)
.maxstack 2
.locals init ([0] valuetype [mscorlib]System.Nullable`1<int64> V_0,
[1] valuetype [mscorlib]System.Nullable`1<int64> V_1,
[2] int64 V_2,
[3] string V_3)
//000011: private static string Test(long? orderId)
//000012: {
IL_0000: nop …Run Code Online (Sandbox Code Playgroud) 我正在开发一个MMORPG游戏服务器,而游戏服务器中80%的功能不需要这种方法,使用它的20%的功能占资源使用的99%.
我正在尝试完成一个限制功能.假设您调用了一个庞大的函数,或者您正在搜索特定条件的海量数据结构.被激活的一个调用可能导致大量CPU使用.
例如,一个庞大的功能,当被调用时,使用稳定的30%的CPU在4秒内完成,比如我们按照标准在拍卖系统中搜索数百万个条目.想象一下,我们有4个人同时这样做.我想制作相同的方法,只占用1%或更少的CPU,可能需要超过15-20秒才能返回相同的结果而不会使硬件匮乏.
截至目前我正在使用这个课程.
public class AsyncLazy<T> : Lazy<Task<T>>
{
public AsyncLazy(Func<T> valueFactory) :
base(() => Task.Factory.StartNew(valueFactory)) { }
public AsyncLazy(Func<Task<T>> taskFactory) :
base(() => Task.Factory.StartNew(() => taskFactory()).Unwrap()) { }
}
Run Code Online (Sandbox Code Playgroud)
这不会阻止值的初始化,并且在使用惰性值时不会阻塞.但是,在大规模操作期间,执行操作会使硬件无法完成任务,从而导致4秒内CPU使用率达到30%.我的目标是在几乎无限的时间内限制呼叫,这样他们就可以在不使硬件匮乏的情况下完成任务.
编辑:感谢Scott Chamberlin用正确的"行话"来纠正我.我不打算懒洋洋地调用一个方法,但我正在寻找"Throttle"特定的方法调用.
在使用时await,默认情况下SynchronizationContext捕获(如果存在)并且await使用该上下文执行(继续块)之后的代码块(这导致线程上下文切换).
public async Task DoSomethingAsync()
{
// We are on a thread that has a SynchronizationContext here.
await DoSomethingElseAsync();
// We are back on the same thread as before here
//(well sometimes, depending on how the captured SynchronizationContext is implemented)
}
Run Code Online (Sandbox Code Playgroud)
虽然此默认值可能在您希望在异步操作完成后返回UI线程的UI上下文中有意义,但它似乎没有意义作为大多数其他方案的默认值.对于内部库代码来说当然没有意义,因为
在我看来,微软已经决定了错误的默认值.
现在我的问题:
有没有其他(最好是更好的)方法来解决这个问题,而不是混淆await我的代码中的所有调用.ConfigureAwait(false)?这很容易忘记,使代码可读性降低.
更新:await Task.Yield().ConfigureAwait(false);在每个方法的开头
调用是否足够?如果这可以保证我将在一个没有SynchronizationContextaferwards 的线程上,所有后续await调用都不会捕获任何上下文.
如何获取用户当前关注的窗口标题?我正在制作一个与另一个Window一起运行的程序,如果用户没有关注该窗口,我发现我的程序没有理由继续更新.
那么如何确定用户关注的窗口?
我确实试着去研究一下
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
Run Code Online (Sandbox Code Playgroud)
但我似乎只能使用它,如果Window是我的应用程序的一部分,它不是.
我有一个界面,例如,为此看起来像这样:
interface IFoo<TEnum> where TEnum : struct, IConvertible, IComparable, IFormattable
{
TEnum MyEnum { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个抽象的基类,如下所示:
abstract class FooBase<TEnum> : IFoo<TEnum> where TEnum : struct, IConvertible, IFormattable, IComparable
{
public TEnum MyEnum { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我继承基类,如下所示:
class MyFoo : FooBase<MyFoo.MyFooEnum>
{
public enum MyFooEnum
{
Foo1,
Foo2,
}
}
Run Code Online (Sandbox Code Playgroud)
如何MyFoo使用类型参数从泛型方法实例化FooBase?
我几乎都在寻找这样的东西:
static class FooMethods
{
public static TFooClass GetFoo<TFooClass>() where TFooClass : FooBase, new()
{
TFooClass fooclass = new TFooClass();
return …Run Code Online (Sandbox Code Playgroud)