小编Til*_*lak的帖子

使用Assert作为前提条件是否可以?

我正在阅读与处理空值有关的这篇文章.

其中一个建议(根据SO帖子)是在空值无效时使用Assert.

我(到目前为止)在测试项目中广泛使用了null.在普通代码(测试项目除外)中使用Asserts语句对我来说很奇怪.

为什么 - >因为我从未使用过这种方式,所以也不要在任何书中读过它.

问题
1.使用Asserts作为前提条件是否可行
2. 断言的优点/缺点检查参数并抛出Argument___Exception

如果重要,我要求.NET(不适用于java)

.net c# null assert illegalargumentexception

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

使 WPF 按钮看起来不那么平坦

我的表单上有 2 个按钮,它们看起来都非常非常扁平。

扁平按钮

我似乎找不到让它们看起来更像的方法:

不平按钮

我的按钮的 XAML 是:

<Button x:Name="bttnDailyReport" HorizontalAlignment="Left" Margin="618,27,0,0" VerticalAlignment="Top" Width="121" Height="93" FontFamily="Microsoft Sans Serif" FontSize="20" FontWeight="Bold" Grid.Column="1" BorderBrush="Black">
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"><Run Text="  Generate"/><LineBreak/><Run Text="Daily Report"/></TextBlock>
</Button>
<Button x:Name="bttnCancel" HorizontalAlignment="Left" Margin="618,126,0,0" VerticalAlignment="Top" Width="121" Height="93" FontFamily="Microsoft Sans Serif" FontSize="20" FontWeight="Bold" Click="BttnCancelClick">
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"><Run Text="Exit"/></TextBlock>
</Button>
Run Code Online (Sandbox Code Playgroud)

现在,我的问题是,是否可以使按钮看起来像 WinForms 中的按钮,或者我是否使用扁平按钮?

c# wpf xaml templating button

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

事件和线程

假设我有:

ethernet_adapter.PacketArrived += (s, e) => 
{
    //long processing...
};
Run Code Online (Sandbox Code Playgroud)

处理可能需要很长时间,而当它处于中间时,另一个数据包已到达.接下来会发生什么:处理完成然后触发另一个事件,或者可能在新线程上立即触发新事件?

.net c# events multithreading event-handling

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

返回linq查询的数据类型

我有一个函数返回一个对象,该对象表示我的数据库中的记录以及其他列.我没有为这个对象创建一个单独的类,而是想知道是否还有其他方法,例如:

public object GetRecord(string key)
{
    var item = select new {column1, column2};

    return item;
}

public void main() 
{
    var item = GetRecord(1);

    //  I want to be able to reference column1 on item.  
    var x = item.column1;  
}
Run Code Online (Sandbox Code Playgroud)

.net c# linq anonymous dynamic

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

取消通过System.Diagnostics.Process.Start()启动的进程

我正在编写一个应用程序,我在其中运行了一个进程BackgroundWorker.我想支持从用户界面取消,但我没有看到一个简单的方法来做到这一点.

Process实际上是一个相当长的运行命令行exe文件.输出通过Progress.OutputDataReceived事件异步重定向,并用于向GUI报告进度.

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
    using (var process = new Process())
    {
        //...

        process.Start()

        //...

        process.WaitForExit();
    }
}

private void CancelWorker()
{
    worker.CancelAsync();
    // where to actually listen for the cancellation???
}
Run Code Online (Sandbox Code Playgroud)

除了StandardInput应用程序本身将响应特定输入以中止之外,似乎没有办法让进程"监听"来自主线程的任何输入.

有没有办法根据主线程的取消请求取消进程?

出于我在运行过程中运行的EXE的目的,我可以调用Process.Close()退出而没有任何副作用,但该Process对象仅为worker_DoWork()方法所知,因此我需要跟踪Process实例以进行取消. ..这就是为什么我希望有更好的方法.

(如果重要的话,我的目标是.NET 3.5以解决兼容性问题.)

c#

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

Action,Func和Predicate委托 - C#

我试图理解Action<T>, Func<T> and Predicate<T>代表之间的差异,作为我的WPF/MVVM学习的一部分.

