小编Ere*_*mez的帖子

尝试抓住加速我的代码?

我写了一些代码来测试try-catch的影响,但看到了一些令人惊讶的结果.

static void Main(string[] args)
{
    Thread.CurrentThread.Priority = ThreadPriority.Highest;
    Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;

    long start = 0, stop = 0, elapsed = 0;
    double avg = 0.0;

    long temp = Fibo(1);

    for (int i = 1; i < 100000000; i++)
    {
        start = Stopwatch.GetTimestamp();
        temp = Fibo(100);
        stop = Stopwatch.GetTimestamp();

        elapsed = stop - start;
        avg = avg + ((double)elapsed - avg) / i;
    }

    Console.WriteLine("Elapsed: " + avg);
    Console.ReadKey();
}

static long Fibo(int n)
{
    long n1 = 0, n2 …
Run Code Online (Sandbox Code Playgroud)

.net c# clr try-catch performance-testing

1463
推荐指数
5
解决办法
10万
查看次数

如何在Visual Studio 2010中禁用智能感知建议模式

Visual Studio 2010允许使用Ctrl+ Alt+ Space快捷方式在"完成模式"和"建议模式"(又名"低影响模式")之间切换智能感知模式.我喜欢在常规完成模式下工作,但似乎我一直误打Ctrl+ Alt+ Space,听起来很傻!

有没有办法禁用Intellisense建议模式(或Ctrl+ Alt+ Space快捷方式),以便我总是在常规完成模式下工作?

visual-studio-2010 visual-studio

19
推荐指数
1
解决办法
6890
查看次数

DataGridTextColumn可见性绑定

我正在尝试将列可见性绑定到另一个元素的可见性,如下所示:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>
<StackPanel>
    <CheckBox x:Name="chkColumnVisible" Content="Show column" />
    <DataGrid x:Name="MyDataGrid" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Column1" Visibility="{Binding ElementName=chkColumnVisible, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}"/>
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

但我在VS输出中得到这个错误:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=IsChecked; DataItem=null; target element is 'DataGridTextColumn' (HashCode=48860040); target property is 'Visibility' (type 'Visibility')
Run Code Online (Sandbox Code Playgroud)

是否有纯XAML方法来实现这一目标?

c# wpf xaml wpfdatagrid

13
推荐指数
2
解决办法
2万
查看次数

扩展方法与静态方法优先级

考虑以下程序:

class A
{
    public static void Foo()
    {
    }
}

static class Ext
{
    public static void Foo(this A a)
    {
    }
}

class Program
{
    static void Main(string[] args)
    {
        var a = new A();
        a.Foo();
    }
}
Run Code Online (Sandbox Code Playgroud)

这无法编译,错误如下:

无法使用实例引用访问成员'Test.A.Foo()'; 用类型名称来限定它

为什么编译器会忽略扩展方法?

c# extension-methods static-methods

11
推荐指数
2
解决办法
1562
查看次数

如何在嵌套引用中检查null

寻找一些最佳实践指导.假设我有一行代码如下:

Color color = someOrder.Customer.LastOrder.Product.Color;
Run Code Online (Sandbox Code Playgroud)

Customer,LastOrder,Product和Color可以null在正常条件下.但是,如果路径中的任何一个对象为null,我希望color为null; 为了避免空引用异常,我需要检查每个对象的空条件,例如

Color color = someOrder == null ||
              someOrder.Customer == null || 
              someOrder.Customer.LastOrder == null ||
              someOrder.Customer.Product == null ? 
              null : someOrder.Customer.LastOrder.Product.Color;
Run Code Online (Sandbox Code Playgroud)

或者我可以这样做

Color color = null;
try {color = someOrder.Customer.LastOrder.Product.Color}
catch (NullReferenceException) {}
Run Code Online (Sandbox Code Playgroud)

第一种方法显然有效,但编码和更难阅读似乎更乏味.第二种方法稍微容易一点,但对此使用异常处理可能不是一个好主意.

是否有另一种检查空值的快捷方式,并在必要时将null指定为颜色?或者在使用这种嵌套引用时如何避免NullReferenceExceptions的任何想法?

c# nullreferenceexception c#-4.0

7
推荐指数
1
解决办法
3534
查看次数

Clr名称空间映射到默认的xaml名称空间

我有点厌倦了必须在每个xaml文件中声明一个xmlns并且必须为我的自定义控件使用前缀.是否可以将clr命名空间映射到"http://schemas.microsoft.com/winfx/2006/xaml/presentation"?

我在AssemblyInfo.cs中尝试了以下内容:

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation","MyOwnNamespace")]
Run Code Online (Sandbox Code Playgroud)

但这似乎不起作用.我仍然得到一个编译错误,如:

XML名称空间"http://schemas.microsoft.com/winfx/2006/xaml/presentation"中不存在标记"MyCustomControl".

注意:我的控件在同一个程序集中(我有一个程序集).

.net c# wpf xaml

7
推荐指数
1
解决办法
2625
查看次数

将EF LINQ方法语法转换为查询语法

你会如何使用LINQ查询语法编写这个完全相同的查询?

var q2 = list.GroupBy(x => x.GroupId)
             .Select(g => g
                 .OrderByDescending(x => x.Date)
                 .FirstOrDefault());
Run Code Online (Sandbox Code Playgroud)

我虽然这应该工作,但它没有:

var q1 = from x in list
         group x by x.GroupId into g
         from y in g
         orderby y.Date descending
         select g.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)

如果你想玩它,这是一个测试程序:

public class MyClass
{
    public int Id { get; set; }
    public string GroupId { get; set; }
    public DateTime Date { get; set; }

