Ejemplo n.º 1
0
        private void PrepareFinish(AutoGenerateContext context)
        {
            if (!FinishInitialized)
            {
                // Try and get the registered finish with for the current rule
                FinalizeActions.TryGetValue(currentRuleSet, out FinalizeAction <TType> finishWith);

                // Add an internal finish to auto populate any remaining values
                FinishWith((faker, instance) =>
                {
                    // Extract the unpopulated member infos
                    var members     = new List <MemberInfo>();
                    var memberNames = GetRuleSetsMemberNames(context);

                    foreach (var member in TypeProperties)
                    {
                        if (!memberNames.Contains(member.Key))
                        {
                            members.Add(member.Value);
                        }
                    }

                    // Finalize the instance population
                    context.Binder.PopulateInstance <TType>(instance, context, members);

                    // Ensure the default finish with is invoke
                    if (finishWith != null)
                    {
                        finishWith.Action(faker, instance);
                    }
                });

                FinishInitialized = true;
            }
        }
Ejemplo n.º 2
0
        private void PrepareFinish(AutoGenerateContext context)
        {
            if (!FinishInitialized)
            {
                // Try and get the registered finish with for the current rule
                FinalizeActions.TryGetValue(currentRuleSet, out FinalizeAction <TType> finishWith);

                // Add an internal finish to auto populate any remaining values
                FinishWith((faker, instance) =>
                {
#if !NETSTANDARD1_3
                    // If dynamic objects are supported, populate as a dictionary
                    var type = instance?.GetType();

                    if (ReflectionHelper.IsExpandoObject(type))
                    {
                        // Configure the context
                        context.ParentType   = null;
                        context.GenerateType = type;
                        context.GenerateName = null;

                        context.Instance = instance;

                        // Get the expando generator and populate the instance
                        var generator = AutoGeneratorFactory.GetGenerator(context);
                        generator.Generate(context);

                        // Clear the context instance
                        context.Instance = null;

                        return;
                    }
#endif
                    // Otherwise continue with a standard populate
                    // Extract the unpopulated member infos
                    var members     = new List <MemberInfo>();
                    var memberNames = GetRuleSetsMemberNames(context);

                    foreach (var member in TypeProperties)
                    {
                        if (!memberNames.Contains(member.Key))
                        {
                            members.Add(member.Value);
                        }
                    }

                    // Finalize the instance population
                    context.Binder.PopulateInstance <TType>(instance, context, members);

                    // Ensure the default finish with is invoke
                    if (finishWith != null)
                    {
                        finishWith.Action(faker, instance);
                    }
                });

                FinishInitialized = true;
            }
        }