刚刚获得VS2012并试图获得处理async.
假设我有一个方法可以从阻塞源中获取一些值.我不希望方法的调用者阻止.我可以编写方法来获取在值到达时调用的回调,但由于我使用的是C#5,我决定使方法异步,因此调用者不必处理回调:
// contrived example (edited in response to Servy's comment)
public static Task<string> PromptForStringAsync(string prompt)
{
return Task.Factory.StartNew(() => {
Console.Write(prompt);
return Console.ReadLine();
});
}
Run Code Online (Sandbox Code Playgroud)
这是一个调用它的示例方法.如果PromptForStringAsync不是异步,则此方法需要在回调中嵌套回调.使用异步,我可以用这种非常自然的方式编写我的方法:
public static async Task GetNameAsync()
{
string firstname = await PromptForStringAsync("Enter your first name: ");
Console.WriteLine("Welcome {0}.", firstname);
string lastname = await PromptForStringAsync("Enter your last name: ");
Console.WriteLine("Name saved as '{0} {1}'.", firstname, lastname);
}
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.问题是当我调用 GetNameAsync时:
public static void DoStuff()
{
GetNameAsync();
MainWorkOfApplicationIDontWantBlocked();
}
Run Code Online (Sandbox Code Playgroud)
重点GetNameAsync是它是异步的.我不 …
我有一个包含两个可执行项目的解决方案
Main.exe依赖于Subordinate.exe.
这些项目都有App.config文件,因此在各自的输出目录中,我有Main.exe.config和Subordinate.exe.config.
当我构建时Main.exe,Subordinate.exe被复制到Main的输出目录中,但Subordinate.exe.config不是.
是否有一种标准方法告诉Visual Studio这样做?
我的系统在运行几个小时后开始产生错误的值。我在调试器下重现了它,发现问题System.Math.Round开始返回错误的值。
我有两个相同版本的 Visual Studio 实例,在同一台计算机上并行运行,具有相同的项目、相同的代码、堆栈跟踪的相同部分 - 一切都是相同的 - 除了一个一直在运行几个小时后已经开始失败,而另一个却没有。
我在各自的立即窗口中执行常量表达式,并获得不同的值。
在良好的运行中:
在糟糕的运行中:
这个小差异对我的应用程序有重大影响。
.NET 版本,从运行代码中转储:
System.Environment.Version=> 4.0.30319.42000
(typeof(string).Assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false))[0]=> 4.8.4644.0
有没有人见过这个?这是一个已知的错误吗?有什么办法可以解决这个问题吗?
编辑:@Kit 不信任立即窗口,所以这里有更多信息。我展示了立即窗口结果,因为它可以让您看到相同的常量表达式从 中产生不同的结果Math.Round。下面是实际代码中相关的行,您可以看到它也在Math.Round实际代码中产生了错误的值:
假设我有这个简单的列表渲染组件:
import {Input, Component } from 'angular2/core'
@Component({
selector: 'my-list',
template: `
<div *ngFor='#item of items' (click)='onItemClicked(item)'>
{{item}}
</div>
`
})
class MyList {
@Input() items: string[];
onItemClicked(item) { console.log('Item clicked:', item); }
}
Run Code Online (Sandbox Code Playgroud)
我这样使用它:
<my-list [items]='myAppsItems'></my-list>
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.
接下来我决定我希望用户能够为渲染项提供自己的模板,因此我更改了组件
@Component({
selector: 'my-list',
template: `
<template ngFor [ngForOf]="items" [ngForTemplate]="userItemTemplate" (click)='onItemClicked(item)'>
</template>
`
})
class MyList {
@Input() items: string[];
@ContentChild(TemplateRef) userItemTemplate: TemplateRef;
onItemClicked(item) { console.log('Item clicked:', item); }
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
<my-list [items]='items'>
<template #item>
<h1>item: {{item}}</h1>
</template>
</my-list>
Run Code Online (Sandbox Code Playgroud)
这只适用于我没有将任何事件处理程序绑定到列表项(plunker).如果我尝试绑定到click事件,就像我在组件的第一个版本中所做的那样,Angular会抛出以下异常: …
我有一个Rails项目,其中一个常量在服务请求时在某个时刻被激活.
我正在使用mime/types和restclient宝石.该restclient模块定义了MIME包含该方法的扩展type_for_extension.
module RestClient
...
def stringify_headers headers
result[key] = target_values.map { |ext| MIME::Types.type_for_extension(ext.to_s.strip) }.join(', ')
...
end
end
end
module MIME
class Types
def type_for_extension ext
candidates = @extension_index[ext]
candidates.empty? ? ext : candidates[0].content_type
end
class << self
def type_for_extension ext
@__types__.type_for_extension ext
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
我可以MIME::Types.type_for_extension在第一次调用给定的控制器动作时访问.在第二次调用时,它已经消失了.
我仍然可以使用MIME::Types.type_for,但添加的方法已经消失了,所以当我尝试使用RestClient模块时,它会在showin的行中引发异常stringify_headers:
NoMethodError, message: undefined method `type_for_extension' for MIME::Types:Class
Run Code Online (Sandbox Code Playgroud)
**这怎么可能?type_for_extension在同一个文件中定义 …
编辑:这似乎是Typescript中的一个已知问题.一个解决方案一旦实施,但由于无法解决的性能问题而最终被解决.
这种情况通常出现在我们的代码库中:
function consumer<T>(valueProducer: () => T) {
let value = valueProducer();
console.log(value);
}
class Foo {
private _value: number = 100;
getValue(): number {
return this._value;
}
constructor() {
// Oops! Inside consumer(), getValue will be called with wrong this
consumer(this.getValue);
}
}
Run Code Online (Sandbox Code Playgroud)
Typescript中的解决方案是这样的:
consumer( () => this.getValue() ); // capture correct this
Run Code Online (Sandbox Code Playgroud)
或这个:
consumer( this.getValue.bind(this) ); // bind to correct this
Run Code Online (Sandbox Code Playgroud)
这个问题对于Typescript/Javascript程序员来说可能是显而易见的,但是我们的团队正在将大量的C#移植到Typescript,而在C#中这不是错误(即C#中传递的方法自动绑定到对象实例).所以我希望类型系统能够捕获此错误,如果可能的话.
显式键入this回调使用的第一个明显的步骤:
function consumer<T>(valueProducer: (this: void) => T) {
let value …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法将Lua嵌入到我的跨平台嵌入应用程序中.问题是 - 我没有在这个平台上找到任何完整,稳定,有效的Lua实现.我尝试了以下(这里是存储库列表及其问题):
此外,当我进行简单的测试时,大多数这些实现都失败了:
for(i=0;i<100000;i)
Lua.CallSomeFunctionFromLua()
Run Code Online (Sandbox Code Playgroud)
它不时会调用错误,特别是在KopiLua和LuaInterface上.
问题是:在C#中是否有任何完整,稳定的Lua实现,没有任何平台依赖性?
此Plunker定义了一个<view>可以渲染任意模型+模板的组件.需要更改此项以替换先前呈现的内容,而不是添加新的对等项.
编辑:由于user3636086的响应,现在正在运行.
仍然存在一个问题:与Angular 1不同,Angular 2强迫我创建一个嵌套组件来更新模板(因为模板实际上是组件类的静态属性),所以我添加了一堆不必要的DOM节点.
在我们的项目中,我们更喜欢我们的大部分代码都没有直接依赖于UI框架.我们有一个viewmodel类,它将模型和视图连接在一起.以下是简化示例:
interface IView {
template: string;
}
class SalesView implements IView {
sales: number = 100;
get template() { return "<p>Current sales: {{model.sales}} widgets.<p>"; }
}
class CalendarView implements IView {
eventName: string = "Christmas Party";
get template() { return "<p>Next event: {{model.eventName}}.<p>"; }
}
class CompositeView implements IView {
calendarView = new CalendarView();
salesView = new SalesView();
get template() { return …Run Code Online (Sandbox Code Playgroud) 我的应用程序主屏幕通过SKCanvasView呈现.颜色准确反映了我在代码中指定的值.
如果我交换SKGLView(硬件加速版),不更改其他代码,结果是60%更暗:
<!--<skia:SKCanvasView PaintSurface="OnCanvasViewPaintSurface" />-->
<skia:SKGLView PaintSurface="OnCanvasViewPaintSurface" />
Run Code Online (Sandbox Code Playgroud)

为什么会发生这种情况,我该如何解决?
c# ×3
.net ×2
angular ×2
lua ×2
app-config ×1
async-await ×1
luainterface ×1
mime ×1
mono ×1
rest-client ×1
ruby ×1
skiasharp ×1
torch ×1
typescript ×1
xamarin ×1