小编Jel*_*neK的帖子

为什么在传递动态对象的接口类型上调用继承的方法时会抛出RuntimeBinderException?

你能解释一下这段代码的行为吗:

namespace DynamicTesting
{
    public interface IDynamicTargetBase
    {
        string Hello(int a);
    }

    public interface IDynamicTarget : IDynamicTargetBase
    {
    }

    public class DynamicTarget : IDynamicTarget
    {
        public string Hello(int a)
        {
            return "Hello!";
        }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            dynamic a = 123;

            IDynamicTargetBase obj1 = new DynamicTarget();
            obj1.Hello(a);  // This works just fine

            IDynamicTarget obj2 = new DynamicTarget();
            obj2.Hello(a); // RuntimeBinderException "No overload for method 'Hello' takes '1' arguments"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果是:

[Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:在System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2 …

c# dynamic

6
推荐指数
0
解决办法
94
查看次数

C# - 将嵌套的 json 反序列化为嵌套的 Dictionary<string, object>

我正在使用 .net core 3.1 和库 System.Text.Json

如何将嵌套的 json 对象反序列化为 Dictionary<string, object>,但期望基于 json 属性类型我将获得正确的 C# 类型:

String -> string
Number -> int/double
Object -> Dictionary<string, object>
Run Code Online (Sandbox Code Playgroud)

默认情况下 - 如果我尝试反序列化为 Dictionary<string, object> - 基本上每个对象都是一个 JsonElement。我希望它是如上所述的类型。

知道如何实现吗?

c# json system.text.json

1
推荐指数
1
解决办法
633
查看次数

标签 统计

c# ×2

dynamic ×1

json ×1

system.text.json ×1