Пример #1
0
            private IEnumerable <KeyValuePair <string, IASValue> > GetMappedDynamicProperties(IActionScriptSerializer serializer, object instance)
            {
                foreach (ActionScriptPropertyMapping mapping in dynamicPropertyMappings)
                {
                    yield return(new KeyValuePair <string, IASValue>(mapping.ASPropertyName,
                                                                     GetMappedPropertyOrField(serializer, instance, mapping.NativePropertyOrField)));
                }

                IDynamic dynamic = instance as IDynamic;

                if (dynamic != null)
                {
                    // This somewhat contorted bit of code serves to catch and report
                    // failures while getting dynamic properties from a class in a somewhat
                    // more default fashion than would be possible using a plain foreach loop
                    // because yield return cannot be used inside a try/catch.
                    IEnumerator <KeyValuePair <string, IASValue> > en;
                    try
                    {
                        en = dynamic.GetDynamicProperties(serializer).GetEnumerator();
                    }
                    catch (Exception ex)
                    {
                        throw new ActionScriptException(String.Format(CultureInfo.CurrentCulture,
                                                                      "Error while getting values of dynamic properties from class '{0}'",
                                                                      dynamic.GetType().FullName), ex);
                    }

                    for (; ;)
                    {
                        KeyValuePair <string, IASValue> pair;
                        try
                        {
                            if (!en.MoveNext())
                            {
                                break;
                            }
                            pair = en.Current;
                        }
                        catch (Exception ex)
                        {
                            throw new ActionScriptException(String.Format(CultureInfo.CurrentCulture,
                                                                          "Error while getting values of dynamic properties from class '{0}'",
                                                                          dynamic.GetType().FullName), ex);
                        }

                        yield return(pair);
                    }
                }
            }