Пример #1
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="metaType">The parent meta type.</param>
        /// <param name="mi">The method info.</param>
        public AttributedMetaFunction(AttributedMetaModel model, MethodInfo mi)
        {
            this.model      = model;
            this.methodInfo = mi;
            this.rowTypes   = _emptyTypes;

            this.functionAttrib = Attribute.GetCustomAttribute(mi, typeof(FunctionAttribute), false) as FunctionAttribute;
            System.Diagnostics.Debug.Assert(functionAttrib != null);

            // Gather up all mapped results
            ResultTypeAttribute[] attrs = (ResultTypeAttribute[])Attribute.GetCustomAttributes(mi, typeof(ResultTypeAttribute));
            if (attrs.Length == 0 && mi.ReturnType == typeof(IMultipleResults))
            {
                throw Error.NoResultTypesDeclaredForFunction(mi.Name);
            }
            else if (attrs.Length > 1 && mi.ReturnType != typeof(IMultipleResults))
            {
                throw Error.TooManyResultTypesDeclaredForFunction(mi.Name);
            }
            else if (attrs.Length <= 1 && mi.ReturnType.IsGenericType &&
                     (mi.ReturnType.GetGenericTypeDefinition() == typeof(IEnumerable <>) ||
                      mi.ReturnType.GetGenericTypeDefinition() == typeof(ISingleResult <>) ||
                      mi.ReturnType.GetGenericTypeDefinition() == typeof(IQueryable <>)))
            {
                Type elementType = TypeSystem.GetElementType(mi.ReturnType);
                this.rowTypes = new List <MetaType>(1)
                {
                    this.GetMetaType(elementType)
                }.AsReadOnly();
            }
            else if (attrs.Length > 0)
            {
                List <MetaType> rowTypes = new List <MetaType>();
                foreach (ResultTypeAttribute rat in attrs)
                {
                    Type     type = rat.Type;
                    MetaType mt   = this.GetMetaType(type);
                    // Only add unique meta types
                    if (!rowTypes.Contains(mt))
                    {
                        rowTypes.Add(mt);
                    }
                }
                this.rowTypes = rowTypes.AsReadOnly();
            }
            else
            {
                this.returnParameter = new AttributedMetaParameter(this.methodInfo.ReturnParameter);
            }

            // gather up all meta parameter
            ParameterInfo[] pis = mi.GetParameters();
            if (pis.Length > 0)
            {
                List <MetaParameter> mps = new List <MetaParameter>(pis.Length);
                for (int i = 0, n = pis.Length; i < n; i++)
                {
                    AttributedMetaParameter metaParam = new AttributedMetaParameter(pis[i]);
                    mps.Add(metaParam);
                }
                this.parameters = mps.AsReadOnly();
            }
            else
            {
                this.parameters = _emptyParameters;
            }
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="metaType">The parent meta type.</param>
        /// <param name="mi">The method info.</param>
        public AttributedMetaFunction(AttributedMetaModel model, MethodInfo mi) {
            this.model = model;
            this.methodInfo = mi;
            this.rowTypes = _emptyTypes;

            this.functionAttrib = Attribute.GetCustomAttribute(mi, typeof(FunctionAttribute), false) as FunctionAttribute;
            System.Diagnostics.Debug.Assert(functionAttrib != null);

            // Gather up all mapped results
            ResultTypeAttribute[] attrs = (ResultTypeAttribute[])Attribute.GetCustomAttributes(mi, typeof(ResultTypeAttribute));
            if (attrs.Length == 0 && mi.ReturnType == typeof(IMultipleResults)) {
                throw Error.NoResultTypesDeclaredForFunction(mi.Name);
            }
            else if (attrs.Length > 1 && mi.ReturnType != typeof(IMultipleResults)) {
                throw Error.TooManyResultTypesDeclaredForFunction(mi.Name);
            }
            else if (attrs.Length <= 1 && mi.ReturnType.IsGenericType &&
                     (mi.ReturnType.GetGenericTypeDefinition() == typeof(IEnumerable<>) ||
                      mi.ReturnType.GetGenericTypeDefinition() == typeof(ISingleResult<>) ||
                      mi.ReturnType.GetGenericTypeDefinition() == typeof(IQueryable<>))) {
                Type elementType = TypeSystem.GetElementType(mi.ReturnType);
                this.rowTypes = new List<MetaType>(1) { this.GetMetaType(elementType) }.AsReadOnly();
            }
            else if (attrs.Length > 0) {
                List<MetaType> rowTypes = new List<MetaType>();
                foreach (ResultTypeAttribute rat in attrs) {
                    Type type = rat.Type;
                    MetaType mt = this.GetMetaType(type);
                    // Only add unique meta types
                    if (!rowTypes.Contains(mt)) {
                        rowTypes.Add(mt);
                    } 
                }
                this.rowTypes = rowTypes.AsReadOnly();
            }
            else {
                this.returnParameter = new AttributedMetaParameter(this.methodInfo.ReturnParameter);
            }

            // gather up all meta parameter
            ParameterInfo[] pis = mi.GetParameters();
            if (pis.Length > 0) {
                List<MetaParameter> mps = new List<MetaParameter>(pis.Length);
                for (int i = 0, n = pis.Length; i < n; i++) {
                    AttributedMetaParameter metaParam = new AttributedMetaParameter(pis[i]);
                    mps.Add(metaParam);
                }
                this.parameters = mps.AsReadOnly();
            }
            else {
                this.parameters = _emptyParameters;
            }
        }
Пример #3
0
 public AttributedMetaFunction(MethodInfo method, FunctionAttribute attribute)
 {
     functionAttribute = attribute;
     methodInfo = method;
 }