这是什么奇怪的方法调用?

发布于 2024-09-29 00:12:41 字数 1195 浏览 1 评论 0原文

我刚刚阅读了 Microsoft Surface 教程。有以下 C# 示例:

private void OnCenterItems(object sender, RoutedEventArgs e)
{
    var x = this.Photos.ActualWidth / 2;
    var y = this.Photos.ActualHeight / 2;
    FindChildren(this.Photos, d => d.GetType() == typeof(ScatterViewItem), 
                          d => ((ScatterViewItem)d).Center = new Point(x,y));
}

private void FindChildren(DependencyObject source, 
                          Predicate<DependencyObject> predicate, 
                          Action<DependencyObject> itemFoundCallback)
{
    int childCount = VisualTreeHelper.GetChildrenCount(source);
    for (int i = 0; i < childCount; i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(source, i);
        if (predicate(child))
        {
            itemFoundCallback(child);
        }
        FindChildren(child, predicate, itemFoundCallback);
    }
}

我想我或多或少了解这两个方法在做什么,但我从未见过这样的方法调用:

 FindChildren(this.Photos, d => d.GetType() == typeof(ScatterViewItem), 
                           d => ((ScatterViewItem)d).Center = new Point(x,y));

这可能是因为我是一名 Java 程序员。那么谁能解释一下这个语法是做什么的?

I just read the Microsoft Surface Tutorial. There is the following C# sample:

private void OnCenterItems(object sender, RoutedEventArgs e)
{
    var x = this.Photos.ActualWidth / 2;
    var y = this.Photos.ActualHeight / 2;
    FindChildren(this.Photos, d => d.GetType() == typeof(ScatterViewItem), 
                          d => ((ScatterViewItem)d).Center = new Point(x,y));
}

private void FindChildren(DependencyObject source, 
                          Predicate<DependencyObject> predicate, 
                          Action<DependencyObject> itemFoundCallback)
{
    int childCount = VisualTreeHelper.GetChildrenCount(source);
    for (int i = 0; i < childCount; i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(source, i);
        if (predicate(child))
        {
            itemFoundCallback(child);
        }
        FindChildren(child, predicate, itemFoundCallback);
    }
}

I think I understand more or less what this two methods are doing, but I never saw a method call like this:

 FindChildren(this.Photos, d => d.GetType() == typeof(ScatterViewItem), 
                           d => ((ScatterViewItem)d).Center = new Point(x,y));

This is maybe because I'm a Java programmer. So can anyone explain what this syntax is doing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一梦等七年七年为一梦 2024-10-06 00:12:41

这些是 lambda 表达式,生成匿名方法。

d => d.GetType() == typeof(ScatterViewItem)

这可以写成

(object d) => { return (d.GetType() == typeof(ScatterViewItem)); }

这个调用代表这个方法,如果写出来的话:

public bool CheckObjecteEqualsScatterViewItemType (object d)
{
  return d.GetType() == typeof(ScatterViewItem);
}

FindChildren 方法需要一个通用委托(相当于 C 中的函数指针,但强类型)Func(即任何采用对象并返回 bool 的方法)作为参数给出,lambda 表达式会动态生成此方法。 (这是由编译器完成的,因此不会在运行时完成,并且经过完全类型检查,并且不涉及性能损失)。

These are lambda expressions, generating anonymous methods.

d => d.GetType() == typeof(ScatterViewItem)

this can be written as

(object d) => { return (d.GetType() == typeof(ScatterViewItem)); }

this call would represent this method, if written out:

public bool CheckObjecteEqualsScatterViewItemType (object d)
{
  return d.GetType() == typeof(ScatterViewItem);
}

The method FindChildren requires a generic delegate (compareable to a function pointer in C, but strongly typed) Func<object, bool> (ie. any method taking an object and returning a bool) to be given as parameter, the lambda expression generates this method on the fly. (This is done by the compiler, so this is not done at runtime and is fully type checked, and involves no loss of performance).

千年*琉璃梦 2024-10-06 00:12:41

您在下面看到的是一个 C# lambda 表达式,

d => d.GetType() == typeof(ScatterViewItem)

它本质上是一个生成委托的表达式。有些人喜欢将它们视为内联函数。

以下是一些阅读参考

What your seeing with the following is a C# lambda expression

d => d.GetType() == typeof(ScatterViewItem)

It's essentially an expression which produces a delegate. Some people like to think of them as inline functions.

Here are some references for reading

自在安然 2024-10-06 00:12:41

<代码> d => d.GetType() == typeof(ScatterViewItem) 称为 Lambda 表达式。它说“对于任何 DependencyObject,d,返回 (d.GetType() == typeof(ScatterViewItem))”。这是一个没有名字的方法,一个匿名方法。

长手将是:

static bool myPredicate(DependencyObject d)
{
  return d.GetType() == typeof(ScatterViewItem);
}

用以下方式调用:

FindChildren(this.Photos, myPredicate, [...] )

Predicate 是委托类型,或“签名形状”。它可以保存对任何采用 DependencyObject 并返回 bool 的方法的引用。

FindChildren 知道使用提供的谓词来评估它应该执行的操作。因为它需要某些类型的数据并返回某些类型的数据,所以 FindChildren 并不关心操作如何发生,它只需调用它并知道它将得到有用的答案。

d => d.GetType() == typeof(ScatterViewItem) is called a Lambda Expression. It says "For any DependencyObject, d, return (d.GetType() == typeof(ScatterViewItem))". It's a method without a name, an anonymous method.

Long-hand would be:

static bool myPredicate(DependencyObject d)
{
  return d.GetType() == typeof(ScatterViewItem);
}

called with:

FindChildren(this.Photos, myPredicate, [...] )

Predicate<DependencyObject> is a delegate-type, or "signature shape". It can hold a reference to any method which takes a DependencyObject and returns a bool.

FindChildren knows to use the provided predicate to evaluate whatever it's supposed to do. Because it takes certain kinds of data and returns certain kinds of data, FindChildren doesn't care how the operation happens, it can just call it and know it will get a useful answer.

心的位置 2024-10-06 00:12:41

=> 是新的“lambda”运算符,用于动态定义函数。

请参阅:www.switchonthecode.com/tutorials/csharp-tutorial-the-lambda - 运算符

The => is the new "lambda" operator, for defining functions on the fly.

See: www.switchonthecode.com/tutorials/csharp-tutorial-the-lambda-operator

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文