예제 #1
0
 private IEnumerable <KeyValuePair <string, object> > GetSubItems(object obj)
 {
     // not recursive, that would take forever and probably cause an infinite loop
     if (obj != null)
     {
         if (obj is IEnumerable)
         {
             // list, dictionary, etc.
             foreach (var item in obj as IEnumerable)
             {
                 yield return(new KeyValuePair <string, object>(item.ToString(), item));
             }
         }
         else
         {
             // regular object
             var ctx   = new ObjectGraphContext();
             var props = ctx.GetProperties(obj.GetType());
             foreach (var prop in props)
             {
                 yield return(new KeyValuePair <string, object>(prop.Key, prop.Value.GetValue(obj, null)));
             }
         }
     }
 }
예제 #2
0
        static void Main(string[] args)
        {
            char yn;

            Console.WriteLine("Which implementation of Equals is faster?");
            PrintReport(EqualsPerf.WhichEqualsIsFaster());

            do
            {
                Console.WriteLine("Run savegame analysis? [yn] This might take a while...");
                yn = Console.ReadKey(true).KeyChar.ToString().ToLower()[0];
            } while (yn != 'y' && yn != 'n');

            if (yn == 'y')
            {
                Console.WriteLine("Running analysis...");
                Galaxy.Load("Quickstart_1.gam");
                var             dict     = new SafeDictionary <Type, Tuple <int, int> >(true);
                var             p        = new ObjectGraphParser();
                Action <object> startobj = o =>
                {
                    var t         = o.GetType();
                    var old       = dict[t];
                    int propcount = old.Item1;
                    if (propcount <= 0)
                    {
                        ObjectGraphContext.AddProperties(t);
                        propcount = ObjectGraphContext.GetKnownProperties(t).Count;
                    }
                    int objcount = old.Item2;
                    objcount++;
                    dict[t] = Tuple.Create(propcount, objcount);
                };
                p.StartObject += new ObjectGraphParser.ObjectDelegate(startobj);
                p.Parse(Galaxy.Current);
                var list = new List <Tuple <string, int, int, int> >();
                foreach (var x in dict)
                {
                    var typename  = x.Key.Name;
                    var propcount = x.Value.Item1;
                    var objcount  = x.Value.Item2;
                    var cost      = propcount * objcount;
                    list.Add(Tuple.Create(typename, propcount, objcount, cost));
                }
                var sorted    = list.OrderByDescending(x => x.Item4);              // sort by total cost descending
                var totalcost = list.Sum(x => x.Item4);

                Console.WriteLine($"{list.Sum(x => x.Item3)} total objects found in savegame with a total of {totalcost} property accesses.");
                WriteTabbed("Type", "Props", "Objs", "Cost", "%Cost");
                foreach (var x in sorted)
                {
                    WriteTabbed(x.Item1, x.Item2, x.Item3, x.Item4, (100d * x.Item4 / totalcost) + "%");
                }
            }
            else
            {
                Console.WriteLine("OK, skipping it then.");
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
예제 #3
0
        public void Bind()
        {
            table.RowStyles.Clear();
            table.Controls.Clear();
            if (Data != null)
            {
                if (Data is IEnumerable)
                {
                    // list or dictionary or whatever
                    var  type     = Data.GetType();
                    var  list     = Data as IEnumerable;
                    Type itemType = null;
                    bool isDict;
                    if (type.GetGenericArguments().Length == 2)
                    {
                        // HACK - assume it's a dictionary, no real way to test
                        itemType = typeof(KeyValuePair <,>).MakeGenericType(type.GetGenericArguments());
                        isDict   = true;
                    }
                    else if (type.BaseType.GetGenericArguments().Length == 2)
                    {
                        // HACK - Resources inherits from a dictionary type
                        itemType = typeof(KeyValuePair <,>).MakeGenericType(type.BaseType.GetGenericArguments());
                        isDict   = true;
                    }
                    else if (type.GetGenericArguments().Length == 1)
                    {
                        // HACK - assume it's a collection, no real way to test
                        itemType = type.GetGenericArguments()[0];
                        isDict   = false;
                    }
                    else
                    {
                        // no generic type? probably a list of objects?
                        itemType = typeof(object);
                        isDict   = false;
                    }

                    int row = 0;
                    foreach (var item in list)
                    {
                        table.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                        var lbl = new Label();
                        if (isDict)
                        {
                            lbl.Text = item.GetPropertyValue("Key").ToString();
                        }
                        else
                        {
                            lbl.Text = "Item " + row;
                        }
                        table.Controls.Add(lbl, 0, row);
                        var editor = MakeEditor(itemType, item);
                        table.Controls.Add(editor, 1, row);
                        row++;
                    }
                }
                else
                {
                    // regular object
                    var ctx   = new ObjectGraphContext();
                    var props = ctx.GetProperties(Data.GetType());
                    int row   = 0;
                    foreach (var prop in props.Values)
                    {
                        table.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                        var lbl = new Label();
                        lbl.Text = prop.Name.ToSpacedString().Capitalize();
                        table.Controls.Add(lbl, 0, row);
                        var editor = MakeEditor(prop.PropertyType, prop.GetValue(Data, null));
                        table.Controls.Add(editor, 1, row);
                        row++;
                    }
                }
            }
        }
 /// <summary>
 /// Initializes an instance of MockResolutionContext.
 /// </summary>
 /// <param name="autoMocker">The <c>AutoMocker</c> instance.</param>
 /// <param name="requestType">The requested type to resolve.</param>
 /// <param name="initialValue">The initial value to use.</param>
 /// <param name="objectGraphContext">
 /// Context within the object graph being created. This differs from the MockResolutionContext which is
 /// only relevant for a single object creation.
 /// </param>
 public MockResolutionContext(AutoMocker autoMocker, Type requestType, object?initialValue, ObjectGraphContext objectGraphContext)
 {
     AutoMocker         = autoMocker ?? throw new ArgumentNullException(nameof(autoMocker));
     RequestType        = requestType ?? throw new ArgumentNullException(nameof(requestType));
     Value              = initialValue;
     ObjectGraphContext = objectGraphContext ?? throw new ArgumentNullException(nameof(objectGraphContext));
 }