Пример #1
0
        private Action <TCase> GenerateSetter(AttributeSchema attributeSchema, AttributeDefinition attributeDefinition)
        {
            Func <Delegate, Action <TCase> > factory = SetterFactory <int>;

            var setter = DynamicCodeUtils.GenerateSetter <TCase>(attributeDefinition);

            return(DynamicCodeUtils.DelegateInvoke(factory, attributeDefinition.Type, setter, this));

            Action <TCase> SetterFactory <T>(Delegate generatedSetter)
            {
                if (!(generatedSetter is Setter <TCase, T> setterDelegate))
                {
                    throw new InvalidOperationException("Encountered incorrect setter type.");
                }

                var getter = cursor.GetAttributeGetter <T>(attributeSchema);

                return(target =>
                {
                    T value = default;
                    getter(ref value);
                    setterDelegate(target, value);
                });
            }
        }
Пример #2
0
 protected TypedCaseFrame(CaseSchema schema)
 {
     Definition     = new CaseDefinitionBuilder(typeof(TCase), schema).Build();
     Schema         = schema ?? Definition.GetCaseSchema();
     IdGetter       = DynamicCodeUtils.GenerateGetter <TCase>(Definition.IdAttribute);
     SolutionGetter = DynamicCodeUtils.GenerateGetter <TCase>(Definition.SolutionAttribute);
     Getters        = GenerateGetters(Definition);
 }
Пример #3
0
 public static Delegate Generate <TTarget>(PropertyInfo property)
 {
     if (property == null)
     {
         throw new ArgumentNullException(nameof(property));
     }
     return(DynamicCodeUtils.DelegateInvoke(Generate <int, int>, typeof(TTarget), property.PropertyType, property));
 }
Пример #4
0
 public Case(TCase caseData, CaseSchema schema = null)
 {
     Data           = caseData ?? throw new ArgumentNullException(nameof(caseData));
     definition     = new CaseDefinitionBuilder(typeof(TCase), schema).Build();
     Schema         = schema ?? definition.GetCaseSchema();
     idGetter       = DynamicCodeUtils.GenerateGetter <TCase>(definition.IdAttribute);
     solutionGetter = DynamicCodeUtils.GenerateGetter <TCase>(definition.SolutionAttribute);
     getters        = GenerateGetters();
 }
Пример #5
0
        private static Delegate[] GenerateGetters(CaseDefinition definition)
        {
            var result = new Delegate[definition.Count];

            for (var i = 0; i < definition.Count; i++)
            {
                result[i] = DynamicCodeUtils.GenerateGetter <TCase>(definition[i]);
            }

            return(result);
        }
Пример #6
0
        private Action <TCase> GenerateIdSetter()
        {
            var setter = DynamicCodeUtils.GenerateSetter <TCase>(definition.IdAttribute);

            if (!(setter is Setter <TCase, string> setterDelegate))
            {
                throw new InvalidOperationException("Encountered incorrect ID setter type.");
            }

            return(target =>
            {
                var value = cursor.GetId();
                setterDelegate(target, value);
            });
        }
Пример #7
0
        private Action <TCase> GenerateSolutionSetter()
        {
            Func <Delegate, Action <TCase> > factory = SetterFactory <int>;
            var setter = DynamicCodeUtils.GenerateSetter <TCase>(definition.SolutionAttribute);

            return(DynamicCodeUtils.DelegateInvoke(factory, definition.SolutionAttribute.Type, setter, this));

            Action <TCase> SetterFactory <T>(Delegate generatedSetter)
            {
                if (!(generatedSetter is Setter <TCase, T> setterDelegate))
                {
                    throw new InvalidOperationException("Encountered incorrect solution setter type.");
                }

                return(target =>
                {
                    var value = cursor.GetSolution <T>();
                    setterDelegate(target, value);
                });
            }
        }
Пример #8
0
        private Delegate[] GenerateGetters(Delegate[] dynamicGetters = null)
        {
            Func <Delegate, Delegate> factory = GetterFactory <int>;

            var result = new Delegate[Schema.Count];

            for (var i = 0; i < Schema.Count; i++)
            {
                var getter = dynamicGetters != null ? dynamicGetters[i] : DynamicCodeUtils.GenerateGetter <TCase>(definition[i]);
                result[i] = DynamicCodeUtils.DelegateInvoke(factory, Schema[i].Type, getter, this);
            }

            return(result);

            Delegate GetterFactory <T>(Delegate getter)
            {
                if (!(getter is Getter <TCase, T> getterDelegate))
                {
                    throw new InvalidOperationException("Encountered incorrect getter type.");
                }

                return((AttributeGetter <T>)((ref T value) => getterDelegate(Data, ref value)));
            }
        }