tests/DotNetCommon.Test/Extensions/TypeExtensions/GettingPropertiesTests.cs

(完整代码) 1KB

namespace DotNetCommon.Test.Extensions.TypeExtensions
{
    using System.Linq;
    using System.Reflection;
    using NUnit.Framework;
    using Shouldly;
    using System;
    using DotNetCommon.Extensions;

    [TestFixture]
    public sealed class GettingPropertiesTests : Context
    {
        [Test]
        public void When_getting_valid_property_with_given_name()
        {
            PropertyInfo childIdProp;
            typeof(SampleChild).TryGetInstanceProperty("ChildId", out childIdProp).ShouldBeTrue();
            childIdProp.ShouldNotBeNull();
            childIdProp.PropertyType.ShouldBe(typeof(int));

            PropertyInfo privateChildNameProp;
            typeof(SampleChild).TryGetInstanceProperty("PrivateChildName", out privateChildNameProp).ShouldBeTrue();
            privateChildNameProp.ShouldNotBeNull();
            privateChildNameProp.PropertyType.ShouldBe(typeof(string));
        }

        [Test]
        public void When_getting_invalid_property_with_given_name()
        {
            PropertyInfo someProperty;
            typeof(SampleChild).TryGetInstanceProperty("foo", out someProperty).ShouldBeFalse();
            someProperty.ShouldBeNull();
        }

        [Test]
        public void When_getting_all_public_properties()
        {
            var allProps = typeof(SampleChild).GetInstanceProperties();
            allProps.ShouldNotBeNull();
            allProps.ShouldNotBeEmpty();
            allProps.Length.ShouldBe(8);

            var declaredProps = typeof(SampleChild).GetInstanceProperties(false);
            declaredProps.ShouldNotBeNull();
            declaredProps.ShouldNotBeEmpty();
            declaredProps.Length.ShouldBe(5);
        }
    }
}

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。