{"record":{"id":"42b738ac511efc3a","repo":"JamesNK/Newtonsoft.Json","slug":"could-not-resolve-type-0-you-may-need-to-add","errorCode":null,"errorMessage":"Could not resolve type '{0}'. You may need to add a reference to Microsoft.CSharp.dll to work with dynamic types.","messagePattern":"Could not resolve type '(.+?)'\\. You may need to add a reference to Microsoft\\.CSharp\\.dll to work with dynamic types\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Utilities/DynamicUtils.cs","lineNumber":73,"sourceCode":"            private const string CSharpArgumentInfoFlagsTypeName = \"Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags, \" + CSharpAssemblyName;\n            private const string CSharpBinderFlagsTypeName = \"Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags, \" + CSharpAssemblyName;\n\n            private static object? _getCSharpArgumentInfoArray;\n            private static object? _setCSharpArgumentInfoArray;\n            private static MethodCall<object?, object?>? _getMemberCall;\n            private static MethodCall<object?, object?>? _setMemberCall;\n            private static bool _init;\n\n            [RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]\n            [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n            private static void Init()\n            {\n                if (!_init)\n                {\n                    Type? binderType = Type.GetType(BinderTypeName, false);\n                    if (binderType == null)\n                    {\n                        throw new InvalidOperationException(\"Could not resolve type '{0}'. You may need to add a reference to Microsoft.CSharp.dll to work with dynamic types.\".FormatWith(CultureInfo.InvariantCulture, BinderTypeName));\n                    }\n\n                    // None\n                    _getCSharpArgumentInfoArray = CreateSharpArgumentInfoArray(0);\n                    // None, Constant | UseCompileTimeType\n                    _setCSharpArgumentInfoArray = CreateSharpArgumentInfoArray(0, 3);\n                    CreateMemberCalls();\n\n                    _init = true;\n                }\n            }\n\n            [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n            private static object CreateSharpArgumentInfoArray(params int[] values)\n            {\n                Type csharpArgumentInfoType = Type.GetType(CSharpArgumentInfoTypeName, true)!;\n                Type csharpArgumentInfoFlags = Type.GetType(CSharpArgumentInfoFlagsTypeName, true)!;\n","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Utilities/DynamicUtils.cs#L55-L91","documentation":"BinderWrapper.Init (DynamicUtils.cs:66-84) resolves the Microsoft.CSharp.RuntimeBinder types by reflection (Type.GetType of the fully-qualified BinderTypeName) to support deserializing into `dynamic`. If Microsoft.CSharp.dll is not loaded/referenced, Type.GetType returns null and it throws InvalidOperationException at DynamicUtils.cs:73, advising to add a reference to Microsoft.CSharp.dll.","triggerScenarios":"Deserializing JSON into `dynamic`/ExpandoObject (or otherwise exercising the dynamic binder path) on a runtime/framework where the Microsoft.CSharp assembly is absent — only reached when HAVE_REFLECTION_BINDER is not defined.","commonSituations":".NET Core / .NET 5+ projects without the Microsoft.CSharp NuGet package; trimmed/AOT apps where the binder was removed; deserializing to dynamic in environments that ship a minimal BCL.","solutions":["Add the Microsoft.CSharp NuGet package (or the assembly reference) to the project.","Avoid deserializing to dynamic; deserialize to a concrete type or to JObject/JToken instead.","If trimming, ensure Microsoft.CSharp is not trimmed (add a trim root / TrimmerRootDescriptor).","On frameworks with HAVE_REFLECTION_BINDER defined, ensure the Binder type is reachable."],"exampleFix":"// before\ndynamic d = JsonConvert.DeserializeObject(json); // throws if Microsoft.CSharp missing\n// after: add the Microsoft.CSharp package, or\nJObject d = JsonConvert.DeserializeObject<JObject>(json);","handlingStrategy":"validation","validationCode":"// Detect the missing binder before deserializing to dynamic.\nstatic bool DynamicBinderAvailable() {\n    try { return System.AppDomain.CurrentDomain.GetAssemblies().Any(a => a.GetName().Name == \"Microsoft.CSharp\"); }\n    catch { return false; }\n}","typeGuard":"static bool SupportsDynamic() => Type.GetType(\"Microsoft.CSharp.RuntimeBinder.Binder, Microsoft.CSharp\") != null;","tryCatchPattern":"try { dynamic d = JsonConvert.DeserializeObject(json); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Microsoft.CSharp.dll\")) { var d = JsonConvert.DeserializeObject<JObject>(json); }","preventionTips":["Add the Microsoft.CSharp package when deserializing to dynamic.","Prefer JObject/JToken over dynamic for portability.","Prevent trimming of Microsoft.CSharp in AOT scenarios."],"tags":["dynamic","configuration","dependency","newtonsoft-json"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}