Func and Action in .NET

Scenario:

Func and Action example in .NET

Solution:

  • Method with Func and Action
    1.  1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      private T Push(Func<HttpClient, HttpContent, HttpResponseMessage> request, Action<StreamWriter> serialize, Func<StreamReader, T> deserialize)
          {
              ..
      
                   using (var ms = new MemoryStream())
              {
                  string data;
      
                  using (var sw = new StreamWriter(ms, Encoding, 1024, true))
                  {
                      serialize(sw);
                  }
      
                  ms.Position = 0;
      
                  using (var sr = new StreamReader(ms, Encoding, true, 1024, true))
                  {
                      data = sr.ReadToEnd();
                  }
      
              ...
      
              response = request?.Invoke(httpClient, httpContent);
      
              ...
      
               ..
      
                          var info = deserialize.Invoke(streamReader);
      
                          return info;
                      }
                  }
              }
  • Call
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
            private async Task Call()
            {
                await client.Push(
                                sw => serializer.WriteObject(sw.BaseStream, inputData),
                                sr =>
                                {
                                    var response =
                                        serializer.ReadObject(sr.BaseStream) as Response;
    
                                    return response;
                                });
            }

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...