Пример #1
0
        public void ModifyingNonVirtualAndStaticAndFinalMethods_Throws()
        {
            var nonVirtualMethod = NormalizingMemberInfoFromExpressionUtility.GetMethod((DomainType obj) => obj.PublicMethod());
            var staticMethod     = NormalizingMemberInfoFromExpressionUtility.GetMethod(() => DomainType.PublicStaticMethod());
            var finalMethod      = NormalizingMemberInfoFromExpressionUtility.GetMethod((DomainType obj) => obj.FinalMethod());

            var type = AssembleType <DomainType> (
                proxyType =>
            {
                var message = "Only virtual methods can be overridden.";
                Assert.That(
                    () => proxyType.GetOrAddOverride(nonVirtualMethod),
                    Throws.TypeOf <ArgumentException>().With.Message.StartsWith(message));

                Assert.That(
                    () => proxyType.GetOrAddOverride(staticMethod),
                    Throws.TypeOf <ArgumentException>().With.Message.StartsWith(message));

                Assert.That(
                    () => proxyType.GetOrAddOverride(finalMethod),
                    Throws.TypeOf <NotSupportedException>().With.Message.EqualTo("Cannot override final method 'DomainType.FinalMethod'."));
            });

            var instance = (DomainType)Activator.CreateInstance(type);

            Assert.That(instance.PublicMethod(), Is.EqualTo(12));

            var method = type.GetMethod("PublicStaticMethod", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);

            Assert.That(method.Invoke(null, null), Is.EqualTo(13));
        }