{"id":13431153,"url":"https://github.com/tonerdo/pose","last_synced_at":"2025-04-14T23:19:24.700Z","repository":{"id":40522835,"uuid":"98982258","full_name":"tonerdo/pose","owner":"tonerdo","description":"Replace any .NET method (including static and non-virtual) with a delegate","archived":false,"fork":false,"pushed_at":"2022-09-23T12:14:55.000Z","size":233,"stargazers_count":1091,"open_issues_count":67,"forks_count":74,"subscribers_count":50,"default_branch":"master","last_synced_at":"2025-04-14T23:19:11.702Z","etag":null,"topics":["csharp","dotnet","dotnet-core","isolation-framework","mocking","testing","testing-framework"],"latest_commit_sha":null,"homepage":"","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/tonerdo.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":"2017-08-01T09:09:59.000Z","updated_at":"2025-04-10T17:08:19.000Z","dependencies_parsed_at":"2022-06-29T19:20:06.609Z","dependency_job_id":null,"html_url":"https://github.com/tonerdo/pose","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tonerdo%2Fpose","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tonerdo%2Fpose/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tonerdo%2Fpose/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tonerdo%2Fpose/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tonerdo","download_url":"https://codeload.github.com/tonerdo/pose/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248975330,"owners_count":21192210,"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":["csharp","dotnet","dotnet-core","isolation-framework","mocking","testing","testing-framework"],"created_at":"2024-07-31T02:01:00.920Z","updated_at":"2025-04-14T23:19:24.684Z","avatar_url":"https://github.com/tonerdo.png","language":"C#","readme":"[![Windows build status](https://ci.appveyor.com/api/projects/status/github/tonerdo/pose?branch=master\u0026svg=true)](https://ci.appveyor.com/project/tonerdo/pose)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)\n[![NuGet version](https://badge.fury.io/nu/Pose.svg)](https://www.nuget.org/packages/Pose)\n# Pose\n\nPose allows you to replace any .NET method (including static and non-virtual) with a delegate. It is similar to [Microsoft Fakes](https://msdn.microsoft.com/en-us/library/hh549175.aspx) but unlike it Pose is implemented _entirely_ in managed code (Reflection Emit API). Everything occurs at runtime and in-memory, no unmanaged Profiling APIs and no file system pollution with re-written assemblies.\n\nPose is cross platform and runs anywhere .NET is supported. It targets .NET Standard 2.0 so it can be used across .NET platforms including .NET Framework, .NET Core, Mono and Xamarin. See version compatibility table [here](https://docs.microsoft.com/en-us/dotnet/standard/net-standard).\n\n## Installation\n\nAvailable on [NuGet](https://www.nuget.org/packages/Pose/)\n\nVisual Studio:\n\n```powershell\nPM\u003e Install-Package Pose\n```\n\n.NET Core CLI:\n\n```bash\ndotnet add package Pose\n```\n\n## Usage\n\nPose gives you the ability to create shims by way of the `Shim` class. Shims are basically objects that let you specify the method you want to replace as well as the replacement delegate. Delegate signatures (arguments and return type) must match that of the methods they replace. The `Is` class is used to create instances of a type and all code you want to apply your shims to is isolated using the `PoseContext` class.\n\n\n### Shim static method\n\n```csharp\nusing Pose;\n\nShim consoleShim = Shim.Replace(() =\u003e Console.WriteLine(Is.A\u003cstring\u003e())).With(\n    delegate (string s) { Console.WriteLine(\"Hijacked: {0}\", s); });\n```\n\n### Shim static property getter\n\n```csharp\nusing Pose;\n\nShim dateTimeShim = Shim.Replace(() =\u003e DateTime.Now).With(() =\u003e new DateTime(2004, 4, 4));\n```\n\n### Shim static property setter\n\n```csharp\nusing Pose;\n\nShim setterShim = Shim.Replace(() =\u003e Console.Title, true).With((string title) =\u003e { Console.Title = \"My Title\"; });\n```\n\n### Shim instance property getter\n\n```csharp\nusing Pose;\n\nclass MyClass\n{\n    public int MyProperty { get; set; }\n    public void DoSomething() =\u003e Console.WriteLine(\"doing someting\");\n}\n\nShim classPropShim = Shim.Replace(() =\u003e Is.A\u003cMyClass\u003e().MyProperty).With((MyClass @this) =\u003e 100);\n```\n\n### Shim instance property setter\n\n```csharp\nusing Pose;\n\nShim classPropShim = Shim.Replace(() =\u003e Is.A\u003cMyClass\u003e().MyProperty, true).With((MyClass @this, int prop) =\u003e { @this.MyProperty = prop * 10; });\n```\n\n### Shim constructor\n\n```csharp\nusing Pose;\n\nShim ctorShim = Shim.Replace(() =\u003e new MyClass()).With(() =\u003e new MyClass() { MyProperty = 10 });\n```\n\n### Shim instance method of a Reference Type\n\n```csharp\nusing Pose;\n\nShim classShim = Shim.Replace(() =\u003e Is.A\u003cMyClass\u003e().DoSomething()).With(\n    delegate (MyClass @this) { Console.WriteLine(\"doing someting else\"); });\n```\n\n_Note: The first argument to an instance method replacement delegate is always the instance of the class_\n\n### Shim method of specific instance of a Reference Type\n\n```csharp\nusing Pose;\n\nMyClass myClass = new MyClass();\nShim myClassShim = Shim.Replace(() =\u003e myClass.DoSomething()).With(\n    delegate (MyClass @this) { Console.WriteLine(\"doing someting else with myClass\"); });\n```\n\n### Shim instance method of a Value Type\n\n```csharp\nusing Pose;\n\nShim structShim = Shim.Replace(() =\u003e Is.A\u003cMyStruct\u003e().DoSomething()).With(\n    delegate (ref MyStruct @this) { Console.WriteLine(\"doing someting else\"); });\n```\n\n_Note: You cannot shim methods on specific instances of Value Types_\n\n### Isolating your code\n\n```csharp\n// This block executes immediately\nPoseContext.Isolate(() =\u003e\n{\n    // All code that executes within this block\n    // is isolated and shimmed methods are replaced\n\n    // Outputs \"Hijacked: Hello World!\"\n    Console.WriteLine(\"Hello World!\");\n\n    // Outputs \"4/4/04 12:00:00 AM\"\n    Console.WriteLine(DateTime.Now);\n\n    // Outputs \"doing someting else\"\n    new MyClass().DoSomething();\n\n    // Outputs \"doing someting else with myClass\"\n    myClass.DoSomething();\n\n}, consoleShim, dateTimeShim, classPropShim, classShim, myClassShim, structShim);\n```\n\n## Caveats \u0026 Limitations\n\n* **Breakpoints** - At this time any breakpoints set anywhere in the isolated code and its execution path will not be hit. However, breakpoints set within a shim replacement delegate are hit.\n* **Exceptions** - At this time all unhandled exceptions thrown in isolated code and its execution path are always wrapped in `System.Reflection.TargetInvocationException`.\n\n## Roadmap\n\n* **Performance Improvements** - Pose can be used outside the context of unit tests. Better performance would make it suitable for use in production code, possibly to override legacy functionality.\n* **Exceptions Stack Trace** - Currently when exceptions are thrown in your own code under isolation, the supplied exception stack trace is quite confusing. Providing an undiluted exception stack trace is needed.\n\n## Issues \u0026 Contributions\n\nIf you find a bug or have a feature request, please report them at this repository's issues section. Contributions are highly welcome, however, except for very small changes kindly file an issue and let's have a discussion before you open a pull request.\n\n## License\n\nThis project is licensed under the MIT license. See the [LICENSE](LICENSE) file for more info.\n","funding_links":[],"categories":["Frameworks, Libraries and Tools","框架, 库和工具","testing","csharp"],"sub_categories":["Misc","大杂烩"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftonerdo%2Fpose","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftonerdo%2Fpose","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftonerdo%2Fpose/lists"}