Ejemplo n.º 1
0
        public static T GetAttribute <T>(ICustomAttributeProvider attributeProvider, bool inherit) where T : Attribute
        {
            T[] attributes = GetAttributes <T>(attributeProvider, inherit);

            return(CollectionUtils.GetSingleItem(attributes, true));
        }
Ejemplo n.º 2
0
        public static bool IsPropertyIndexed(PropertyInfo property)
        {
            ValidationUtils.ArgumentNotNull(property, "property");

            return(!CollectionUtils.IsNullOrEmpty <ParameterInfo>(property.GetIndexParameters()));
        }
Ejemplo n.º 3
0
        private Expression BuildMethodCall(MethodBase method, Type type, ParameterExpression?targetParameterExpression, ParameterExpression argsParameterExpression)
        {
            ParameterInfo[] parametersInfo = method.GetParameters();

            Expression[]           argsExpression;
            IList <ByRefParameter> refParameterMap;

            if (parametersInfo.Length == 0)
            {
                argsExpression  = CollectionUtils.ArrayEmpty <Expression>();
                refParameterMap = CollectionUtils.ArrayEmpty <ByRefParameter>();
            }
            else
            {
                argsExpression  = new Expression[parametersInfo.Length];
                refParameterMap = new List <ByRefParameter>();

                for (int i = 0; i < parametersInfo.Length; i++)
                {
                    ParameterInfo parameter     = parametersInfo[i];
                    Type          parameterType = parameter.ParameterType;
                    bool          isByRef       = false;
                    if (parameterType.IsByRef)
                    {
                        parameterType = parameterType.GetElementType();
                        isByRef       = true;
                    }

                    Expression indexExpression = Expression.Constant(i);

                    Expression paramAccessorExpression = Expression.ArrayIndex(argsParameterExpression, indexExpression);

                    Expression argExpression = EnsureCastExpression(paramAccessorExpression, parameterType, !isByRef);

                    if (isByRef)
                    {
                        ParameterExpression variable = Expression.Variable(parameterType);
                        refParameterMap.Add(new ByRefParameter(argExpression, variable, parameter.IsOut));

                        argExpression = variable;
                    }

                    argsExpression[i] = argExpression;
                }
            }

            Expression callExpression;

            if (method.IsConstructor)
            {
                callExpression = Expression.New((ConstructorInfo)method, argsExpression);
            }
            else if (method.IsStatic)
            {
                callExpression = Expression.Call((MethodInfo)method, argsExpression);
            }
            else
            {
                Expression readParameter = EnsureCastExpression(targetParameterExpression !, method.DeclaringType);

                callExpression = Expression.Call(readParameter, (MethodInfo)method, argsExpression);
            }

            if (method is MethodInfo m)
            {
                if (m.ReturnType != typeof(void))
                {
                    callExpression = EnsureCastExpression(callExpression, type);
                }
                else
                {
                    callExpression = Expression.Block(callExpression, Expression.Constant(null));
                }
            }
            else
            {
                callExpression = EnsureCastExpression(callExpression, type);
            }

            if (refParameterMap.Count > 0)
            {
                IList <ParameterExpression> variableExpressions = new List <ParameterExpression>();
                IList <Expression>          bodyExpressions     = new List <Expression>();
                foreach (ByRefParameter p in refParameterMap)
                {
                    if (!p.IsOut)
                    {
                        bodyExpressions.Add(Expression.Assign(p.Variable, p.Value));
                    }

                    variableExpressions.Add(p.Variable);
                }

                bodyExpressions.Add(callExpression);

                callExpression = Expression.Block(variableExpressions, bodyExpressions);
            }

            return(callExpression);
        }
