{"id":8686,"date":"2017-06-29T13:57:56","date_gmt":"2017-06-29T04:57:56","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/?p=8686"},"modified":"2017-06-29T15:00:01","modified_gmt":"2017-06-29T06:00:01","slug":"%e3%81%a1%e3%82%87%e3%81%a3%e3%81%a8%e9%9b%91%e3%81%a0%e3%81%8c%e3%80%81c-%e3%81%a7-jsonprovider-%e3%82%82%e3%81%a9%e3%81%8d%e3%82%92%e4%bd%9c%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.moonmile.net\/blog\/archives\/8686","title":{"rendered":"\u3061\u3087\u3063\u3068\u96d1\u3060\u304c\u3001C# \u3067 JsonProvider \u3082\u3069\u304d\u3092\u4f5c\u308b"},"content":{"rendered":"<p>\nF# \u306b\u306f TypeProvider \u3068\u3044\u3046\u306e\u304c\u3042\u3063\u3066\u3001\u52d5\u7684\u306b\u30af\u30e9\u30b9\u3092\u4f5c\u308a\u3001\u305d\u308c\u3092 F# \u306e\u30b3\u30fc\u30c9\u3067\u6271\u3048\u308b\u3082\u306e\u3067\u3059\u3002\n<\/p>\n<p>\nF# Data: JSON \u578b\u30d7\u30ed\u30d0\u30a4\u30c0\u30fc<br \/>\n<a href=\"https:\/\/fsharp.github.io\/FSharp.Data\/ja\/library\/JsonProvider.html\">https:\/\/fsharp.github.io\/FSharp.Data\/ja\/library\/JsonProvider.html<\/a>\n<\/p>\n<p>\n\u3067\u3082\u3063\u3066\u3001C# \u306b\u306f TypeProvider \u304c\u306a\u3044\u3093\u306e\u3067 F# \u304c\u7fa8\u307e\u3057\u304b\u3063\u305f\u308a\u3001\u3044\u3084\u305d\u3046\u8a00\u3046\u306a\u3089 F# \u3067\u7d44\u3081\u3070\u3044\u3044\u8a33\u3067\u3059\u304c\u3002\u4ee5\u524d\u3001F# \u306e XAML \u306e TypeProvider \u3092\u4f5c\u3063\u305f\u3068\u304d\u306b\u3001\u305d\u306e\u307e\u307e Xamarin \u306e PCL \u306b\u306f\u6301\u3063\u3066\u884c\u3051\u306a\u304f\u3066\u8ae6\u3081\u305f\u899a\u3048\u304c\u3042\u308b\u3093\u3067\u3059\u304c\u3002\u4eca\u3060\u3068\u3082\u3046\u3061\u3087\u3063\u3068\u5de5\u592b\u3067\u304d\u308b\u304b\u3082\u3001\u3063\u3066\u3053\u3068\u3067\u3001C# \u3067 TypeBuilder \u3092\u4f7f\u3063\u3066\u307f\u307e\u3059\u3002\n<\/p>\n<p><h2>\u96d1\u306b JsonProvider \u3092\u4f5c\u308b<\/h2>\n<\/p>\n<p>\nJSON \u306e\u6587\u5b57\u5217\u3092\u6e21\u3057\u3066\u3001\u305d\u308c\u3092\u30d7\u30ed\u30d1\u30c6\u30a3\u306b\u6301\u3064\u30af\u30e9\u30b9\u3092\u4f5c\u308a\u307e\u3059\u3002\u5b9f\u306f\u3001Newtonsoft.Json \u306f dynamic \u3092\u6301\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u3042\u307e\u308a\u610f\u5473\u306f\u306a\u3044&#8230;\u3068\u3044\u3046\u304b\u3001\u7d50\u8ad6\u304b\u3089\u8a00\u3048\u3070 T4 \u3068\u304b CodeDOM \u3092\u4f7f\u3063\u305f\u307b\u3046\u304c\u65e9\u3044\u306e\u3067\u306f\uff1f\u3068\u3044\u3046\u611f\u3058\u306f\u3057\u307e\u3059\u3002\n<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nclass Program\n{\n    static void Main(string&#x5B;] args)\n    {\n        var json = @&quot;{ name: &#039;tomoaki&#039;, num: 101 }&quot;;\n        var jp = new JsonProvider(json);\n        var t = jp.Make(&quot;SampleJson&quot;);\n\n        var o = new SampleJson();\n        o.name = &quot;aaaa&quot;;\n        o.num = &quot;100&quot;;\n    }\n}\npublic class JsonProvider\n{\n    JObject root;\n    Dictionary&lt;string, string&gt; dic = new Dictionary&lt;string, string&gt;();\n\n    public JsonProvider( string json )\n    {\n        root = JObject.Parse(json);\n        var cur = root.GetEnumerator();\n        while ( cur.MoveNext() )\n        {\n            var it = cur.Current;\n            Debug.WriteLine(&quot;{0} {1}&quot;, it.Key, it.Value);\n            dic.Add(it.Key.ToString(), it.Value.ToString());\n        }\n    }\n\n    public Type Make( string className )\n    {\n        var assemblyName = new AssemblyName(&quot;JsonProviderAssembly&quot;);\n        var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);\n        var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name, assemblyName.Name + &quot;.dll&quot;);\n        var mb = moduleBuilder.DefineType(className, TypeAttributes.Class | TypeAttributes.Public, typeof(object));\n\n        foreach (var it in dic)\n        {\n            var propName = it.Key;\n\n            FieldBuilder customerNameBldr = mb.DefineField(&quot;_&quot; + propName, typeof(string), FieldAttributes.Private);\n            PropertyBuilder custNamePropBldr = mb.DefineProperty(propName, PropertyAttributes.HasDefault, typeof(string), null);\n            MethodBuilder custNameGetPropMthdBldr =\n                        mb.DefineMethod(&quot;get_&quot; + propName,\n                            MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,\n                            typeof(string),\n                            Type.EmptyTypes);\n            ILGenerator custNameGetIL = custNameGetPropMthdBldr.GetILGenerator();\n            custNameGetIL.Emit(OpCodes.Ldarg_0);\n            custNameGetIL.Emit(OpCodes.Ldfld, customerNameBldr);\n            custNameGetIL.Emit(OpCodes.Ret);\n            MethodBuilder custNameSetPropMthdBldr =\n                        mb.DefineMethod(&quot;set_&quot; + propName,\n                            MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,\n                            null,\n                            new Type&#x5B;] { typeof(string) });\n            ILGenerator custNameSetIL = custNameSetPropMthdBldr.GetILGenerator();\n            custNameSetIL.Emit(OpCodes.Ldarg_0);\n            custNameSetIL.Emit(OpCodes.Ldarg_1);\n            custNameSetIL.Emit(OpCodes.Stfld, customerNameBldr);\n            custNameSetIL.Emit(OpCodes.Ret);\n            custNamePropBldr.SetGetMethod(custNameGetPropMthdBldr);\n            custNamePropBldr.SetSetMethod(custNameSetPropMthdBldr);\n        }\n        Type t = mb.CreateType();\n        assemblyBuilder.Save(assemblyName.Name + &quot;.dll&quot;);\n        return t;\n    }\n}\n<\/pre>\n<p>\nModuleBuilder.CreateType \u3067\u30af\u30e9\u30b9\u3092\u4f5c\u3063\u305f\u5f8c\u3067\u3001AssemblyBuilder.Save \u3067\u4fdd\u5b58\u3057\u307e\u3059\u3002F# \u306e TypeProvider \u306e\u5834\u5408\u306f\u3053\u308c\u304c\u30d3\u30eb\u30c9\u6642\u306b\u884c\u308f\u308c\u308b\u306e\u3067\u3001\u30d3\u30eb\u30c9\u6642\u306e\u30a2\u30bb\u30f3\u30d6\u30ea\u3068\u5b9f\u884c\u6642\u306e\u30a2\u30bb\u30f3\u30d6\u30ea\u304c\u7570\u306a\u308b\u306e\u3067\u4e0d\u6574\u5408\u304c\u8d77\u308a\u307e\u3059\u3002\u3058\u3083\u3042\u3001\u3069\u3061\u3089\u3082 .NET Framework\u306e\u74b0\u5883\u3067\u3042\u3063\u305f\u308a\u3001\u30d3\u30eb\u30c9\u6642\u306b\u6562\u3048\u3066 Xamarin.Forms \u306e PCL \u306b\u5408\u3046\u3088\u3046\u306a\u30a2\u30bb\u30f3\u30d6\u30ea\u3092\u885d\u304f\u3066\u3084\u308c\u3070\u826f\u3044\u306e\u3060\u308d\u3046\u3001\u3068\u8003\u3048\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u3053\u308c\u306f\u307e\u305f\u5f8c\u3067\u5b9f\u9a13\u3057\u307e\u3059\u3002\n<\/p>\n<p>\n\u3055\u3066\u3001AssemblyBuilder.Save \u3067\u4fdd\u5b58\u3057\u305f DLL \u3092\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304b\u3089\u53c2\u7167\u8a2d\u5b9a\u3059\u308b\u3068\u4f5c\u6210\u3057\u305f SampleJson \u30af\u30e9\u30b9\u304c\u4f7f\u3048\u307e\u3059\u3002\u4e00\u5ea6\u3001\u5b9f\u884c\u3057\u3066 DLL \u3092\u4f5c\u3089\u306a\u3044\u3068\u99c4\u76ee\u3063\u3066\u3068\u3053\u308d\u304c\u3001\u7d50\u5c40\u306e\u3068\u3053\u308d T4 \u3068\u540c\u3058\u3067\u3001\u3042\u307e\u308a\u610f\u5473\u304c\u306a\u3044\u3002F# \u306e TypeBuilder \u306e\u3088\u3046\u306b\u81ea\u52d5\u3067\u30a4\u30f3\u30c6\u30ea\u30bb\u30f3\u30b9\u304c\u52b9\u3051\u3070\u3044\u3044\u3093\u3060\u3051\u3069\u3002<br \/>\n\u3061\u306a\u307f\u306b\u3001\u4f5c\u6210\u3057\u305f JsonProviderAssembly.dll \u3092\u53c2\u7167\u8a2d\u5b9a\u3057\u3066\u3001SampleJson \u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u308d\u3046\u3068\u3059\u308b\u3068 DLL \u304c\u30ed\u30c3\u30af\u3055\u308c\u3066\u66f8\u304d\u8fbc\u3081\u306a\u3044\u3068\u3044\u3046\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u306a\u72b6\u614b\u306b\u306a\u308a\u307e\u3059\u3002\u30d3\u30eb\u30c9\u6642\u306b\u30b3\u30d4\u30fc\u3059\u308b\u51e6\u7406\u304c\u5fc5\u8981\u3067\u3059\u306d\u3002<br \/>\n\u3042\u3068\u3001\u5b9f\u884c\u3057\u306a\u3044\u3068\u30a2\u30bb\u30f3\u30d6\u30ea\u304c\u4f5c\u3089\u308c\u306a\u3044\u306e\u3067\u3001T4 \u306b\u3057\u3066\u3001\u30d3\u30eb\u30c9\u6642\u306b\u30a2\u30bb\u30f3\u30d6\u30ea\u3092\u4f5c\u3063\u3066\u5f8c\u304b\u3089\u53c2\u7167\u3059\u308b\u3068\u304b\u306b\u3057\u306a\u3044\u3068\u3002\n<\/p>\n<p>\n\u30af\u30e9\u30b9\u3092\u5b9a\u7fa9\u3059\u308b\u305f\u3081\u306e\u3044\u304f\u3064\u304b\u306e\u65b9\u6cd5: C#\u3000\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3000\u518d\u5165\u9580<br \/>\n<a href=\"http:\/\/dotnetcsharptips.seesaa.net\/article\/416983160.html\">http:\/\/dotnetcsharptips.seesaa.net\/article\/416983160.html<\/a>\n<\/p>\n<p>\n\u3053\u308c\u3092\u898b\u308b\u9650\u308a\u3001CSharpCodeProvider \u3092\u4f7f\u3063\u3066\u6587\u5b57\u5217\u304b\u3089\u751f\u6210\u3059\u308b\u307b\u3046\u304c\u697d\u305d\u3046\u3067\u3059\u306d\u3002\u304c\u3001Xamarin.Android \u304b\u3089\u306f Microsoft.CSharp.CSharpCodeProvider \u304c\u898b\u5f53\u305f\u3089\u306a\u3044\u306e\u3067\u3001\u3053\u308c\u306f\u3053\u308c\u3067\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>F# \u306b\u306f TypeProvider \u3068\u3044\u3046\u306e\u304c\u3042\u3063\u3066\u3001\u52d5\u7684\u306b\u30af\u30e9\u30b9\u3092\u4f5c\u308a\u3001\u305d\u308c\u3092 F# \u306e\u30b3\u30fc\u30c9\u3067\u6271\u3048\u308b\u3082\u306e\u3067\u3059\u3002 F# Data: JSON \u578b\u30d7\u30ed\u30d0\u30a4\u30c0\u30fc https:\/\/fsharp.github.io\/FSha &hellip; <a href=\"https:\/\/www.moonmile.net\/blog\/archives\/8686\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-8686","post","type-post","status-publish","format-standard","hentry","category-dev"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/8686","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=8686"}],"version-history":[{"count":1,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/8686\/revisions"}],"predecessor-version":[{"id":8687,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/8686\/revisions\/8687"}],"wp:attachment":[{"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=8686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=8686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=8686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}