public static PropertyInfo GetProperty <T, TProperty>(this T x, Expression <Func <T, TProperty> > propertySelector, PropertyAccessOptions accessOptions = PropertyAccessOptions.Both) { if (x is null) { throw new ArgumentNullException(nameof(x)); } if (propertySelector is null) { throw new ArgumentNullException(nameof(propertySelector)); } return(TypeVisit.GetProperty(propertySelector, accessOptions)); }
public void ExtensionsSinglePropertyTest() { var type = typeof(NormalPropertyClass); var members = type.GetMembers(); var property1 = members.Single(m => m.Name == nameof(NormalPropertyClass.PublicGetSet)); var property2 = members.Single(m => m.Name == nameof(NormalPropertyClass.PublicGet)); var property3 = members.Single(m => m.Name == nameof(NormalPropertyClass.PublicSet)); var x4 = TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicGetSet); var x5 = TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicGetSet, PropertyAccessOptions.Getters); var x6 = TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicGetSet, PropertyAccessOptions.Setters); x4.ShouldNotBeNull(); x5.ShouldNotBeNull(); x6.ShouldNotBeNull(); x4.ShouldBe(property1); x5.ShouldBe(property1); x6.ShouldBe(property1); var x7 = TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicGet); var x8 = TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicGet, PropertyAccessOptions.Getters); Assert.Throws <ArgumentException>(() => TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicGet, PropertyAccessOptions.Setters)); x7.ShouldNotBeNull(); x8.ShouldNotBeNull(); x7.ShouldBe(property2); x8.ShouldBe(property2); var x9 = TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicSet); var x10 = TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicSet, PropertyAccessOptions.Setters); Assert.Throws <ArgumentException>(() => TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicSet, PropertyAccessOptions.Getters)); x9.ShouldNotBeNull(); x10.ShouldNotBeNull(); x9.ShouldBe(property3); x10.ShouldBe(property3); }
public void FieldErrTest() { Assert.Throws <ArgumentException>(() => TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicField)); }