https://stackoverflow.com/questions/77231327/how-to-serialize-a-castle-dynamic-proxy-interface-using-newtonsoft-json

CSharp · Console

/Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Reflection;
using System.Dynamic;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;	

using Moq;

using NUnit.Framework; // Throws NUnit.Framework.AssertionException

//https://stackoverflow.com/questions/77231327/how-to-serialize-a-castle-dynamic-proxy-interface-using-newtonsoft-json

	// Define other methods and classes here
	public interface IConfig
	{
		string Key { get; set; }
	}

    class TestClass
    {
		static void ShowExplicitImplementations(Type concreteType, Type ifaceType)
		{
			InterfaceMapping map = concreteType.GetInterfaceMap(ifaceType);
			for (int i = 0; i < map.InterfaceMethods.Length; i++)
			{
				MethodInfo ifaceMethod = map.InterfaceMethods[i];
				MethodInfo targetMethod = map.TargetMethods[i];
				Console.WriteLine(String.Format("{0} maps to {1}", ifaceMethod, targetMethod));
			}
		}
		
        public static void Test()
        {
			var config = new Mock<IConfig>();
			config.Setup(x => x.Key).Returns("test");
			var proxy = config.Object;
			
			var contract = (new DefaultContractResolver()).ResolveContract(proxy.GetType());
			Console.WriteLine(proxy.GetType());
			if (contract is JsonObjectContract objContract)
				foreach (var property in objContract.Properties)
					Console.WriteLine("  {0}: {1}", property.UnderlyingName, property.PropertyType);
			
			Console.WriteLine();

			var json = JsonConvert.SerializeObject(proxy, Newtonsoft.Json.Formatting.Indented);
			json.Dump();			
		}	
	}

	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: {0} ({1}), {2}", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
			Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Namespace, typeof(JsonSerializer).Assembly.FullName);
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Failed with unhandled exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
	}
;