Esempio n. 1
0
        public void SetBody()
        {
            var declaringType     = MutableTypeObjectMother.Create();
            var attribtes         = MethodAttributes.Virtual; // Methods which have a base method must be virtual.
            var returnType        = typeof(object);
            var parameters        = ParameterDeclarationObjectMother.CreateMultiple(2);
            var baseMethod        = ReflectionObjectMother.GetSomeVirtualMethod(); // Base method must be virtual.
            var genericParameters = new[] { MutableGenericParameterObjectMother.Create() };
            var method            = MutableMethodInfoObjectMother.Create(
                declaringType, "Method", attribtes, returnType, parameters, baseMethod, genericParameters: genericParameters);

            var fakeBody = ExpressionTreeObjectMother.GetSomeExpression(typeof(int));
            Func <MethodBodyModificationContext, Expression> bodyProvider = ctx =>
            {
                Assert.That(ctx.DeclaringType, Is.SameAs(declaringType));
                Assert.That(ctx.IsStatic, Is.False);
                Assert.That(ctx.Parameters, Is.EqualTo(method.ParameterExpressions).And.Not.Empty);
                Assert.That(ctx.GenericParameters, Is.EqualTo(genericParameters));
                Assert.That(ctx.ReturnType, Is.SameAs(returnType));
                Assert.That(ctx.BaseMethod, Is.SameAs(baseMethod));
                Assert.That(ctx.PreviousBody, Is.SameAs(method.Body));

                return(fakeBody);
            };

            method.SetBody(bodyProvider);

            var expectedBody = Expression.Convert(fakeBody, returnType);

            ExpressionTreeComparer.CheckAreEqualTrees(expectedBody, method.Body);
        }
Esempio n. 2
0
        public void GetOverride_EqualBaseDefinition()
        {
            var baseDefinition = ReflectionObjectMother.GetSomeVirtualMethod().GetBaseDefinition();
            var candidate      = MutableMethodInfoObjectMother.Create(baseMethod: baseDefinition);

            var result = _finder.GetOverride(baseDefinition, new[] { candidate });

            Assert.That(result, Is.SameAs(candidate));
        }
Esempio n. 3
0
        public void Initialization()
        {
            var declaringType = MutableTypeObjectMother.Create();
            var name          = "abc";
            var attributes    = (MethodAttributes)7 | MethodAttributes.Virtual;
            var returnType    = ReflectionObjectMother.GetSomeType();
            var parameters    = ParameterDeclarationObjectMother.CreateMultiple(2);
            var baseMethod    = ReflectionObjectMother.GetSomeVirtualMethod();
            var body          = ExpressionTreeObjectMother.GetSomeExpression(returnType);

            var method = new MutableMethodInfo(
                declaringType, name, attributes, new MutableGenericParameter[0], returnType, parameters.AsOneTime(), baseMethod, body);

            Assert.That(method.DeclaringType, Is.SameAs(declaringType));
            Assert.That(method.MutableDeclaringType, Is.SameAs(declaringType));
            Assert.That(method.Name, Is.EqualTo(name));
            Assert.That(method.Attributes, Is.EqualTo(attributes));
            Assert.That(method.IsGenericMethod, Is.False);

            CustomParameterInfoTest.CheckParameter(method.ReturnParameter, method, -1, null, returnType, ParameterAttributes.None);
            Assert.That(method.MutableReturnParameter, Is.SameAs(method.ReturnParameter));

            var actualParameters = method.GetParameters();

            Assert.That(actualParameters, Has.Length.EqualTo(2));
            CustomParameterInfoTest.CheckParameter(actualParameters[0], method, 0, parameters[0].Name, parameters[0].Type, parameters[0].Attributes);
            CustomParameterInfoTest.CheckParameter(actualParameters[1], method, 1, parameters[1].Name, parameters[1].Type, parameters[1].Attributes);
            Assert.That(method.MutableParameters, Is.EqualTo(actualParameters));

            var paramExpressions = method.ParameterExpressions;

            Assert.That(paramExpressions, Has.Count.EqualTo(2));
            Assert.That(paramExpressions[0], Has.Property("Name").EqualTo(parameters[0].Name).And.Property("Type").SameAs(parameters[0].Type));
            Assert.That(paramExpressions[1], Has.Property("Name").EqualTo(parameters[1].Name).And.Property("Type").SameAs(parameters[1].Type));

            Assert.That(method.BaseMethod, Is.SameAs(baseMethod));
            Assert.That(method.Body, Is.SameAs(body));
        }