コード例 #1
0
        public static IronPython.Runtime.PythonDictionary AsPyDictionary(this object source)        //, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
        {
            var  pyDic          = new IronPython.Runtime.PythonDictionary();
            Type someObjectType = source.GetType();

            foreach (var item in source.GetType().GetProperties())
            {
                object newValue = null;
                try
                {
                    if ((item.PropertyType.GetInterfaces().Contains(typeof(System.Collections.IList))))
                    {
                        MethodInfo method = typeof(ObjectExtensions).GetMethod("AsPyList");
                        var        value  = item.GetValue(source, null);
                        if (value != null)
                        {
                            newValue = method.Invoke(null, new object[] { item.GetValue(source, null) });
                        }
                    }
                    else if (item.PropertyType.Namespace == typeof(ObjectExtensions).Namespace)
                    {
                        MethodInfo method = typeof(ObjectExtensions).GetMethod("AsPyDictionary");
                        var        value  = item.GetValue(source, null);
                        if (value != null)
                        {
                            newValue = method.Invoke(null, new object[] { item.GetValue(source, null) });
                        }
                    }
                    else
                    {
                        newValue = item.GetValue(source, null);
                    }

                    //pyDic.Add(item.Name, item.GetValue(source, null));
                    if (newValue != null)
                    {
                        //we encode strings as unicode
                        if (newValue.GetType() == typeof(string))
                        {
                            pyDic.Add(item.Name, Encoding.Unicode.GetString(Encoding.Unicode.GetBytes((string)newValue)));
                        }
                        else
                        {
                            pyDic.Add(item.Name, newValue);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Unable to add:" + newValue.ToString());
                    Console.WriteLine(ex.Message);
                }
            }


            return(pyDic);
        }
コード例 #2
0
 public RefUnit(Unit unit)
 {
     foreach (var bts in unit.bt.status)
     {
         var key   = bts.Key;
         var value = bts.Value.now;
         status.Add(key, value);
     }
 }
コード例 #3
0
        public static IronPython.Runtime.PythonDictionary ToPythonDictionary(this System.Collections.Generic.IDictionary <string, object> dictionary)
        {
            var pyDictionary = new IronPython.Runtime.PythonDictionary();

            foreach (var pair in dictionary)
            {
                pyDictionary.Add(System.Collections.Generic.KeyValuePair.Create <object, object>(pair.Key.ToString(), pair.Value.ToString()));
            }
            return(pyDictionary);
        }