/// <summary> /// Creates an instace of type <paramref name="nodeType"/>. /// </summary> /// <exception cref="ExpressionNodeInstantiationException"> /// Thrown if the <paramref name="parseInfo"/> or the <paramref name="additionalConstructorParameters"/> /// do not match expected constructor parameters of the <paramref name="nodeType"/>. /// </exception> public static IExpressionNode CreateExpressionNode( Type nodeType, MethodCallExpressionParseInfo parseInfo, object[] additionalConstructorParameters) { ArgumentUtility.CheckNotNull("nodeType", nodeType); ArgumentUtility.CheckTypeIsAssignableFrom("nodeType", nodeType, typeof(IExpressionNode)); ArgumentUtility.CheckNotNull("additionalConstructorParameters", additionalConstructorParameters); #if NETFX_CORE var constructors = nodeType.GetTypeInfo().DeclaredConstructors.Where(c => c.IsPublic).ToArray(); #else var constructors = nodeType.GetTypeInfo().GetConstructors().Where(c => c.IsPublic).ToArray(); #endif if (constructors.Length > 1) { var message = string.Format( "Expression node type '{0}' contains too many constructors. It must only contain a single constructor, allowing null to be passed for any optional arguments.", nodeType.FullName); throw new ArgumentException(message, "nodeType"); } object[] constructorParameterArray = GetParameterArray(constructors[0], parseInfo, additionalConstructorParameters); try { return((IExpressionNode)constructors[0].Invoke(constructorParameterArray)); } catch (ArgumentException ex) { var message = GetArgumentMismatchMessage(ex); throw new ExpressionNodeInstantiationException(message); } }
/// <summary> /// Registers the specific <paramref name="methods"/> with the given <paramref name="nodeType"/>. The given methods must either be non-generic /// or open generic method definitions. If a method has already been registered before, the later registration overwrites the earlier one. /// </summary> public void Register(IEnumerable <MethodInfo> methods, Type nodeType) { ArgumentUtility.CheckNotNull("methods", methods); ArgumentUtility.CheckNotNull("nodeType", nodeType); ArgumentUtility.CheckTypeIsAssignableFrom("nodeType", nodeType, typeof(IExpressionNode)); foreach (var method in methods) { if (method.IsGenericMethod && !method.IsGenericMethodDefinition) { var message = string.Format( "Cannot register closed generic method '{0}', try to register its generic method definition instead.", method.Name); throw new InvalidOperationException(message); } if (method.DeclaringType.GetTypeInfo().IsGenericType&& !method.DeclaringType.GetTypeInfo().IsGenericTypeDefinition) { var message = string.Format( "Cannot register method '{0}' in closed generic type '{1}', try to register its equivalent in the generic type definition instead.", method.Name, method.DeclaringType); throw new InvalidOperationException(message); } _registeredMethodInfoTypes[method] = nodeType; } }
/// <summary> Gets the native transaction.</summary> /// <typeparam name="TTransaction">The type of the transaction abstracted by this instance.</typeparam> public TTransaction To <TTransaction> () { // ReSharper disable NotResolvedInText - We use the generic parameter on purpose. ArgumentUtility.CheckTypeIsAssignableFrom("TTransaction", typeof(TTransaction), typeof(ClientTransaction)); // ReSharper restore NotResolvedInText return((TTransaction)(object)_wrappedInstance); }
public MethodCallExpressionTransformerAttribute(Type transformerType) { ArgumentUtility.CheckNotNull("transformerType", transformerType); ArgumentUtility.CheckTypeIsAssignableFrom("transformerType", transformerType, typeof(IExpressionTransformer <MethodCallExpression>)); _transformerType = transformerType; }
private void CheckQueryableType(Type queryableType) { ArgumentUtility.CheckTypeIsAssignableFrom("queryableType", queryableType, typeof(IQueryable)); var queryableTypeInfo = queryableType.GetTypeInfo(); if (!queryableTypeInfo.IsGenericTypeDefinition) { var message = string.Format( "Expected the generic type definition of an implementation of IQueryable<T>, but was '{0}'.", queryableType); throw new ArgumentException(message, "queryableType"); } #if NETFX_CORE var genericArgumentCount = queryableTypeInfo.GenericTypeParameters.Length; #else var genericArgumentCount = queryableTypeInfo.GetGenericArguments().Length; #endif if (genericArgumentCount != 1) { var message = string.Format( "Expected the generic type definition of an implementation of IQueryable<T> with exactly one type argument, but found {0} arguments on '{1}.", genericArgumentCount, queryableType); throw new ArgumentException(message, "queryableType"); } }
/// <summary> /// Initializes a new instance of the <see cref="QueryableBase<T>"/> class with a given <paramref name="provider"/> and /// <paramref name="expression"/>. This is an infrastructure constructor that must be exposed on subclasses because it is used by /// <see cref="DefaultQueryProvider"/> to construct queries around this <see cref="QueryableBase{T}"/> when a query method (e.g. of the /// <see cref="Queryable"/> class) is called. /// </summary> /// <param name="provider">The provider used to execute the query represented by this <see cref="QueryableBase{T}"/> and to construct /// queries around this <see cref="QueryableBase{T}"/>.</param> /// <param name="expression">The expression representing the query.</param> protected QueryableBase(IQueryProvider provider, Expression expression) { ArgumentUtility.CheckNotNull("provider", provider); ArgumentUtility.CheckNotNull("expression", expression); ArgumentUtility.CheckTypeIsAssignableFrom("expression", expression.Type, typeof(IEnumerable <T>)); _queryProvider = provider; Expression = expression; }
public MainSourceExpressionNode(string associatedIdentifier, Expression expression) { ArgumentUtility.CheckNotNullOrEmpty("associatedIdentifier", associatedIdentifier); ArgumentUtility.CheckNotNull("expression", expression); ArgumentUtility.CheckTypeIsAssignableFrom("expression.Type", expression.Type, typeof(IEnumerable)); QuerySourceType = expression.Type; QuerySourceElementType = ReflectionUtility.TryGetItemTypeOfClosedGenericIEnumerable(expression.Type) ?? typeof(object); AssociatedIdentifier = associatedIdentifier; ParsedExpression = expression; }
private void CheckQueryableType(Type queryableType) { ArgumentUtility.CheckTypeIsAssignableFrom("queryableType", queryableType, typeof(IQueryable)); if (!queryableType.IsGenericTypeDefinition) { var message = string.Format( "Expected the generic type definition of an implementation of IQueryable<T>, but was '{0}'.", queryableType.FullName); throw new ArgumentTypeException(message, "queryableType", typeof(IQueryable <>), queryableType); } var genericArgumentCount = queryableType.GetGenericArguments().Length; if (genericArgumentCount != 1) { var message = string.Format( "Expected the generic type definition of an implementation of IQueryable<T> with exactly one type argument, but found {0} arguments.", genericArgumentCount); throw new ArgumentTypeException(message, "queryableType", typeof(IQueryable <>), queryableType); } }
public CustomAttributeExpression(TypeReference attributeOwner, Type attributeType, int index, bool inherited) { ArgumentUtility.CheckNotNull("attributeOwner", attributeOwner); ArgumentUtility.CheckNotNull("attributeType", attributeType); ArgumentUtility.CheckTypeIsAssignableFrom("attributeOwner", attributeOwner.Type, typeof(ICustomAttributeProvider)); _attributeOwner = attributeOwner; _attributeType = attributeType; _index = index; _inherited = inherited; Expression getAttributesExpression = new ConvertExpression( _attributeType.MakeArrayType(), new VirtualMethodInvocationExpression( _attributeOwner, s_getCustomAttributesMethod, new TypeTokenExpression(_attributeType), new ConstReference(_inherited).ToExpression())); _getAttributeExpression = new LoadCalculatedArrayElementExpression(getAttributesExpression, new ConstReference(_index).ToExpression(), _attributeType); }
public TTransaction To <TTransaction> () { ArgumentUtility.CheckTypeIsAssignableFrom("TTransaction", typeof(TTransaction), typeof(TestTransaction)); return((TTransaction)(object)this); }
public void Succeed() { Type result = ArgumentUtility.CheckTypeIsAssignableFrom("arg", typeof(string), typeof(object)); Assert.That(result, Is.SameAs(typeof(string))); }
public void Succeed_Null() { Type result = ArgumentUtility.CheckTypeIsAssignableFrom("arg", null, typeof(object)); Assert.That(result, Is.Null); }
public void Fail() { ArgumentUtility.CheckTypeIsAssignableFrom("arg", typeof(object), typeof(string)); }