public MutableConstructorInfo CreateConstructor( MutableType declaringType, MethodAttributes attributes, IEnumerable <ParameterDeclaration> parameters, Func <ConstructorBodyCreationContext, Expression> bodyProvider) { ArgumentUtility.CheckNotNull("declaringType", declaringType); ArgumentUtility.CheckNotNull("parameters", parameters); ArgumentUtility.CheckNotNull("bodyProvider", bodyProvider); MemberAttributesUtility.ValidateAttributes("constructors", MemberAttributesUtility.InvalidConstructorAttributes, attributes, "attributes"); var isStatic = attributes.IsSet(MethodAttributes.Static); var paras = parameters.ToList(); if (isStatic && paras.Count != 0) { throw new ArgumentException("A type initializer (static constructor) cannot have parameters.", "parameters"); } var signature = new MethodSignature(typeof(void), paras.Select(p => p.Type), 0); if (declaringType.AddedConstructors.Any(ctor => ctor.IsStatic == isStatic && MethodSignature.Create(ctor).Equals(signature))) { throw new InvalidOperationException("Constructor with equal signature already exists."); } var parameterExpressions = paras.Select(p => p.Expression); var context = new ConstructorBodyCreationContext(declaringType, isStatic, parameterExpressions); var body = BodyProviderUtility.GetTypedBody(typeof(void), bodyProvider, context); var attr = attributes.Set(MethodAttributes.SpecialName | MethodAttributes.RTSpecialName); return(new MutableConstructorInfo(declaringType, attr, paras, body)); }
public void SetBody(Func <ConstructorBodyModificationContext, Expression> bodyProvider) { ArgumentUtility.CheckNotNull("bodyProvider", bodyProvider); var context = new ConstructorBodyModificationContext((MutableType)DeclaringType, IsStatic, ParameterExpressions, _body); _body = BodyProviderUtility.GetTypedBody(typeof(void), bodyProvider, context); }
public void GetTypedBody_ThrowsForNullBody() { var bodyProvider = CreateBodyProvider(returnedBody: null); Assert.That( () => BodyProviderUtility.GetTypedBody(typeof(void), bodyProvider, _context), Throws.ArgumentException .With.ArgumentExceptionMessageEqualTo( "Provider must not return null.", "bodyProvider")); }
public void GetTypedBody_BoxingConversion() { var body = ExpressionTreeObjectMother.GetSomeExpression(typeof(int)); var bodyProvider = CreateBodyProvider(body); var result = BodyProviderUtility.GetTypedBody(typeof(object), bodyProvider, _context); var expectedBody = Expression.Convert(body, typeof(object)); ExpressionTreeComparer.CheckAreEqualTrees(expectedBody, result); }
private Expression GetMethodBody( MutableType declaringType, MethodAttributes attributes, Func <MethodBodyCreationContext, Expression> bodyProvider, MethodSignatureItems signatureItems, MethodInfo baseMethod) { var parameterExpressions = signatureItems.ParameterDeclarations.Select(pd => pd.Expression); var isStatic = attributes.IsSet(MethodAttributes.Static); var context = new MethodBodyCreationContext( declaringType, isStatic, parameterExpressions, signatureItems.GenericParameters.Cast <Type>(), signatureItems.ReturnType, baseMethod); var body = bodyProvider == null ? null : BodyProviderUtility.GetTypedBody(signatureItems.ReturnType, bodyProvider, context); return(body); }
public void SetBody(Func <MethodBodyModificationContext, Expression> bodyProvider) { ArgumentUtility.CheckNotNull("bodyProvider", bodyProvider); var context = new MethodBodyModificationContext( MutableDeclaringType, IsStatic, _parameterExpressions, _genericParameters.Cast <Type>(), ReturnType, _baseMethod, _body); var newBody = BodyProviderUtility.GetTypedBody(ReturnType, bodyProvider, context); var oldBody = _body; _body = newBody; if (BodyChanged != null) { BodyChanged(this, new BodyChangedEventArgs(oldBody, _body)); } }
public void GetTypedBody_ThrowsForNullBody() { var bodyProvider = CreateBodyProvider(returnedBody: null); BodyProviderUtility.GetTypedBody(typeof(void), bodyProvider, _context); }