小编dha*_*ech的帖子

类成员初始化的简写表示法

在C#块中,我可以按如下方式定义和初始化变量:

var xyz = new Xyz();
Run Code Online (Sandbox Code Playgroud)

xyz将相应地设置类型.

但是,在类级别,我必须指定两次类型:

class Abc
{
    Xyz xyz = new Xyz();
}
Run Code Online (Sandbox Code Playgroud)

是否有一个简写语法,避免键入两次类型名称?

对于短类型来说这不是什么大问题,Xyz但是较短的符号会对LongTypeNames有帮助.

c#

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

列表上的扩展方法?

如何在F#列表上定义扩展方法?

像这样的天真尝试会导致错误:

type list with
    member this.abc() = 100
Run Code Online (Sandbox Code Playgroud)

extension-methods f#

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

模式的方法:"if(obj是Abc &&(obj as Abc)......)"

这是两个简单的类:

class Abc { public int x; }
class Bcd { public int y; }
Run Code Online (Sandbox Code Playgroud)

鉴于它obj是类型object,这里有几个测试AbcBcd具有某些特征的例子:

if (obj is Abc && (obj as Abc).x > 0 && Math.Pow((obj as Abc).x, 2) > 100)
    Console.WriteLine(123);

if (obj is Bcd && (obj as Bcd).y > 0 && Math.Pow((obj as Bcd).y, 2) > 100)
    Console.WriteLine(234);
Run Code Online (Sandbox Code Playgroud)

处理这种模式的好方法是什么:

if (obj is Abc && (obj as Abc).SomePropertyOrMethod && ...
Run Code Online (Sandbox Code Playgroud)

一种方法是Is扩展方法:

public static bool Is<T>(this object obj, …
Run Code Online (Sandbox Code Playgroud)

c#

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

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

如何使用 GDB 调试 jonesforth?

jonesforth 通常按如下方式启动:

cat jonesforth.f  - | ./jonesforth
Run Code Online (Sandbox Code Playgroud)

有什么好的调试方法吗jonesforth

assembly gdb gnu-assembler forth

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

以编程方式访问.NET API

有没有办法检索有关.NET API的元数据?

例如,假设我想获得为其定义的所有属性的列表System.Windows.Documents.List.以一些结构化格式(如XML,JSON等)获取此信息会很好.每个条目应如下所示:

<property name="MarkerStyle" type="TextMarkerStyle" get="true" set="true"/>
Run Code Online (Sandbox Code Playgroud)

我想避免屏幕刮掉MSDN库.:-)

.net c# reflection system.reflection

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

可空类成员表示数学未知数

假设我有一节课Point:

class Point
{
    public double? x, y;
}
Run Code Online (Sandbox Code Playgroud)

为了表明其中任何一个x或者y是未知的,我已经将它们的类型设为可空.

现在,当在数学表达式中使用这些值时,每次必须将它们的值转换为双倍是不方便的.即Math.Sin(p.x)产生编译时错误; 你必须投它:Math.Sin((double)p.x).

我解决转换问题的方法是使用包装器只读属性来执行转换:

class Point
{
    public double? x, y;

    public double X { get { if (x != null) return (double)x; else throw new Exception(); } }
    public double Y { get { if (y != null) return (double)y; else throw new Exception(); } }
}
Run Code Online (Sandbox Code Playgroud)

这是一个好方法吗?

c# nullable

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

关于绑定到非属性(即字段)的官方说法是什么?

从WPF 4 Unleashed一书中可以看出:

虽然source属性可以是任何.NET对象上的任何.NET属性,但数据绑定目标也不是这样.target属性必须是依赖项属性.另请注意,源成员必须是真实(和公共)属性,而不仅仅是简单字段.

但是,这是声明源必须是属性的反例.该程序将a Label和a 绑定ListBox到正常的类型字段ObservableCollection<int>.

XAML:

<Window x:Class="BindingObservableCollectionCountLabel.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">

    <DockPanel>

        <StackPanel>
            <TextBox Name="textBox" Text="10"/>
            <Button Name="add" Click="add_Click" Content="Add"/>
            <Button Name="del" Click="del_Click" Content="Del"/>
            <Label Name="label" Content="{Binding Source={StaticResource ints}, Path=Count}"/>
            <ListBox ItemsSource="{Binding Source={StaticResource ints}}"/>
        </StackPanel>

    </DockPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

C#:

using System;
using System.Windows;
using System.Collections.ObjectModel;

namespace BindingObservableCollectionCountLabel
{
    public partial class MainWindow : Window
    {
        public ObservableCollection<int> ints;

        public MainWindow()
        {
            Resources.Add("ints", ints = new ObservableCollection<int>());

            InitializeComponent();
        }

        private void add_Click(object sender, …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf xaml

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

从FileSystemWatcher事件更新ListBox

我想更新ListBox一个FileSystemWatcher事件.当事件运行时,我收到此错误:

Unhandled Exception: System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
   at System.Windows.Threading.Dispatcher.VerifyAccess()
   at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
   at System.Windows.Controls.Panel.get_IsItemsHost()
   at System.Windows.Controls.Panel.VerifyBoundState()
   at System.Windows.Controls.Panel.OnItemsChanged(Object sender, ItemsChangedEventArgs args)
   at System.Windows.Controls.ItemContainerGenerator.OnItemAdded(Object item, Int32 index)
   at System.Windows.Controls.ItemContainerGenerator.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args)
   at System.Windows.Controls.ItemContainerGenerator.System.Windows.IWeakEventListener.ReceiveWeakEvent(Type managerType, Object sender, Eve
ntArgs e)
   at System.Windows.WeakEventManager.DeliverEventToList(Object sender, EventArgs args, ListenerList list)
   at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args)
   at System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args)

   at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args)
   at System.Windows.Controls.ItemCollection.System.Windows.IWeakEventListener.ReceiveWeakEvent(Type …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml

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

如何通过 PowerShell 列出 Reddit 子版块的热门链接?

在 PowerShell 中通过 API 检索 subreddit 链接的好方法是什么?

powershell

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