{"id":10,"date":"2016-09-24T00:03:44","date_gmt":"2016-09-23T15:03:44","guid":{"rendered":"http:\/\/reasty.net\/?p=10"},"modified":"2016-09-24T00:03:44","modified_gmt":"2016-09-23T15:03:44","slug":"%ed%99%95%ec%9e%a5-%eb%a9%94%ec%84%9c%eb%93%9c-extension-method","status":"publish","type":"post","link":"https:\/\/reasty.net\/?p=10","title":{"rendered":"\ud655\uc7a5 \uba54\uc11c\ub4dc (Extension method)"},"content":{"rendered":"<p>\ud655\uc7a5 \uba54\uc11c\ub4dc\ub294 C# 3.0\ubd80\ud130 \ucd94\uac00\ub418\uc5c8\uace0, \ub300\ud45c\uc801\uc778 \uc608\ub85c\ub294 Linq\uac00 \uc788\ub2e4. Linq\uc758 \ub300\ubd80\ubd84\uc758(\uac70\uc758 \ubaa8\ub4e0) \uba54\uc11c\ub4dc\ub294 IEnumerable&lt;T&gt; \uc778\ud130\ud398\uc774\uc2a4\uc758 \ud655\uc7a5 \uba54\uc11c\ub4dc\ub85c \uad6c\ud604\ub418\uc5b4 \uc788\ub2e4.<\/p>\n<h1>1. \uad6c\ud604<\/h1>\n<p>\uae30\ubcf8\uc801\uc778 \uad6c\ud604\ubc29\ubc95\uc740 class\ub97c <strong>public static<\/strong>\uc73c\ub85c \uc120\uc5b8\ud558\uace0, \uba54\uc11c\ub4dc \uc5ed\uc2dc public static\uc73c\ub85c \uc120\uc5b8\ud558\uba70, \ud655\uc7a5\ud558\uace0\uc790 \ud558\ub294 class Type \uc55e\uc5d0 <strong>this \ud0a4\uc6cc\ub4dc<\/strong>\ub97c \ubd99\uc5ec\uc11c\uc11c \uc120\uc5b8\ud55c\ub2e4. \uad6c\ud604\ubd80\ub294 \uc77c\ubc18 \uba54\uc11c\ub4dc\uc640 \uac19\ub2e4.<br \/>\n\uc544\ub798\ub294 String\uc5d0\uc11c \ub2e8\uc5b4\uc758 \uac2f\uc218\ub97c \ubc18\ud658\ud574\uc8fc\ub294 \uac04\ub2e8\ud55c \uc608\uc81c\ub2e4.<\/p>\n<pre title=\"\ud655\uc7a5 \uba54\uc11c\ub4dc \uad6c\ud604 \uc608\uc81c\" class=\"theme:vs2012 lang:c# mark:5,7 decode:true \">using System;\n\nnamespace ExtensionMethods\n{\n    public static class MyExtensions\n    {\n        public static int WordCount(this String str)\n        {\n            return str.Split(new char[] { '' '', ''.'', ''?'' }, \n                             StringSplitOptions.RemoveEmptyEntries).Length;\n        }\n    }   \n}<\/pre>\n<h1>2. \uc0ac\uc6a9<\/h1>\n<p>\ud655\uc7a5 \uba54\uc11c\ub4dc\uc758 \uc0ac\uc6a9\ubc29\ubc95\uc740 \ud655\uc7a5 \uba54\uc11c\ub4dc\uac00 \uad6c\ud604\ub41c class\uc758 <strong>Namespace\ub97c using \uc808\uc5d0 \ucd94\uac00<\/strong>\ud558\uc5ec, \uc77c\ubc18 \uba64\ubc84 \uba54\uc11c\ub4dc\uc640 \ub3d9\uc77c\ud558\uac8c \uc0ac\uc6a9\uac00\ub2a5\ud558\ub2e4. \ud574\ub2f9 class\uc758\u00a0intellisence\uc5d0\uc11c\ub294 (Extension) \uc774\ub77c\ub294 \ud0a4\uc6cc\ub4dc\uac00 \ubd99\ub294\uac70 \uc774\uc678\uc5d4 \uba64\ubc84 \uba54\uc11c\ub4dc\uc640 \ub3d9\uc77c\ud558\ub2e4. \uc544\ub798\ub294 \uc704\uc5d0\uc11c \uad6c\ud604\ud55c \ud655\uc7a5 \uba54\uc11c\ub4dc\uc758 \uc0ac\uc6a9 \uc608\uc81c\uc774\ub2e4.<\/p>\n<pre title=\"\ud655\uc7a5 \uba54\uc11c\ub4dc \uc0ac\uc6a9 \uc608\uc81c\" class=\"theme:vs2012 lang:c# mark:11,2 decode:true \">using System;\nusing ExtensionMethods;\n\nnamespace ConsoleApplication1\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            string s = \"Hello Extension Methods\";\n            int i = s.WordCount();\n\n            Console.WriteLine(\"WordCount = {0}\", i);\n        }\n    }\n}<\/pre>\n<h1>3. \uc8fc\uc758\uc0ac\ud56d<\/h1>\n<p>\ud655\uc7a5\ud560 class\uc758 \uba64\ubc84 \uba54\uc11c\ub4dc\uc5d0 \ub3d9\uc77c\ud55c\u00a0\uad6c\uc870\uc758\u00a0\uba54\uc11c\ub4dc\uac00 \uc788\uc744 \uacbd\uc6b0 \ud638\ucd9c \ub418\uc9c0 \uc54a\ub294\ub2e4. \ub610\ud55c Namespace \ubc94\uc704\ub85c \ud655\uc7a5 \uba54\uc11c\ub4dc\ub97c \uac00\uc838\uc624\uac8c \ub418\ubbc0\ub85c \ud638\ucd9c\ud558\ub294 \ucf54\ub4dc\uc5d0\uc11c\ub294 \ubc18\ub4dc\uc2dc \ud655\uc7a5 \uba54\uc11c\ub4dc\uac00 \uad6c\ud604\ub41c class\uc758 Namespace\ub97c using \uc808\uc5d0 \ucd94\uac00\ud558\uc5ec\uc57c \ud55c\ub2e4.<\/p>\n<p><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/bb383977.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">Microsoft C# \ud504\ub85c\uadf8\ub798\ubc0d \uac00\uc774\ub4dc<\/a>\uc5d0\uc11c\ub294 \ubc18\ub4dc\uc2dc \ud544\uc694\ud55c \uacbd\uc6b0\uac00 \uc544\ub2c8\uba74 \ud655\uc7a5 \uba54\uc11c\ub4dc \ub300\uc2e0\uc5d0 class\uc758 \uc0c1\uc18d\uc73c\ub85c \uad6c\ud604\ud558\ub77c\uace0 \uac00\uc774\ub4dc\ud55c\ub2e4. \uae30\uc874 class\uc5d0 \ud655\uc7a5 \uba54\uc11c\ub4dc\uc640 \ub3d9\uc77c\ud55c \uad6c\uc870\uc758 \uba64\ubc84 \uba54\uc11c\ub4dc\uac00 \ucd94\uac00\ub418\uba74 \ud655\uc7a5 \uba54\uc11c\ub4dc\uac00 \ud638\ucd9c\ub418\uc9c0 \uc54a\uace0 \uba64\ubc84 \uba54\uc11c\ub4dc\uac00 \ud638\ucd9c\ub418\uae30 \ub54c\ubb38\uc774\ub2e4. \uc608\uc81c\ub294 \ub9c1\ud06c\ub97c \ucc38\uc870\ud558\uae30 \ubc14\ub780\ub2e4.<\/p>\n<h1>4. \uc758\uacac<\/h1>\n<p>\ub098\ub294 \uc8fc\ub85c \uae30\uc874 \ud074\ub798\uc2a4\uc758 \uad6c\uc870\ub97c \ubb34\ub108\ub728\ub9ac\uc9c0 \uc54a\uc73c\uba74\uc11c \ud544\uc694\ud55c \uae30\ub2a5\uc744 \ud655\uc7a5\ud558\uace0 \uc2f6\uc744\ub54c \uc0ac\uc6a9\ud55c\ub2e4. Util\uad00\ub828 \uba54\uc11c\ub4dc\ub4e4\uc744 \ud655\uc7a5 \uba54\uc11c\ub4dc\ub85c \uad6c\ud604\ud558\uace4 \ud55c\ub2e4. Namespace\ub97c \ucd94\uac00\ud558\uba74 \uac04\ub2e8\ud558\uac8c \uc0ac\uc6a9\uac00\ub2a5\ud558\uae30 \ub54c\ubb38\uc774\ub2e4. class\uc758 \uc0c1\uc18d\ub3c4 \ud558\ub098\uc758 \ubc29\ubc95\uc774\uc9c0\ub9cc,\u00a0\uc0c1\uc18d\ub41c class\uac00 \uc99d\uac00\ud568\uc5d0 \ub530\ub77c \uac01 class\uac04\uc758 \uacb0\ud569\ub3c4\ub294 \uc99d\uac00\ud558\uae30 \ub54c\ubb38\uc5d0 \ub418\ub3c4\ub85d \uc0c1\uc18d\uc744 \ud53c\ud558\uace0, \ud655\uc7a5 \uba54\uc11c\ub4dc\uac00 \ud3ec\ud568\ub41c class\ub97c \ubcc4\ub3c4\uc758 \ub77c\uc774\ube0c\ub7ec\ub9ac\ub85c \uad00\ub9ac\ud558\uc5ec \ucf54\ub4dc\uc758 \uc7ac\uc0ac\uc6a9\uc728\uc744 \ub192\uc774\ub294 \ud3b8\uc774\ub2e4.<\/p>\n<p>\uc544\ub798\ub294 \ub0b4\uac00 \uc790\uc8fc\uc0ac\uc6a9\ud558\ub294 \ud655\uc7a5 \uba54\uc11c\ub4dc\uc758 \uad6c\ud604\uc774\ub2e4. \ud544\uc694\ud560 \ub54c\ub9c8\ub2e4 \uc5ec\ub7ec \ud504\ub85c\uc81d\ud2b8\uc5d0 \uc0ac\uc6a9\ud558\uace0\uc790 \uacc4\uc18d \uc5c5\ub370\uc774\ud2b8\ud560 \uacc4\ud68d\uc774\ub2e4.<\/p>\n<pre title=\"String Extension Methods\" class=\"theme:vs2012 lang:c# decode:true\">namespace System\n{\n    public static class StringExtensions\n    {\n        \n    }\n\n    public static class ArrayExtensions\n    {\n        public static T[] CopySlice&lt;T&gt;(this T[] source, int index, int length, bool padToLength = false)\n        {\n            int n = length;\n            T[] slice = null;\n\n            if (source.Length &lt; index + length)\n            {\n                n = source.Length - index;\n                if (padToLength)\n                {\n                    slice = new T[length];\n                }\n            }\n\n            if (slice == null) slice = new T[n];\n            Array.Copy(source, index, slice, 0, n);\n            return slice;\n        }\n\n        public static IEnumerable&lt;T[]&gt; Slices&lt;T&gt;(this T[] source, int count, bool padToLength = false)\n        {\n            for (var i = 0; i &lt; source.Length; i += count)\n                yield return source.CopySlice(i, count, padToLength);\n        }\n    }\n\n    public static class ByteArrayExtensions\n    {\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \uac01 byte\ub97c Hex\ub97c \ub098\ud0c0\ub0b4\ub294 \ubb38\uc790\uc5f4\ub85c \ubc18\ud658\ud569\ub2c8\ub2e4.\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;typeparam name=\"T\"&gt;&lt;\/typeparam&gt;\n        \/\/\/ &lt;param name=\"bytes\"&gt;&lt;\/param&gt;\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\n        public static string ToHexString(this IEnumerable&lt;byte&gt; bytes)\n        {\n            var hexString = new StringBuilder();\n            foreach (var byteFromHash in bytes)\n            {\n                hexString.AppendFormat(\"{0:x2}\", byteFromHash);\n            }\n\n            return hexString.ToString();\n        }\n    }\n\n    public static class DataRowExtensions\n    {\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ DbNull\uc77c \uacbd\uc6b0 defaultValue, \uc544\ub2d0 \uacbd\uc6b0 \uceec\ub7fc\uc758 \uac12\uc744 \uac00\uc838\uc628\ub2e4.\n        \/\/\/ SQLite\uc758 INTEGER \ud0c0\uc785\uc758 \uacbd\uc6b0 \ubc18\ub4dc\uc2dc Int64\ub85c \uac00\uc838\uc640\uc57c \ud55c\ub2e4.\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;typeparam name=\"T\"&gt;&lt;\/typeparam&gt;\n        \/\/\/ &lt;param name=\"dataRow\"&gt;&lt;\/param&gt;\n        \/\/\/ &lt;param name=\"columnName\"&gt;&lt;\/param&gt;\n        \/\/\/ &lt;param name=\"defaultValue\"&gt;&lt;\/param&gt;\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\n        public static T GetFieldValue&lt;T&gt;(this DataRow dataRow, string columnName, T defaultValue)\n        {\n            if (dataRow.Table.Columns.Contains(columnName) != true || dataRow.IsNull(columnName))\n            {\n                return defaultValue;\n            }\n            else\n            {\n                return dataRow.Field&lt;T&gt;(columnName);\n            }\n        }\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ DbNull\uc77c \uacbd\uc6b0 T\uc758 \uae30\ubcf8\uac12, \uc544\ub2d0 \uacbd\uc6b0 \uceec\ub7fc\uc758 \uac12\uc744 \uac00\uc838\uc628\ub2e4.\n        \/\/\/ SQLite\uc758 INTEGER \ud0c0\uc785\uc758 \uacbd\uc6b0 \ubc18\ub4dc\uc2dc Int64\ub85c \uac00\uc838\uc640\uc57c \ud55c\ub2e4.\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;typeparam name=\"T\"&gt;&lt;\/typeparam&gt;\n        \/\/\/ &lt;param name=\"dataRow\"&gt;&lt;\/param&gt;\n        \/\/\/ &lt;param name=\"columnName\"&gt;&lt;\/param&gt;\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\n        public static T GetFieldValue&lt;T&gt;(this DataRow dataRow, string columnName)\n        {\n            return dataRow.GetFieldValue&lt;T&gt;(columnName, default(T));\n        }\n\n        public static T Cast&lt;T&gt;(this DataRow tableRow) where T : new()\n        {\n            Type t = typeof(T);\n            T returnObject = new T();\n\n            foreach (DataColumn col in tableRow.Table.Columns)\n            {\n                string colName = col.ColumnName;\n                PropertyInfo pInfo = t.GetProperty(colName.ToLower(), BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);\n                if (pInfo == null)\n                    continue;\n                MethodInfo mInfo = pInfo.GetSetMethod();\n                if (pInfo != null &amp;&amp; mInfo != null)\n                {\n                    object val = tableRow[colName];\n\n                    bool IsNullable = (Nullable.GetUnderlyingType(pInfo.PropertyType) != null);\n                    if (IsNullable)\n                    {\n                        if (val is System.DBNull)\n                        {\n                            val = null;\n                        }\n                        else\n                        {\n                            val = Convert.ChangeType(val, Nullable.GetUnderlyingType(pInfo.PropertyType));\n                        }\n                    }\n                    else\n                    {\n                        val = Convert.ChangeType(val, pInfo.PropertyType);\n                    }\n                    pInfo.SetValue(returnObject, val, null);\n                }\n            }\n\n            return returnObject;\n        }\n    }\n\n    public static class DataTableExtensions\n    {\n        public static IList&lt;T&gt; ToList&lt;T&gt;(this DataTable table) where T : new()\n        {\n            IList&lt;PropertyInfo&gt; properties = typeof(T).GetProperties().ToList();\n            IList&lt;T&gt; result = new List&lt;T&gt;();\n\n            foreach (var row in table.Rows)\n            {\n                var item = CreateItemFromRow&lt;T&gt;((DataRow)row, properties);\n                result.Add(item);\n            }\n\n            return result;\n        }\n\n        public static IList&lt;T&gt; ToList&lt;T&gt;(this DataTable table, Dictionary&lt;string, string&gt; mappings) where T : new()\n        {\n            IList&lt;PropertyInfo&gt; properties = typeof(T).GetProperties().ToList();\n            IList&lt;T&gt; result = new List&lt;T&gt;();\n\n            foreach (var row in table.Rows)\n            {\n                var item = CreateItemFromRow&lt;T&gt;((DataRow)row, properties, mappings);\n                result.Add(item);\n            }\n\n            return result;\n        }\n\n        private static T CreateItemFromRow&lt;T&gt;(DataRow row, IList&lt;PropertyInfo&gt; properties) where T : new()\n        {\n            T item = new T();\n            foreach (var property in properties)\n            {\n                property.SetValue(item, row[property.Name], null);\n            }\n            return item;\n        }\n\n        private static T CreateItemFromRow&lt;T&gt;(DataRow row, IList&lt;PropertyInfo&gt; properties, Dictionary&lt;string, string&gt; mappings) where T : new()\n        {\n            T item = new T();\n            foreach (var property in properties)\n            {\n                if (mappings.ContainsKey(property.Name) &amp;&amp; String.IsNullOrEmpty(mappings[property.Name]) != true)\n                    property.SetValue(item, row[mappings[property.Name]], null);\n            }\n            return item;\n        }\n    }\n\n    public static class ICollectionExtensions\n    {\n        public static void AddIfNotExist&lt;T&gt;(this ICollection&lt;T&gt; collection, T item)\n        {\n            if (collection.Contains(item) != true)\n            {\n                collection.Add(item);\n            }\n        }\n\n        public static void AddRange&lt;T&gt;(this ICollection&lt;T&gt; collection, IEnumerable&lt;T&gt; items)\n        {\n            foreach (T item in items)\n            {\n                collection.Add(item);\n            }\n        }\n\n        public static void AddRangeIfNotExist&lt;T&gt;(this ICollection&lt;T&gt; collection, IEnumerable&lt;T&gt; items)\n        {\n            foreach (T item in items)\n            {\n                collection.AddIfNotExist(item);\n            }\n        }\n\n        public static bool RemoveRange&lt;T&gt;(this ICollection&lt;T&gt; collection, IEnumerable&lt;T&gt; items)\n        {\n            bool result = true;\n            foreach (T item in items)\n            {\n                result &amp;= collection.Remove(item);\n            }\n\n            return result;\n        }\n    }\n}<\/pre>\n<h1>5. \ucc38\uace0<\/h1>\n<p><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/bb383977.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/msdn.microsoft.com\/en-us\/library\/bb383977.aspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud655\uc7a5 \uba54\uc11c\ub4dc\ub294 C# 3.0\ubd80\ud130 \ucd94\uac00\ub418\uc5c8\uace0, \ub300\ud45c\uc801\uc778 \uc608\ub85c\ub294 Linq\uac00 \uc788\ub2e4. Linq\uc758 \ub300\ubd80\ubd84\uc758(\uac70\uc758 \ubaa8\ub4e0) \uba54\uc11c\ub4dc\ub294 IEnumerable&lt;T&gt; \uc778\ud130\ud398\uc774\uc2a4\uc758 \ud655\uc7a5 \uba54\uc11c\ub4dc\ub85c \uad6c\ud604\ub418\uc5b4 \uc788\ub2e4. 1. \uad6c\ud604 \uae30\ubcf8\uc801\uc778 \uad6c\ud604\ubc29\ubc95\uc740 class\ub97c public static\uc73c\ub85c \uc120\uc5b8\ud558\uace0, \uba54\uc11c\ub4dc \uc5ed\uc2dc public static\uc73c\ub85c \uc120\uc5b8\ud558\uba70, \ud655\uc7a5\ud558\uace0\uc790 \ud558\ub294 class Type \uc55e\uc5d0 this \ud0a4\uc6cc\ub4dc\ub97c \ubd99\uc5ec\uc11c\uc11c \uc120\uc5b8\ud55c\ub2e4. \uad6c\ud604\ubd80\ub294 \uc77c\ubc18 \uba54\uc11c\ub4dc\uc640 \uac19\ub2e4. \uc544\ub798\ub294 String\uc5d0\uc11c \ub2e8\uc5b4\uc758 \uac2f\uc218\ub97c \ubc18\ud658\ud574\uc8fc\ub294 \uac04\ub2e8\ud55c \uc608\uc81c\ub2e4. using System; namespace ExtensionMethods { public static class&#8230; <a href=\"https:\/\/reasty.net\/?p=10\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[21,31,58],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-net","tag-c","tag-extension-method","tag-58"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/reasty.net\/index.php?rest_route=\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/reasty.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/reasty.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/reasty.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/reasty.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10"}],"version-history":[{"count":0,"href":"https:\/\/reasty.net\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions"}],"wp:attachment":[{"href":"https:\/\/reasty.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/reasty.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/reasty.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}