Last active
June 22, 2017 07:41
-
-
Save sakapon/3d0726245f7319d26913045637a6e6cd to your computer and use it in GitHub Desktop.
ProxySample/DynamicHttpConsole/DynamicHttpProxy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Dynamic; | |
using System.Linq; | |
namespace DynamicHttpConsole | |
{ | |
public class DynamicHttpProxy : DynamicObject | |
{ | |
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) | |
{ | |
if (binder.Name == "Get") | |
{ | |
result = Get(binder.CallInfo.ArgumentNames, args); | |
return true; | |
} | |
return base.TryInvokeMember(binder, args, out result); | |
} | |
static string Get(ICollection<string> argNames, object[] args) | |
{ | |
var query = argNames | |
.Zip(args.Skip(1), (n, v) => new { n, v }) | |
.ToDictionary(_ => _.n, _ => _.v); | |
return HttpHelper.Get((string)args[0], query); | |
} | |
public string Get(string uri, object query = null) => HttpHelper.Get(uri, query); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace DynamicHttpConsole | |
{ | |
class Program | |
{ | |
// http://zip.cgis.biz/ | |
const string Uri_Cgis_Xml = "http://zip.cgis.biz/xml/zip.php"; | |
static void Main(string[] args) | |
{ | |
dynamic http = new DynamicHttpProxy(); | |
// No query | |
Console.WriteLine(http.Get(Uri_Cgis_Xml)); | |
// Named arguments | |
Console.WriteLine(http.Get(Uri_Cgis_Xml, zn: "1510052")); | |
Console.WriteLine(http.Get(Uri_Cgis_Xml, zn: "402", ver: 1)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment