{"id":22493422,"url":"https://github.com/ufcpp/delegateinterface","last_synced_at":"2025-03-27T20:45:18.765Z","repository":{"id":39637118,"uuid":"496977880","full_name":"ufcpp/DelegateInterface","owner":"ufcpp","description":null,"archived":false,"fork":false,"pushed_at":"2023-01-20T07:48:46.000Z","size":47,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-01T23:17:49.670Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ufcpp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-05-27T11:52:32.000Z","updated_at":"2024-06-05T15:48:46.000Z","dependencies_parsed_at":"2023-02-12T00:30:22.205Z","dependency_job_id":null,"html_url":"https://github.com/ufcpp/DelegateInterface","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ufcpp%2FDelegateInterface","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ufcpp%2FDelegateInterface/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ufcpp%2FDelegateInterface/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ufcpp%2FDelegateInterface/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ufcpp","download_url":"https://codeload.github.com/ufcpp/DelegateInterface/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245924499,"owners_count":20694728,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-06T18:39:28.862Z","updated_at":"2025-03-27T20:45:18.741Z","avatar_url":"https://github.com/ufcpp.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DelegateInterface\n\n## Library usage:\n\n```cs\npublic interface IA\n{\n    int A(int x);\n    int B(string x);\n    (int x, int y) C(TimeSpan x);\n    P D(P x, P y);\n}\n```\n\n```cs\nusing DelegateInterface;\n\n// Create a proxy instance.\nvar a = Cache\u003cIA\u003e.CreateInstance();\n\n// Add delegates to the proxy.\nvar d = (IDelegateInterface)a;\nint factor = 10;\nd.Methods[\"A\"] = (int x) =\u003e factor * x;\nd.Methods[\"B\"] = static (string x) =\u003e x.Length;\nd.Methods[\"C\"] = new C(1, 2).M;\nd.Methods[\"D\"] = static P (P x, P y) =\u003e new(x.X + y.X, x.Y + y.Y);\n\n// Invoke interface methods.\nConsole.WriteLine(a.A(3));\nConsole.WriteLine(a.B(\"abc\"));\nConsole.WriteLine(a.C(TimeSpan.FromSeconds(9999)));\nConsole.WriteLine(a.D(new(2, 3), new(5, 7)));\n```\n\n```\n30\n3\n(2, 46)\nP { X = 7, Y = 10 }\n```\n\n## How it works\n\n`DelegateInterfaceTypeBuilder` class build a proxy class by using `System.Reflection.Emit`.\n\nIf you have a interface:\n\n```cs\npublic interface IA\n{\n    void M1();\n    string M2();\n    void M3(TimeSpan x);\n    int M4(int x, int y);\n}\n```\n\nThe builder class dynamically create a proxy class:\n\n```cs\nclass IA_Proxy : IDynamicInterface, IA\n{\n    public IDictionary\u003cstring, Delegate\u003e Methods { get; }\n    public void M1() =\u003e Methods[\"M1\"].DynamicInvoke();\n    public string M2() =\u003e (string)Methods[\"M2\"].DynamicInvoke();\n    public void M3(TimeSpan x) =\u003e Methods[\"M3\"].DynamicInvoke(x);\n    public int M4(int x, int y) =\u003e (int)Methods[\"M4\"].DynamicInvoke(x, y);\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fufcpp%2Fdelegateinterface","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fufcpp%2Fdelegateinterface","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fufcpp%2Fdelegateinterface/lists"}