예제 #1
0
 public SimpleArgBuilder(ParameterInfo info, int index)
     : this(info, info.ParameterType, index, info.IsParamArray(), info.IsParamDictionary()) {
 }
예제 #2
0
 /// <summary>
 /// 检查方法参数。
 /// </summary>
 /// <param name="parameter">要检查的方法参数。</param>
 /// <param name="type">方法实参类型。</param>
 /// <param name="optionalParamBinding">是否对可选参数进行绑定。</param>
 /// <param name="isExplicit">类型检查时,如果考虑显式类型转换,则为 <c>true</c>;
 /// 否则只考虑隐式类型转换。</param>
 /// <param name="convertRefType">是否允许对按引用传递的类型进行类型转换。</param>
 /// <returns>如果方法参数与实参兼容,则为 <c>true</c>;否则为 <c>false</c>。</returns>
 private static bool CheckParameter(ParameterInfo parameter, Type type, bool optionalParamBinding,
     bool isExplicit, bool convertRefType)
 {
     Type paramType = parameter.ParameterType;
     if (paramType.ContainsGenericParameters)
     {
         return true;
     }
     if (type == typeof(Missing))
     {
         // 检查可选参数和 params 参数。
         return parameter.IsParamArray() || (optionalParamBinding && parameter.HasDefaultValue);
     }
     bool isByRef = false;
     if (paramType.IsByRef)
     {
         paramType = paramType.GetElementType();
         isByRef = true;
     }
     if (type == null)
     {
         if (isByRef && !convertRefType)
         {
             return false;
         }
         // 检查引用类型。
         return !paramType.IsValueType;
     }
     if (type.IsByRef)
     {
         if (isByRef)
         {
             return type.GetElementType() == paramType;
         }
         if (!convertRefType)
         {
             return false;
         }
         type = type.GetElementType();
     }
     return paramType.IsConvertFrom(type, isExplicit);
 }
예제 #3
0
        public override Expression Convert(DynamicMetaObject metaObject, Type restrictedType, ParameterInfo info, Type toType)
        {
            if (toType.IsAssignableFrom(restrictedType))
                return AstUtils.Convert(metaObject.Expression, toType);

            if (info.IsParamArray() && typeof(IList<object>).IsAssignableFrom(metaObject.LimitType))
            {
                return GetDynamicConversion(metaObject.Expression, toType);
            }

            if (toType == typeof(string))
            {
                return GetDynamicConversion(metaObject.Expression, toType);
            }

            return base.Convert(metaObject, restrictedType, info, toType);
        }