我知道Action<T> and Func<T>两个都是零到一个+参数,只Func<T>返回一个值,而Action<T>不是.

至于Predicate<T>- 我不知道.

因此,我提出了以下问题:

  1. 怎么Predicate<T>办?(欢迎举例!)
  2. 如果什么都不Action<T>返回,那么使用它会不会更简单?(或任何其他类型,如果我们正在讨论.)voidFunc<T>

我希望你在问题中避免使用LINQ/List示例.
我已经看过那些了但是它们只是让它变得更加混乱,因为让我对这些代表"感兴趣"的代码与它无关(我想!).
因此,我想使用我熟悉的代码来获得我的答案.

这里是:

public class RelayCommand : ICommand
{
    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

public RelayCommand(Action<object> execute)
    : this(execute, null)
{
}

public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
    if (execute == null)
        throw new ArgumentNullException("execute");

    _execute = execute;
    _canExecute = canExecute;
}

[DebuggerStepThrough]
public bool CanExecute(object parameters)
{
    return …
Run Code Online (Sandbox Code Playgroud)

.net c# wpf delegates mvvm

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

带MVVM模式的WPF MessageBox?

假设我想向用户显示一些验证错误.在MVVM模式中,我可以有一个标签绑定到我的viewmodel上的某个属性.但是,如果我想在严格遵守MVVM模式的同时显示消息框,该怎么办呢?我的viewmodel会绑定什么,以及它将如何触发创建/显示消息框?

.net c# wpf messagebox mvvm

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

如何在c#中创建嵌套的自定义配置部分?

我需要以下列方式进行自定义配置

<root>
<group>
 <groupitem> 
    <property1/>
    <property2/>
    <property3/>
    <property4/>   
 </groupitem>
<group>
</root>
Run Code Online (Sandbox Code Playgroud)

我没有找到任何如何做到这一点的例子.

以下非常接近,

嵌套配置部分app.config

但我仍然坚持定义与groupitem相对应的类.如果我改变property1,property2从元素属性,很容易定义.

但如果这些属性具有嵌套属性,那将会产生问题.

问题是,如何以上面定义的方式定义嵌套层次结构?

c# configuration custom-configuration

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

派生类型也成为基类中定义的依赖项属性的所有者(在WPF/XAML中)

在其中一个模块中,我看到了以下样式设置.

<Style TargetType="Rectangle">
            <Style.Triggers>
                <EventTrigger RoutedEvent="MouseEnter">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation To="300" Duration="0:0:1.5" 
                    AccelerationRatio="0.10" DecelerationRatio="0.25" 
                    Storyboard.TargetProperty="(Canvas.Width)" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
Run Code Online (Sandbox Code Playgroud)

注意,TargetTypeRectangle,Storyboard.TargetPropertyCanvas.Width.样式/触发器仍然正常工作.这是动画的Rectangle.width财产.

我理解在Storyboard.TargetProperty(或XAML中的任何其他地方),我们必须使用PropertyPath语法,就像(ownerType.PropertyName).

我的问题是如何设置动画Canvas.Width是动画Rectangle.Width

  1. 是因为Canvas.Width,Rectangle.Width或FrameworkElement.Width都指向FrameowrkElement.Width属性,因为Width在FrameworkElement中定义,Canvas/Rectangle是从它派生的.
  2. 或者是因为继承,Canvas和Rectangle都成为依赖属性的所有者.

.net c# wpf xaml dependency-properties

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

SQL Server:将varchar转换为十进制(同时考虑指数表示法)

我需要转换表的数据并进行一些操作.其中一个列数据类型是Varchar,但它存储十进制数字.我正在努力将varchar转换为十进制.

我试过跟随CAST(@ TempPercent1 AS DECIMAL(28,16))

问题是数据也有一些指数表示法的值,例如:1.61022e-016.sql查询在遇到这样的值时抛出错误.错误是Varchar

我需要知道如何在varchar到十进制转换期间处理指数表示法值?

sql sql-server decimal type-conversion

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