Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何实现类似SomeObject.SomeFunction().SomeOtherFunction()的代码;_C# - Fatal编程技术网

C# 如何实现类似SomeObject.SomeFunction().SomeOtherFunction()的代码;

C# 如何实现类似SomeObject.SomeFunction().SomeOtherFunction()的代码;,c#,C#,今天,我搜索了一行代码,其编写方式如下: SomeObject.SomeFunction().SomeOtherFunction(); 我无法理解这一点。我试着在谷歌上搜索,但没有运气 请帮助我理解这一点。SomeObject有一个名为SomeFunction()的函数。此函数返回一个对象(根据您的示例,我们的对象类型未知)。此对象有一个名为SomeOtherFunction()的函数 “如何实现”这个问题回答起来有点模糊。这被称为Fluent编码或方法链接,是一种允许将命令链接在一起的编程方

今天,我搜索了一行代码,其编写方式如下:

SomeObject.SomeFunction().SomeOtherFunction();
我无法理解这一点。我试着在谷歌上搜索,但没有运气


请帮助我理解这一点。

SomeObject有一个名为SomeFunction()的函数。此函数返回一个对象(根据您的示例,我们的对象类型未知)。此对象有一个名为SomeOtherFunction()的函数


“如何实现”这个问题回答起来有点模糊。

这被称为Fluent编码或方法链接,是一种允许将命令链接在一起的编程方法。这在LINQ中非常常见,您可能会遇到以下情况:

var result = myList.Where(x => x.ID > 5).GroupBy(x => x.Name).Sort().ToList();
var result = myList.Where(x => x.ID > 5);
result = result.GroupBy(x => x.Name);
result = result.Sort();
result = result.ToList();
这将为您提供大于5的所有记录,然后按名称分组、排序并作为列表返回。同样的代码可以像这样用长时间编写:

var result = myList.Where(x => x.ID > 5).GroupBy(x => x.Name).Sort().ToList();
var result = myList.Where(x => x.ID > 5);
result = result.GroupBy(x => x.Name);
result = result.Sort();
result = result.ToList();

但是你可以看到,这是一种冗长得多的编程风格。

这种编程风格称为风格

例如:

可以创建对象并使用方法,如下所示:

  var fluentStyle = new FluentStyle();
     fluentStyle.ConnectToDb().FetchData().BindData().RefreshData();
考虑以下几点

public class FirstClass
{
    public SecondClass SomeFunction()
    {
        return new SecondClass();  
    }
}

public class SecondClass
{
    public void SomeOtherFunction()
    {

    }
}
因此,以下是等效的

 FirstClass SomeObject = new FirstClass();
 SomeObject.SomeFuntion().SomeOtherFunction();


这种类型的链接可能涉及扩展方法。这些方法允许向现有类(甚至那些没有源代码的类)添加新方法

e、 g


这可以使用扩展方法来完成

 public class FirstClass
{
}

public class SecondClass
{
}
public class ThridClass
{
}

public static class Extensions
{

    public static SecondClass GetSecondClass(this FirstClass f)
    {
        return new SecondClass();
    }
    public static ThridClass GetThridClass(this SecondClass s)
    {
        return new ThridClass();
    }
}
}

然后你就可以

        FirstClass f= new FirstClass();
        f.GetSecondClass().GetThridClass();

SomeFunction()
返回一个对象,该对象具有一个方法
someOtherFunction()
SomeObject。SomeFunction()
只返回一个具有方法
someOtherFunction
的类型的值,它没有什么特殊之处。这就像您编写了例如
user.GetFullName().ToLower()
。尝试使用
method chaining
SomeObject有一个方法SomeFunction,它返回一个对象,该对象本身公开了一个SomeOtherFunction。最佳答案是,告诉您它是什么,以及如何创建它。一点问题都没有。其他答案也与实现Fluent API的扩展或链接方法相关,但我认为这是演示它的最简化的OO示例
        FirstClass f= new FirstClass();
        f.GetSecondClass().GetThridClass();