using System.Collections.Generic;
using static System.Console;
public class Dynamic : DynamicObject {
Dictionary<string, object> dictionary = new Dictionary<string, object>();
public override bool TryGetMember(GetMemberBinder binder, out object result) => dictionary.TryGetValue(binder.Name, out result);
public override bool TrySetMember(SetMemberBinder binder, object value) {
dictionary[binder.Name] = value;
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) {
if (dictionary.ContainsKey(binder.Name)) {
((Action)dictionary[binder.Name]).Invoke();
result = "Método dinâmico executou";
result = (typeof(Dictionary<string, object>)).InvokeMember(binder.Name, BindingFlags.InvokeMethod, null, dictionary, args);
result = "Resultado falho";
WriteLine($"Executando método \"{binder.Name}\".");
foreach (var pair in dictionary) {
if (!(pair.Value is Delegate)) {
WriteLine(pair.Key + " " + pair.Value);
if (dictionary.Count == 0) {
WriteLine("Sem membros para listar");
public static void Main(string[] args) {
dynamic din = new Dynamic();
din.Action = new Action(() => WriteLine("Action Existe"));