我在foreach循环中迭代一个集合,并且想知道.当它由.NET运行时执行时
foreach (object obj in myDict.Values) {
//... do something
}
Run Code Online (Sandbox Code Playgroud)
是为每个循环调用myDict.Values还是只调用一次?
谢谢,
由于以下问题,我正在撕掉我的头发.我有一些代码循环遍历对象列表并为每个对象创建一个处理任务.
IList<TileInfo> tiles = tileSource.Schema.GetTilesInView(extent, level);
List<Task> renderingTasks = new List<Task>();
foreach (TileInfo info in tiles) {
renderingTasks.Add(Task.Factory.StartNew(new Action(delegate {
Console.WriteLine(Task.CurrentId +"Info object"+ info.GetHashCode());
}
})));
}
Run Code Online (Sandbox Code Playgroud)
此代码打印:
1Info object36963566
2Info object36963566
3Info object36963566
4Info object36963566
5Info object36963566
6Info object36963566
7Info object36963566
8Info object36963566
9Info object36963566
10Info object36963566
11Info object36963566
12Info object36963566
...
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,问题是任务似乎都引用了一个对象!
为什么这些任务都只使用列表中的一个对象?
谢谢你的帮助
我有以下代码:
try{
using (StreamReader reader = new StreamReader(...), Encoding.ASCII)){
// Code that can throw an exception
}
}catch (Exception error){
// Display error...
}
Run Code Online (Sandbox Code Playgroud)
StreamReader如果在using块中抛出异常,会发生什么?
我应该finally在关闭流的地方添加一个子句吗?
我有以下问题开始让我疯了.我Class1在Assembly1 有一堂课说.我转移Class1到Assembly2并Class1在Assembly2中废弃(保留相同的命名空间,我现在无法删除它以避免对我的用户进行重大更改).
现在我有一个单元测试组件,TestAssembly2引用程序集Assembly1和Assembly2.现在,Class1当Class1我尝试在单元测试中使用时,我遇到了编译问题的模糊性.这是有道理的,因为我Class1在两个程序集Assembly1和Assembly2中都有两个.
问题
有没有办法告诉编译器使用Class1Assembly2中定义的(因为Assembly1中的那个已经过时)而不是Assembly1?
编辑我不能使用类型转发,因为Assembly1必须没有对Assembly2的引用:(
我使用Roslyn将C#脚本引擎嵌入到我的应用程序中,到目前为止,我可以毫无问题地执行代码.我可以举例来说,执行以下代码:
using System;
var str = "Hello Roslyn";
Console.WriteLine(str);
Run Code Online (Sandbox Code Playgroud)
在上面的代码片段中构建语法树时,我遇到了编译问题.编译器抱怨直接嵌入到主命名空间中的语句,这在编写普通的C#程序时是有意义的,但在我的情况下不是这样,因为我是以脚本的方式.
问题: 有没有办法从C#脚本构建无错误的语法树?
编辑 这是我用来构建语法树的代码.
SyntaxTree tree = SyntaxTree.ParseText(context.SourceCode);
Compilation compilation = Compilation.Create("CSharp", syntaxTrees: new[] {tree}, references: references);
Run Code Online (Sandbox Code Playgroud)
谢谢
我有以下多线程程序:
class Program{
static void main(){
(new Thread(DoSomething)).Start();
}
static void DoSomething(){
// Dome something here...
}
}
Run Code Online (Sandbox Code Playgroud)
几个问题:
我试图通过使用System.currentTimeMillis()(或者替代System.nanoTime())来计算我的程序的性能,并且我注意到每次运行它时 - 它给出了完成任务所花费的时间的不同结果.
即使是直截了当的测试:
long totalTime;
long startTime;
long endTime;
startTime = System.currentTimeMillis();
for (int i = 0; i < 1000000000; i++)
{
for (int j = 0; j < 1000000000; j++)
{
}
}
endTime = System.currentTimeMillis();
totalTime = endTime-startTime;
System.out.println("Time: " + totalTime);
Run Code Online (Sandbox Code Playgroud)
产生各种不同的输出,从0到200.任何人都可以说我做错了或建议替代解决方案?
我有一个类ClassA是其他类的基类.我希望这个类构造函数是内部的和受保护的,以便它不能从我的程序集外部继承和实例化(我无法使它密封,因为我有其他内部类继承它,你可以看到我的其他相关的问题这里)所以我修改它是这样的:
public abstract ClassA{
internal protected ClassA(){
}
}
Run Code Online (Sandbox Code Playgroud)
我被告知这不会起作用,因为组合internal protected被解释为internalOR protected,显然只是构造函数: protected((从外面可见)
问题
internal protected解释为internalOR protected而不是internalAND protected?我宣布了一个字段:
WriteableBitmap colorBitmap;
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个简单的线程来执行某些操作
private void doSomething()
{
// ... bla bla bla
colorBitmap = new WriteableBitmap(/* parameters */);
myImage.Source = colorBitmap; // error here:S
}
Run Code Online (Sandbox Code Playgroud)
在Windows_Loaded事件中,我声明并启动了一个新线程:
private void window_Loaded(object sender, RoutedEventArgs e)
{
Thread th = new Thread(new ThreadStart(doSomething));
th.Start();
}
Run Code Online (Sandbox Code Playgroud)
问题是我无法改变myImage的来源.我有一个错误,如:
InvalidOperationException未处理调用线程无法访问此对象,因为另一个线程拥有它.
我试图使用Dispatcher.Invoke,但它没有帮助...
Application.Current.Dispatcher.Invoke((Action)delegate
{
myImage.Source = colorBitmap;
});
Run Code Online (Sandbox Code Playgroud)
我正在寻找一些答案,但从来没有像我一样找到这个案子.any1帮助我理解如何解决这样的问题(我最近遇到了同样的问题,但我无法调用该方法,因为其他线程拥有它).
我有一个在Azure Web应用程序上运行的Web作业。我的Web作业需要访问客户端证书以进行出站流量加密。
如何从Azure Web作业访问客户端证书?我尝试将证书安装在主机Web应用程序上,并通过以下方式访问证书:
private static X509Certificate2 LoadCertificateFromStore(string certificateThumbprint){
var store = new X509Store(StoreLocation.CurrentUser);
try {
store.Open(OpenFlags.ReadOnly);
var certCollection = store.Certificates;
var currentCertificate = certCollection.Find(X509FindType.FindByThumbprint, certificateThumbprint, false);
if (currentCertificate.Count == 0)
throw new InvalidOperationException("Could not find certificate " + certificateThumbprint);
return currentCertificate[0];
} finally{
store.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,由于找不到证书,因此无法正常工作。我被困住了。