{"id":3596,"date":"2025-04-20T19:09:36","date_gmt":"2025-04-20T11:09:36","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3596"},"modified":"2025-04-20T19:09:36","modified_gmt":"2025-04-20T11:09:36","slug":"c-%e6%b3%9b%e5%9e%8b%e5%ae%9e%e6%88%98%e9%a1%b9%e7%9b%ae%e7%bb%93%e6%9e%84%e6%a8%a1%e6%9d%bf","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/20\/c-%e6%b3%9b%e5%9e%8b%e5%ae%9e%e6%88%98%e9%a1%b9%e7%9b%ae%e7%bb%93%e6%9e%84%e6%a8%a1%e6%9d%bf\/","title":{"rendered":"C# \u6cdb\u578b\u5b9e\u6218\u9879\u76ee\u7ed3\u6784\u6a21\u677f"},"content":{"rendered":"\n<p>\u4ee5\u4e0b\u662f\u6807\u51c6\u5316\u7684 <strong>C# \u6cdb\u578b\u5b9e\u6218\u9879\u76ee\u7ed3\u6784\u6a21\u677f<\/strong>\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u5728 Visual Studio \u4e2d\u521b\u5efa\u4e00\u4e2a <code>.NET Console App<\/code> \u9879\u76ee\uff0c\u5e76\u5c06\u4ee5\u4e0b\u7ed3\u6784\u4e0e\u4ee3\u7801\u7c98\u8d34\u5230\u76f8\u5e94\u6587\u4ef6\u4e2d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u9879\u76ee\u7ed3\u6784\u76ee\u5f55<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/GenericApp\n\u2502\n\u251c\u2500\u2500 Program.cs\n\u251c\u2500\u2500 Services\/\n\u2502   \u251c\u2500\u2500 IService.cs\n\u2502   \u2514\u2500\u2500 MyService.cs\n\u251c\u2500\u2500 Infrastructure\/\n\u2502   \u251c\u2500\u2500 SimpleContainer.cs\n\u2502   \u251c\u2500\u2500 Singleton.cs\n\u2502   \u2514\u2500\u2500 Factory.cs\n\u251c\u2500\u2500 Extensions\/\n\u2502   \u2514\u2500\u2500 EnumerableExtensions.cs<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. Program.cs<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>using GenericApp.Infrastructure;\nusing GenericApp.Services;\nusing GenericApp.Extensions;\n\nnamespace GenericApp\n{\n    class Program\n    {\n        static void Main()\n        {\n            \/\/ IoC \u5bb9\u5668\u6ce8\u518c\n            var container = new SimpleContainer();\n            container.Register&lt;IService, MyService&gt;();\n\n            var service = container.Resolve&lt;IService&gt;();\n            service.Run();\n\n            \/\/ \u6cdb\u578b\u5355\u4f8b\u4f7f\u7528\n            var logger = Singleton&lt;Logger&gt;.Instance;\n            logger.Log(\"\u5355\u4f8b\u65e5\u5fd7\u6d4b\u8bd5\");\n\n            \/\/ \u6cdb\u578b\u5de5\u5382\u4f7f\u7528\n            var factory = new Factory&lt;MyService&gt;();\n            var serviceFromFactory = factory.Create();\n            serviceFromFactory.Run();\n\n            \/\/ \u6cdb\u578b\u6269\u5c55\u65b9\u6cd5\u4f7f\u7528\n            var numbers = new&#91;] { 1, 2, 3, 4 };\n            var squares = numbers.Map(n =&gt; n * n);\n            Console.WriteLine(string.Join(\", \", squares));\n        }\n    }\n\n    public class Logger\n    {\n        public void Log(string message) =&gt; Console.WriteLine($\"&#91;LOG] {message}\");\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Services\/IService.cs<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace GenericApp.Services\n{\n    public interface IService\n    {\n        void Run();\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Services\/MyService.cs<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace GenericApp.Services\n{\n    public class MyService : IService\n    {\n        public void Run()\n        {\n            Console.WriteLine(\"MyService \u6b63\u5728\u8fd0\u884c...\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. Infrastructure\/SimpleContainer.cs<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\n\nnamespace GenericApp.Infrastructure\n{\n    public class SimpleContainer\n    {\n        private readonly Dictionary&lt;Type, Type&gt; _registrations = new();\n\n        public void Register&lt;TInterface, TImplementation&gt;() where TImplementation : TInterface\n        {\n            _registrations&#91;typeof(TInterface)] = typeof(TImplementation);\n        }\n\n        public TInterface Resolve&lt;TInterface&gt;()\n        {\n            var implType = _registrations&#91;typeof(TInterface)];\n            return (TInterface)Activator.CreateInstance(implType)!;\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. Infrastructure\/Singleton.cs<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\n\nnamespace GenericApp.Infrastructure\n{\n    public sealed class Singleton&lt;T&gt; where T : class, new()\n    {\n        private static readonly Lazy&lt;T&gt; _instance = new(() =&gt; new T());\n        public static T Instance =&gt; _instance.Value;\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. Infrastructure\/Factory.cs<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace GenericApp.Infrastructure\n{\n    public class Factory&lt;T&gt; where T : new()\n    {\n        public T Create() =&gt; new T();\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. Extensions\/EnumerableExtensions.cs<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\n\nnamespace GenericApp.Extensions\n{\n    public static class EnumerableExtensions\n    {\n        public static IEnumerable&lt;TResult&gt; Map&lt;TSource, TResult&gt;(\n            this IEnumerable&lt;TSource&gt; source,\n            Func&lt;TSource, TResult&gt; selector)\n        {\n            foreach (var item in source)\n                yield return selector(item);\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u7f16\u8bd1\u65b9\u5f0f<\/h2>\n\n\n\n<p>\u4f60\u53ea\u9700\u8981\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5728 Visual Studio \u6216 Rider \u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0 <code>.NET 6 \u6216 7<\/code> \u7684 Console App \u9879\u76ee<\/li>\n\n\n\n<li>\u6309\u4e0a\u9762\u7ed3\u6784\u65b0\u5efa\u6587\u4ef6\u5e76\u62f7\u8d1d\u4ee3\u7801\u8fdb\u53bb<\/li>\n\n\n\n<li>\u7f16\u8bd1\u8fd0\u884c\uff0c\u89c2\u5bdf\u63a7\u5236\u53f0\u8f93\u51fa\u6548\u679c<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662f\u6807\u51c6\u5316\u7684 C# \u6cdb\u578b\u5b9e\u6218\u9879\u76ee\u7ed3\u6784\u6a21\u677f\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u5728 Visual Studio \u4e2d\u521b\u5efa\u4e00\u4e2a .NET C [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3597,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3596","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3596"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3596\/revisions"}],"predecessor-version":[{"id":3598,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3596\/revisions\/3598"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3597"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}