コード例 #1
0
        public void LoadExpression <TItem>(LambdaExpression getterExpression)
        {
            GetterExpression = getterExpression;

            PropertyValueType      = ResolverTypeUtility.GetPropertyLambdaReturnType <TItem>(getterExpression.GetType());
            EnumerableTypeArgument = GetEnumerableTypeArgument(PropertyValueType);
        }
コード例 #2
0
        private static IIdentifierInfo <TItem> GetGetterIdentifierInfo(LambdaExpression getterExpr, bool multiReferenceCollection)
        {
            Type returnType = ResolverTypeUtility.GetPropertyLambdaReturnType <TItem>(getterExpr.GetType());

            if (multiReferenceCollection)
            {
                Type enumerableType = returnType.GetGenericInterface(typeof(IEnumerable <>));
                if (enumerableType == null)
                {
                    throw new StemAttributeSetupException("References attribute must be placed on expressions that return an IEnumerable.");
                }

                Type itemType = enumerableType.GetGenericArguments()[0];

                return(Reflect.Type(typeof(MultiExpressionIdentifierInfo <,>))
                       .MakeGeneric(typeof(TItem), itemType)
                       .CastTo <IIdentifierInfo <TItem> >()
                       .CreateInstance(getterExpr));
            }
            else
            {
                return(Reflect.Type(typeof(ExpressionIdentifierInfo <,>))
                       .MakeGeneric(typeof(TItem), returnType)
                       .CastTo <IIdentifierInfo <TItem> >()
                       .CreateInstance(getterExpr));
            }
        }
コード例 #3
0
        protected override void IncludeExpression(LambdaExpression expression)
        {
            Type exprBodyType = ResolverTypeUtility.GetPropertyLambdaReturnType(ItemType, expression.GetType());

            SetOrConfirmType(exprBodyType);

            AddExpressionToDefinition(expression);
        }
コード例 #4
0
        private static void IncludeSetterExpression <TItem>(EngineImplementations <TItem> implementations, FieldDefinition definition)
            where TItem : class
        {
            Type writerPropertyValueType =
                ResolverTypeUtility.GetPropertyLambdaReturnType <TItem>(definition.Setter.Expression.GetType());

            var writer = Reflect.Type(typeof(PropertyExpressionFieldWriter <,>))
                         .MakeGeneric(typeof(TItem), writerPropertyValueType)
                         .CastTo <IFieldWriter <TItem> >()
                         .CreateInstance(definition.Setter.Expression);

            var writerFactory = new SingletonFactory <IFieldWriter <TItem>, TItem>(writer);

            implementations.WriterFactories.Add(definition.FieldName, writerFactory);
        }
コード例 #5
0
        private static void IncludeLocatorExpression <TItem>(EngineImplementations <TItem> implementations, FieldDefinition definition)
            where TItem : class
        {
            Type locatorPropertyValueType =
                ResolverTypeUtility.GetPropertyLambdaReturnType <TItem>(definition.Locator.Expression.GetType());

            Debug.Assert(locatorPropertyValueType == definition.FieldType, "FieldType is incorrect");

            var locator = Reflect.Type(typeof(IdentifierExpressionItemLocator <,>))
                          .MakeGeneric(typeof(TItem), locatorPropertyValueType)
                          .CastTo <IItemLocator <TItem> >()
                          .CreateInstance(definition.Locator.Expression);

            var locatorFactory = new SingletonFactory <IItemLocator <TItem>, TItem>(locator);

            implementations.LocatorFactories.Add(definition.FieldName, locatorFactory);
        }
コード例 #6
0
        protected override Type AddMethodToDefinition(MethodInfo method)
        {
            FieldDefinition.Display = _display;

            ParameterInfo[] parameters = method.GetParameters();
            Type            returnType = method.ReturnType;

            if (_argumentMemberName != null)
            {
                PropertyInfo argumentProperty = method.ReflectedType.GetProperty(_argumentMemberName);
                if (argumentProperty == null)
                {
                    throw new StemAttributeSetupException("Cannot find a property in this Stem with the name '" + _argumentMemberName + "'");
                }

                LambdaExpression expression = GetExpressionFromProperty(argumentProperty);

                FieldDefinition.Getter.Expression = expression;

                Type delegateType = typeof(Func <,>).MakeGenericType(expression.ReturnType, returnType);
                AddMethodToHandlerPart(FieldDefinition.Getter, method, delegateType);

                if (expression.ReturnType == returnType)
                {
                    return(expression.ReturnType);
                }

                // we must return a type that both db query type and replacement type derive from.
                // we could find a common ancestor between them
                return(typeof(object));
            }

            if (parameters.Length == 0)
            {
                if (!returnType.IsSubclassOfGeneric(typeof(Expression <>)))
                {
                    throw new StemAttributeSetupException("A getter method with no parameters must return a strongly typed Expression<>.");
                }

                Type delegateType = typeof(Func <>).MakeGenericType(returnType);
                AddMethodToHandlerPart(FieldDefinition.Getter, method, delegateType);

                Type propertyValueType = ResolverTypeUtility.GetPropertyLambdaReturnType(ItemType, returnType);
                return(propertyValueType);
            }
            else
            {
                if (parameters.Length != 1)
                {
                    throw new StemAttributeSetupException("A getter method that does not return an expression must have one parameter for the item.");
                }

                if (parameters[0].ParameterType != ItemType)
                {
                    throw new StemAttributeSetupException("A getter method's first parameter must be of type " + ItemType.Name);
                }

                Type delegateType = typeof(Func <,>).MakeGenericType(ItemType, returnType);
                AddMethodToHandlerPart(FieldDefinition.Getter, method, delegateType);
                return(method.ReturnType);
            }
        }