c# - 为什么这个扩展方法没有初始化返回的对象?

标签 c# generics extension-methods

我编写了一个通用扩展帮助器方法来初始化 SoapHeader 的一些参数,但它不会更新返回的对象。

我错过了什么?

using System.Web.Services.Protocols;

    public class Header: SoapHeader {}

    public class WS {
      public Header securityHeader {
        get;
        set;
      }

    }
    public static class SecurityHeaderExtensions {

      public static T GetSecurityHeader < T > (this T header, string actor, string role) where T: SoapHeader, new() {

        T result = new T() {
          Actor = actor, Role = role
        };

        Console.WriteLine("Actor: " + actor); //prints actor
        Console.WriteLine("Actor: " + result.Actor); //prints blank
        return result;

      }
    }

    void Main() {
      var ws = new WS();
      ws.securityHeader = ws.securityHeader.GetSecurityHeader("actor", null);

    }

最佳答案

看起来ActorRole在内部是同一个字段。

将其设置为 actor,然后将其设置为 null 意味着它是 null

The recipient of that data, known as the SOAP Role in version 1.2 of the SOAP specification and the SOAP Actor in version 1.1

关于c# - 为什么这个扩展方法没有初始化返回的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30897632/

相关文章:

C#图表,如何获取最大x轴值?

c# - MVVM 轻型从 ViewModel 发送消息到 View

java - 一旦方法返回,传递给方法并反序列化的列表将变为空

java - 为什么迭代接收到的通用 HashMap 只能通过增强的 for 循环来完成?

c# - 参数依赖查找中的混淆?

c# - ValueTypes 会导致 GC 吗?

C# 如何单击 IList 中的 IWebelement?

vb.net - 如何将类型传递给泛型类型?

c# - 在C#中使用反射修改循环中的字段,扩展方法

c# - C# 扩展方法是否影响当前实例或返回相同类型的新对象?