将List <dynamic>转换为List <t>

时间:2019-04-23 18:27:53

标签: c# dynamic type-conversion

使用以下功能,如何从List<dynamic> back to List<T>转换?

var retval = (IEnumerable<T>)Convert.ChangeType(normalised, typeof(IEnumerable<dynamic>));

不起作用。我得到的例外是“对象必须实现IConvertible

   public static IEnumerable<T> NormalizeZeroAndOne<T>(this IEnumerable<T> list)
    {
        var max = list.Max();
        var min = list.Min();

        T range = ((dynamic)max - (dynamic)min);

        //var range = (max - min);
        var normalised
            = list.Select(i => ((dynamic)i - (dynamic)min) / range)
                .ToList();

        var retval = (IEnumerable<T>)Convert.ChangeType(normalised, typeof(IEnumerable<dynamic>));

        return retval;
    }

0 个答案:

没有答案