コード例 #1
0
        public void FromExpression_CanGetMetadataFromBasicType()
        {
            var instance1 = new BasicType();
            var instance2 = new BasicType();

            var baseType = typeof(BasicType);

            // instance property as is
            {
                var instancePropertyExpressions = instance2.InstancePropertyExpressions().ToList();
                var baseInstanceProperty        = typeof(BasicType).GetProperty(nameof(BasicType.InstanceProperty), BindingFlags.Instance | BindingFlags.Public);

                var actual = SettingMetadata.FromExpression(instancePropertyExpressions.Last());

                AreSame(instance2, actual.Instance);
                AreEqual(baseInstanceProperty, actual.Member);
                AreEqual(nameof(BasicType.InstanceProperty), actual.MemberName);

                AssertDefault(actual);
            }

            // static property as is
            {
                var staticPropertyExpressions = instance2.StaticPropertyExpressions().ToList();
                var baseStaticProperty        = typeof(BasicType).GetProperty(nameof(BasicType.StaticProperty), BindingFlags.Static | BindingFlags.Public);

                var actual = SettingMetadata.FromExpression(staticPropertyExpressions.Last());

                IsNull(actual.Instance);
                AreEqual(baseStaticProperty, actual.Member);
                AreEqual(nameof(BasicType.StaticProperty), actual.MemberName);

                AssertDefault(actual);
            }

            // instance field as is
            {
                var instanceFieldExpressions = instance2.InstanceFieldExpressions().ToList();
                var baseInstanceField        = typeof(BasicType).GetField("_instanceField", BindingFlags.Instance | BindingFlags.NonPublic);

                var actual = SettingMetadata.FromExpression(instanceFieldExpressions.Last(), nonPublic: true);

                AreSame(instance2, actual.Instance);
                AreEqual(baseInstanceField, actual.Member);
                AreEqual("_instanceField", actual.MemberName);

                AssertDefault(actual);
            }

            // static field as is
            {
                var staticFieldExpressions = instance2.StaticFieldExpressions().ToList();
                var baseStaticField        = typeof(BasicType).GetField("_staticField", BindingFlags.Static | BindingFlags.NonPublic);

                var actual = SettingMetadata.FromExpression(staticFieldExpressions.Last(), nonPublic: true);

                IsNull(actual.Instance);
                AreEqual(baseStaticField, actual.Member);
                AreEqual("_staticField", actual.MemberName);

                AssertDefault(actual);
            }


            void AssertDefault(SettingMetadata actual)
            {
                // These cases are common to all tests.

                AreEqual(baseType, actual.Type);
                IsNull(actual.Prefix);
                AreEqual(Namespace, actual.Namespace);
                AreEqual(baseType.Name, actual.TypeName);
                IsTrue(actual.Validations.Empty());
                IsNull(actual.DefaultValue);
                IsNull(actual.ProviderName);
                AreEqual(SettingNameStrength.Inherit, actual.SettingNameStrength);
                AreEqual(PrefixHandling.Inherit, actual.PrefixHandling);
            }
        }