Ejemplo n.º 4
0
        // Token: 0x06000DA0 RID: 3488 RVA: 0x0004F534 File Offset: 0x0004D734
        private Expression BuildMethodCall(MethodBase method, Type type, [Nullable(2)] ParameterExpression targetParameterExpression, ParameterExpression argsParameterExpression)
        {
            ParameterInfo[] parameters = method.GetParameters();
            Expression[]    array;
            IList <ExpressionReflectionDelegateFactory.ByRefParameter> list;

            if (parameters.Length == 0)
            {
                array = CollectionUtils.ArrayEmpty <Expression>();
                list  = CollectionUtils.ArrayEmpty <ExpressionReflectionDelegateFactory.ByRefParameter>();
            }
            else
            {
                array = new Expression[parameters.Length];
                list  = new List <ExpressionReflectionDelegateFactory.ByRefParameter>();
                for (int i = 0; i < parameters.Length; i++)
                {
                    ParameterInfo parameterInfo = parameters[i];
                    Type          type2         = parameterInfo.ParameterType;
                    bool          flag          = false;
                    if (type2.IsByRef)
                    {
                        type2 = type2.GetElementType();
                        flag  = true;
                    }
                    Expression index       = Expression.Constant(i);
                    Expression expression  = Expression.ArrayIndex(argsParameterExpression, index);
                    Expression expression2 = this.EnsureCastExpression(expression, type2, !flag);
                    if (flag)
                    {
                        ParameterExpression parameterExpression = Expression.Variable(type2);
                        list.Add(new ExpressionReflectionDelegateFactory.ByRefParameter(expression2, parameterExpression, parameterInfo.IsOut));
                        expression2 = parameterExpression;
                    }
                    array[i] = expression2;
                }
            }
            Expression expression3;

            if (method.IsConstructor)
            {
                expression3 = Expression.New((ConstructorInfo)method, array);
            }
            else if (method.IsStatic)
            {
                expression3 = Expression.Call((MethodInfo)method, array);
            }
            else
            {
                expression3 = Expression.Call(this.EnsureCastExpression(targetParameterExpression, method.DeclaringType, false), (MethodInfo)method, array);
            }
            MethodInfo methodInfo = method as MethodInfo;

            if (methodInfo != null)
            {
                if (methodInfo.ReturnType != typeof(void))
                {
                    expression3 = this.EnsureCastExpression(expression3, type, false);
                }
                else
                {
                    expression3 = Expression.Block(expression3, Expression.Constant(null));
                }
            }
            else
            {
                expression3 = this.EnsureCastExpression(expression3, type, false);
            }
            if (list.Count > 0)
            {
                IList <ParameterExpression> list2 = new List <ParameterExpression>();
                IList <Expression>          list3 = new List <Expression>();
                foreach (ExpressionReflectionDelegateFactory.ByRefParameter byRefParameter in list)
                {
                    if (!byRefParameter.IsOut)
                    {
                        list3.Add(Expression.Assign(byRefParameter.Variable, byRefParameter.Value));
                    }
                    list2.Add(byRefParameter.Variable);
                }
                list3.Add(expression3);
                expression3 = Expression.Block(list2, list3);
            }
            return(expression3);
        }
Ejemplo n.º 5
0
        public static IList CreateAndPopulateList(Type listType, Action <IList, bool> populateList)
        {
            ValidationUtils.ArgumentNotNull(listType, "listType");
            ValidationUtils.ArgumentNotNull(populateList, "populateList");

            IList list;
            Type  collectionType;
            bool  isReadOnlyOrFixedSize = false;

            if (listType.IsArray)
            {
                // have to use an arraylist when creating array
                // there is no way to know the size until it is finised
                list = new List <object>();
                isReadOnlyOrFixedSize = true;
            }
            else if (ReflectionUtils.InheritsGenericDefinition(listType, typeof(ReadOnlyCollection <>), out collectionType))
            {
                Type readOnlyCollectionContentsType = collectionType.GetGenericArguments()[0];
                Type genericEnumerable   = ReflectionUtils.MakeGenericType(typeof(IEnumerable <>), readOnlyCollectionContentsType);
                bool suitableConstructor = false;

                foreach (ConstructorInfo constructor in listType.GetConstructors())
                {
                    IList <ParameterInfo> parameters = constructor.GetParameters();

                    if (parameters.Count == 1)
                    {
                        if (genericEnumerable.IsAssignableFrom(parameters[0].ParameterType))
                        {
                            suitableConstructor = true;
                            break;
                        }
                    }
                }

                if (!suitableConstructor)
                {
                    throw new Exception("Read-only type {0} does not have a public constructor that takes a type that implements {1}.".FormatWith(CultureInfo.InvariantCulture, listType, genericEnumerable));
                }

                // can't add or modify a readonly list
                // use List<T> and convert once populated
                list = (IList)CreateGenericList(readOnlyCollectionContentsType);
                isReadOnlyOrFixedSize = true;
            }
            else if (typeof(IList).IsAssignableFrom(listType))
            {
                if (ReflectionUtils.IsInstantiatableType(listType))
                {
                    list = (IList)Activator.CreateInstance(listType);
                }
                else if (listType == typeof(IList))
                {
                    list = new List <object>();
                }
                else
                {
                    list = null;
                }
            }
            else if (listType.IsGenericType && listType.GetGenericTypeDefinition() == typeof(IList <>))
            {
                list = CollectionUtils.CreateGenericList(ReflectionUtils.GetCollectionItemType(listType));
            }
            else
            {
                list = null;
            }

            if (list == null)
            {
                throw new Exception("Cannot create and populate list type {0}.".FormatWith(CultureInfo.InvariantCulture, listType));
            }

            populateList(list, isReadOnlyOrFixedSize);

            // create readonly and fixed sized collections using the temporary list
            if (isReadOnlyOrFixedSize)
            {
                if (listType.IsArray)
                {
                    list = ToArray(((List <object>)list).ToArray(), ReflectionUtils.GetCollectionItemType(listType));
                }
                else if (ReflectionUtils.InheritsGenericDefinition(listType, typeof(ReadOnlyCollection <>)))
                {
                    list = (IList)ReflectionUtils.CreateInstance(listType, list);
                }
            }

            return(list);
        }