Send With Confidence
Partner with the email service trusted by developers and marketers for time-savings, scalability, and delivery expertise.
Time to read: 2 minutes
Partner with the email service trusted by developers and marketers for time-savings, scalability, and delivery expertise.
using System; | |
using System.Dynamic; | |
namespace Fluent | |
{ | |
class Client : DynamicObject | |
{ | |
public string UrlPath; | |
public Client(string urlPath = null) | |
{ | |
UrlPath = (urlPath != null) ? urlPath : null; | |
} | |
private Client BuildClient(string name = null) | |
{ | |
string endpoint; | |
if (name != null) | |
{ | |
endpoint = UrlPath + "/" + name; | |
} | |
else | |
{ | |
endpoint = UrlPath; | |
} | |
UrlPath = null; // Reset the current object's state before we return a new one | |
return new Client(endpoint); | |
} | |
public Client _(string name) | |
{ | |
return BuildClient(name); | |
} | |
public override bool TryGetMember(GetMemberBinder binder, out object result) | |
{ | |
result = BuildClient(binder.Name); | |
return true; | |
} | |
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) | |
{ | |
result = UrlPath; | |
return true; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
dynamic client = new Client(); | |
dynamic chain = client.hello.world; | |
Console.WriteLine(chain.UrlPath); | |
Console.ReadLine(); | |
dynamic new_chain = chain.thanks._("for").all.the.fish; | |
Console.WriteLine(new_chain.method()); | |
Console.ReadLine(); | |
} | |
} | |
} |