    public override bool Equals(object obj)
    {
        var item = obj as MyClass;
        return item == …
Run Code Online (Sandbox Code Playgroud)

c# linq linq-query-syntax

5
推荐指数
1
解决办法
748
查看次数

启用本机代码调试时,AccessViolationException消失

AccessViolationException当我在Windows 7 x64上的Visual Studio 2012中运行Google Drive API示例时,我得到了一个.我的项目针对的是.Net 4.5.我在第185行得到了例外:

await service.Files.Delete(file.Id).ExecuteAsync();
Run Code Online (Sandbox Code Playgroud)
  • 它发生在调试和发布模式以及所有平台(x86,x64,AnyCPU)中.
  • 在没有连接调试器的情况下运行时不会发生这种情况("Start without Debugging").
  • 当我在Project属性中启用"启用本机代码调试"时,不会发生这种情况.

为什么启用本机代码调试可能会阻止异常?

注意:运行示例需要NuGet包(预发布):Google.Apis.Drive.v2


编辑:我希望谷歌的人会插手并告诉他们是否也看过这个,因为示例说明说:

  • 使用Visual Studio打开Goog​​leApisSamples.sln
  • 单击Build> Rebuild Solution
  • 在Drive.Sample\bin\Debug中执行.exe

这很奇怪,因为他们直接从调试文件夹中执行exe而不是只是说"运行示例".

c# windows-7-x64 .net-4.5 google-drive-api visual-studio-2012

4
推荐指数
1
解决办法
1431
查看次数

覆盖ComboBox中的默认TextBlock样式

我在App.xaml中定义了默认的TextBlock样式,这似乎也会影响ComboBox项的文本颜色。现在,如何在主窗口中显式设置ComboBox的文本颜色?(我想保留默认样式,但组合框的文本颜色为蓝色而不是红色...)

应用程式

<Application x:Class="WpfApplication1.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="Red" />
    </Style>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow">
<Grid>
    <ComboBox Name="comboBox1" SelectedIndex="0" HorizontalAlignment="Left" VerticalAlignment="Top">
        <ComboBoxItem Content = "Item1"/>
        <ComboBoxItem Content = "Item2"/>
        <ComboBoxItem Content = "Item3"/>
    </ComboBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)

我尝试过的事情:

  1. 设置组合框。前景
  2. 设置TextElement.Foreground
  3. 设置TextBlock.Foreground
  4. 在ComboBox.Resources中定义另一个隐式TextBlock样式
  5. 在Grid.Resources中定义另一个隐式TextBlock样式
  6. 在Window.Resources中定义另一个隐式TextBlock样式

c# wpf

3
推荐指数
1
解决办法
2890
查看次数

表使用太多磁盘空间

当我在Sql Server Management Studio中运行"按表排列的磁盘使用率"报告时,它显示了一个使用大约1.8GB磁盘空间的表:

在此输入图像描述

表定义:

CREATE TABLE [dbo].[RecipeItems](
    [wo_id] [varchar](50) NOT NULL,
    [invent_id] [varchar](50) NOT NULL,
    [invent_dim_id] [varchar](50) NULL,
    [ratio] [float] NOT NULL
) ON [PRIMARY]
Run Code Online (Sandbox Code Playgroud)

粗略估计,每行只需不到200个字节,只有7K记录,这不应超过1-2MB.但显然,事实并非如此.这个表使用如此多的存储空间可能是什么原因?

sql-server sql-server-2008

2
推荐指数
1
解决办法
2689
查看次数

在C#中,是否可以将类可访问性限制为定义它的文件?

我不能在另一个类中声明它是私有的,因为我有几个使用它的类.我也不希望它是内部的,否则它将暴露给同一程序集中的其他类.我希望它只能访问同一文件中的类.在C#中有可能吗?

c#

2
推荐指数
2
解决办法
1490
查看次数

等待在GUI中显示多个任务

假设我有两个资源AB,这两者我想告诉我的用户,通过MVVM(此==视图模型)

this.A = GetA();
this.B = GetB();
Run Code Online (Sandbox Code Playgroud)

我一开始使用TPL:

this.A = await GetAAsync();
this.B = await GetBAsync();
Run Code Online (Sandbox Code Playgroud)

这开始得到A.什么时候A准备就绪,它显示A并继续做同样的事情B- 不是一个非常好的解决方案.会更好:

var taskA = GetAAsync();
var taskB = GetBAsync();
this.A = await taskA;
this.B = await taskB;
Run Code Online (Sandbox Code Playgroud)

现在,这开始得到A,开始得到B并等待A.当A准备好了,就说明A和等待B,直到它显示了.看起来不错,但是,如果A有时需要花费更多的时间来加载,那该B怎么办?

我该如何实现以下场景:

  • 开始加载A.
  • 开始加载B.
  • 当其中一个准备就绪时,请展示它.
  • 当另一个准备就绪时,也要显示它.

.net c# silverlight task-parallel-library async-await

2
推荐指数
1
解决办法
171
查看次数

.Net Core 3 自包含的单个可执行文件可以反编译吗?

我尝试使用 Dotpeek 和 ILSpy.Net 反编译(我自己的代码),但失败了。

我需要对 .Net Core 3自包含单个可执行文件的分布式二进制文件进行特殊混淆吗?

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <PublishTrimmed>true</PublishTrimmed>
    <PublishReadyToRun>true</PublishReadyToRun>
    <PublishSingleFile>true</PublishSingleFile>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
  </PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

obfuscation decompiling ilspy .net-core

2
推荐指数
1
解决办法
1414
查看次数