Dynamic Anonymous type in Razor causes RuntimeBinderException

后端 未结 12 1012
不思量自难忘°
不思量自难忘° 2020-11-22 07:45

I\'m getting the following error:

\'object\' does not contain a definition for \'RatingName\'

When you look at the anonymous dyn

12条回答
  •  半阙折子戏
    2020-11-22 08:10

    Now in recursive flavor

    public static ExpandoObject ToExpando(this object obj)
        {
            IDictionary expandoObject = new ExpandoObject();
            new RouteValueDictionary(obj).ForEach(o => expandoObject.Add(o.Key, o.Value == null || new[]
            {
                typeof (Enum),
                typeof (String),
                typeof (Char),
                typeof (Guid),
    
                typeof (Boolean),
                typeof (Byte),
                typeof (Int16),
                typeof (Int32),
                typeof (Int64),
                typeof (Single),
                typeof (Double),
                typeof (Decimal),
    
                typeof (SByte),
                typeof (UInt16),
                typeof (UInt32),
                typeof (UInt64),
    
                typeof (DateTime),
                typeof (DateTimeOffset),
                typeof (TimeSpan),
            }.Any(oo => oo.IsInstanceOfType(o.Value))
                ? o.Value
                : o.Value.ToExpando()));
    
            return (ExpandoObject) expandoObject;
        }
    

提交回复
热议问题