Object.GetType() 不适用于动态参数

Object.GetType() doesn't work with dynamic params

我正在学习 C#,我正在尝试了解具有动态参数的函数是如何工作的,我有以下示例:

    Type[] GetTypes(params object[] args)
    {
        Type[] types = new Type[args.Length];
    
        for (int i = 0; i < args.Length; i++)
        {
            types[i] = args.GetType();
            Console.WriteLine(types[i].ToString());
        }
            
        return types;
    }
    
    void MyFunction(params object[] args)
    {
        foreach (var type in GetTypes(args))
        {
            Console.WriteLine(type.ToString());
        }
    }
    
    MyFunction(56, true, 45.9, "testing types deduction");

我在这里发现的问题是推导不正确导致输出是:

>System.Object[] 
>System.Object[] 
>System.Object[] 
>System.Object[] 
>System.Object[] 
>System.Object[] 
>System.Object[] 
>System.Object[] 

并期待

>System.Int32 
>System.Bool 
>System.Double 
>System.String 
>System.Int32 
>System.Bool 
>System.Double 
>System.String

有什么方法可以解决这个问题,或者有不同的方法来获取 args 中参数接收的特定类型吗?

你错误地写了 types[i] = args.GetType();,而你应该写 types[i] = args[i].GetType();