【Azure Developer】C# / .NET 静态函数中this关键字的作用

简介: 【Azure Developer】C# / .NET 静态函数中this关键字的作用

问题描述

在查看.NET代码的时候,发现一个静态方法,第一个参数使用 this 关键字,它在这里是什么作用呢?

public static XElement AquireElement(this XContainer container,
        string name, bool addFirst = false)
    {
      ...

问题解答

通过查看微软的官方博文介绍(https://asp-blogs.azurewebsites.net/scottgu/new-orcas-language-feature-extension-methods), 这是C#的功能:扩展方法。

扩展方法允许开发人员向现有 CLR 类型的公共协定添加新方法,而无需对其进行子类化或重新编译原始类型。  

扩展方法有助于将当今动态语言中流行的“鸭子类型”支持的灵活性与强类型语言的性能和编译时验证相结合。

利用新的扩展方法功能向各个类型添加方法,为开发人员提供了许多有用的可扩展性场景。  然而,扩展方法真正强大的原因在于它们不仅能够应用于单个类型,而且还能够应用于 .NET中的任何父基类或接口。

这使得开发人员能够构建各种丰富的、可组合的框架扩展,这些扩展可以在 .NET 中使用。

例如:

public static int Foo(this MyClass arg)

就可以用

MyClass myClass = new MyClass();

int i = myClass.Foo();

代替传统的写法

MyClass myClass = new MyClass();

int i = Foo(myClass);

如果MyClass类是同一个项目里面的类,那么此方法优势不明显。 但是如果 MyClass类是由其它第三方开发,然后只是在自己的项目中引用。

那么,扩展方法(this)就非常强大。

 

本文中遇见的就是对System.Xml.Linq基类中的XContainer对象进行了扩展。添加了获取一个指定元素的扩展方法AquireElement,如元素不存在,则创建。

public static class XmlLinqExtensions
    {
        public static XElement AquireElement(this XContainer container,
            string name, bool addFirst = false)
        {
            var element = container.Element(name);
            if (null != element)
            {
                return element;
            }
            element = new XElement(name);
            if (addFirst)
            {
                container.AddFirst(element);
            }
            else
            {
                container.Add(element);
            }
            return element;
        }
    }

使用时

//XDocument document;


var configElement = document.AquireElement("configuration", addFirst: true);

var configSections = configElement.AquireElement("configSections", addFirst: true);

 

PS: 原生类中并没有包含AquireElement方法。

 

参考资料

Extension Methods :https://asp-blogs.azurewebsites.net/scottgu/new-orcas-language-feature-extension-methods

Use of "this" keyword in formal parameters for static methods in C# : https://stackoverflow.com/questions/846766/use-of-this-keyword-in-formal-parameters-for-static-methods-in-c-sharp

 

相关文章
|
2月前
|
JSON C# 数据格式
【Azure Function】C#独立工作模式下参数类型 ServiceBusReceivedMessage 无法正常工作
Cannot convert input parameter 'message' to type 'Azure.Messaging.ServiceBus.ServiceBusReceivedMessage' from type 'System.String'.
119 73
|
11天前
|
存储 XML 开发工具
【Azure Storage Account】利用App Service作为反向代理, 并使用.NET Storage Account SDK实现上传/下载操作
本文介绍了如何在Azure上使用App Service作为反向代理,以自定义域名访问Storage Account。主要内容包括: 1. **设置反向代理**:通过配置`applicationhost.xdt`和`web.config`文件,启用IIS代理功能并设置重写规则。 2. **验证访问**:测试原生URL和自定义域名的访问效果,确保两者均可正常访问Storage Account。 3. **.NET SDK连接**:使用共享访问签名(SAS URL)初始化BlobServiceClient对象,实现通过自定义域名访问存储服务。
|
2月前
|
开发框架 安全 .NET
【Azure Developer】.NET Aspire 项目本地调试遇 Grpc.Core.RpcException 异常( Error starting gRPC call ... )
Error starting gRPC call. HttpRequestException: The SSL connection could not be established, see inner exception. AuthenticationException: The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot
67 12
|
3月前
|
开发框架 搜索推荐 算法
一个包含了 50+ C#/.NET编程技巧实战练习教程
一个包含了 50+ C#/.NET编程技巧实战练习教程
153 18
|
3月前
|
缓存 算法 安全
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
100 12
|
3月前
|
开发框架 人工智能 .NET
C#/.NET/.NET Core拾遗补漏合集(24年12月更新)
C#/.NET/.NET Core拾遗补漏合集(24年12月更新)
|
3月前
|
开发框架 算法 .NET
C#/.NET/.NET Core技术前沿周刊 | 第 15 期(2024年11.25-11.30)
C#/.NET/.NET Core技术前沿周刊 | 第 15 期(2024年11.25-11.30)
|
3月前
|
开发框架 Cloud Native .NET
C#/.NET/.NET Core技术前沿周刊 | 第 16 期(2024年12.01-12.08)
C#/.NET/.NET Core技术前沿周刊 | 第 16 期(2024年12.01-12.08)
|
3月前
|
开发框架 监控 .NET
C#进阶-ASP.NET WebForms调用ASMX的WebService接口
通过本文的介绍,希望您能深入理解并掌握ASP.NET WebForms中调用ASMX WebService接口的方法和技巧,并在实际项目中灵活运用这些技术,提高开发效率和应用性能。
108 5