Skip to content

Instantly share code, notes, and snippets.

@sakapon
Last active June 22, 2017 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakapon/3d0726245f7319d26913045637a6e6cd to your computer and use it in GitHub Desktop.
Save sakapon/3d0726245f7319d26913045637a6e6cd to your computer and use it in GitHub Desktop.
ProxySample/DynamicHttpConsole/DynamicHttpProxy.cs
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);
}
}
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