public void GetMemberInfo_LambdaExpression_Basic() { var info = (PropertyInfo)MemberExpressions.GetMemberInfo((ClassParent c) => c.Property1); Assert.AreEqual(typeof(ClassParent), info.ReflectedType); Assert.AreEqual(typeof(int), info.PropertyType); }
public void GetMemberInfo_DeepMemberChain_WithCast() { var memberInfo = MemberExpressions.GetMemberInfo <DeepClass>(c => ((string)c.String).Length); Assert.IsNotNull(memberInfo); Assert.AreEqual(MemberExpressions.GetMemberInfo <string>(c => c.Length), memberInfo); }
public void ClassWithProtectedProperties_ProtectedAccessorsShouldNotBeAvailable() { // Arrange: // const string altogetherProtectedProperty = "ImAltogetherProtected"; // Not expected to be available either as a source or destination property. const string propertyWithProtectedGetter = "MyGetterIsProtected"; var propertyWithProtectedSetter = MemberExpressions.GetMemberInfo <ClassWithProtectedProperties>(c => c.MySetterIsProtected).Name; var altogetherAccessibleProperty = MemberExpressions.GetMemberInfo <ClassWithProtectedProperties>(c => c.ImAltogetherAccessible).Name; var expectedSourcePropertyNames = new[] { propertyWithProtectedSetter, altogetherAccessibleProperty }; var expectedDestinationProperyNames = new[] { propertyWithProtectedGetter, altogetherAccessibleProperty }; // Act: var mappingCollection = new MappingCollection <ClassWithProtectedProperties, ClassWithProtectedProperties, object>(null); var availableSourcePropertyNames = mappingCollection.Unmapped.Source.Select(sourceMember => sourceMember.Name).ToArray(); var availableDestinationPropertyNames = mappingCollection.Unmapped.Destination.Select(destMember => destMember.Name).ToArray(); // Assert: Array.Sort(expectedSourcePropertyNames); Array.Sort(availableSourcePropertyNames); Assert.AreEqual(expectedSourcePropertyNames, availableSourcePropertyNames); Array.Sort(expectedDestinationProperyNames); Array.Sort(availableDestinationPropertyNames); Assert.AreEqual(expectedDestinationProperyNames, availableDestinationPropertyNames); }
public void GetMemberInfo_ClassParent_SimpleField1() { var info = (FieldInfo)MemberExpressions.GetMemberInfo <ClassParent>(c => c.Field1); Assert.IsNotNull(info); Assert.AreEqual(typeof(ClassParent), info.ReflectedType); Assert.AreEqual("Field1", info.Name); }
public void GetMemberInfo_InheritedMember_ReturnsInheritingClassType() { var instance = new ClassChild1(); const int number = 10; var info = (PropertyInfo)MemberExpressions.GetMemberInfo <ClassChild1>(c => c.Property1); Assert.AreEqual(typeof(ClassChild1), info.ReflectedType); info.SetValue(instance, number, null); Assert.AreEqual(number, instance.Property1); }
public void CreateAccessorChain_SingleElement() { var chain = MapperUtils.CreateAccessorChain(MemberExpressions.GetExpressionChain <ClassWithSeveralPropertiesDest>(c => c.Child)); Assert.IsNotNull(chain); var destination = new ClassWithSeveralPropertiesDest(); var child = new ChildClass(); chain.Set(destination, child); Assert.AreSame(child, destination.Child); }
public void Create_SimpleType_NamedMember() { var session = new BuildSession(null, null, new Random(0)); var obj = new SimpleType(); session.PushObject(new ObjectBuildRecord(typeof(SimpleType), obj, true)); session.PushMember(MemberExpressions.GetMemberInfo <SimpleType>(c => c.NamedMember)); Assert.AreEqual("NamedMember0", Generator.CreateRecord(typeof(string), null, session).Object); Assert.AreEqual("NamedMember1", Generator.CreateRecord(typeof(string), null, session).Object); Assert.AreEqual("NamedMember2", Generator.CreateRecord(typeof(string), null, session).Object); }
public void GetMemberChain_DeepMemberChain_WithCast() { var memberInfo = MemberExpressions.GetExpressionChain <DeepClass>(c => ((string)c.String).Length); var expected = new[] { MemberExpressions.GetMemberInfo <DeepClass>(c => c.String), MemberExpressions.GetMemberInfo <string>(c => c.Length) }; Assert.IsNotNull(memberInfo); Assert.AreEqual(expected, memberInfo); }
public void GetMemberInfo_InheritedMember_NewProperty_BaseStillAccessible() { var instance = new SpecificInterface(); const int number = 10; var info = (PropertyInfo)MemberExpressions.GetMemberInfo <IGeneralInterface>(c => c.Property1); Assert.AreEqual(typeof(IGeneralInterface), info.ReflectedType); Assert.AreEqual(typeof(object), info.PropertyType); info.SetValue(instance, number, null); Assert.AreEqual(number, instance.Property1); }
public void CreateAccessorChain_MultipleElements_WithConstruction_DeeperClass() { var resourceMapper = new ResourceMapper <object>(); resourceMapper.InitializeMap(); var chain = MapperUtils.CreateConstructingAccessorChain <object>(MemberExpressions.GetExpressionChain <DeeperClass>(c => c.DeepClass.Child.String), resourceMapper); Assert.IsNotNull(chain); var destination = new DeeperClass(); const string child = "teststring"; chain(destination, child, null); Assert.AreSame(child, destination.DeepClass.Child.String); }
public void ResolveSource_MappingCollection_ReturnsNullIfNoMemberFound() { var searchMember = MemberExpressions.GetMemberInfo <ClassWithSeveralPropertiesDest>(c => c.Property2); var members = new[] { MemberExpressions.GetMemberInfo <ClassWithSeveralPropertiesDest>(c => c.Property1), MemberExpressions.GetMemberInfo <ClassWithSeveralPropertiesDest>(c => c.Property3) }; var mappingCollection = new Mock <IMappingCollection <object, object, object> >(); mappingCollection.Setup(m => m.MemberResolvers).Returns(new PriorityList <IMemberResolver> { new IgnoreCaseNameMatcher() }); mappingCollection.Setup(m => m.Unmapped.Source).Returns(members); Assert.IsNull(mappingCollection.Object.ResolveSource(searchMember)); }
public IMappingCollection <TFrom, TTo, TContext> Set <TPropertyType, TGetterType>( Expression <Func <TTo, TPropertyType> > toExpression, Expression <Func <TFrom, TGetterType> > fromExpression, bool?remap = null) { var to = MemberExpressions.GetExpressionChain(toExpression); // This call should throw an error if toExpression is not a propery chain try { var from = MemberExpressions.GetExpressionChain(fromExpression); return(Set(to, typeof(TPropertyType), from, typeof(TGetterType), remap)); } catch (MemberExpressionException) { var fromDelegate = fromExpression.Compile(); return(Set(toExpression, (frm, t, context) => fromDelegate(frm), remap)); } }
public IMappingCollection <TFrom, TTo, TContext> Set <TPropertyType, TGetterType>( Expression <Func <TTo, TPropertyType> > toExpression, Func <TFrom, TTo, TContext, TGetterType> getter, bool?remap = null) { var toChain = MemberExpressions.GetExpressionChain(toExpression); if (!toChain.Last().IsWritable()) { throw new ArgumentException(string.Format("Target member {0} must be writeable", toChain)); } var setter = MapEntry(toChain); setter.DestinationType = typeof(TPropertyType); setter.SourceFunc = (MapperAction <TContext>)((from, to, context) => getter((TFrom)from, (TTo)to, context)); setter.SourceType = typeof(TGetterType); setter.SourceObjectType = MemberEntryType.Function; setter.Remap = remap ?? RequiresRemappingByDefault(typeof(TPropertyType), typeof(TGetterType), true); if (!setter.Remap) { VerifyReturnType(typeof(TPropertyType), typeof(TGetterType)); } return(this); }
public void ReturnType_Field() { Assert.AreEqual(typeof(int), MemberExpressions.GetMemberInfo <DemoClass>(c => c.Field).ReturnType()); }
private static MemberInfo GetSrcInfo(Expression <Func <ClassWithSeveralPropertiesSrcNullable, object> > expression) { return(MemberExpressions.GetExpressionChain(expression).First()); }
private static MemberInfo[] GetAllDestInfo(Expression <Func <ClassWithSeveralPropertiesDest, object> > expression) { return(MemberExpressions.GetExpressionChain(expression)); }
public IMappingCollection <TFrom, TTo, TContext> Ignore <TMemberType>(Expression <Func <TTo, TMemberType> > expression) { return(IgnoreMember(MemberExpressions.GetMemberInfo(expression))); }
public void GetAccessor_Field() { var accessor = MemberExpressions.GetMemberInfo <NormalPropertiesAndFields>(c => c.ValidField).GetAccessor(); Assert.IsInstanceOf <FieldAccessor>(accessor); }
public void GetAccessor_Property() { var accessor = MemberExpressions.GetMemberInfo <NormalPropertiesAndFields>(c => c.ValidProperty).GetAccessor(); Assert.IsInstanceOf <PropertyAccessor <NormalPropertiesAndFields, int> >(accessor); }
public void ReturnType_Property() { Assert.AreEqual(typeof(string), MemberExpressions.GetMemberInfo <DemoClass>(c => c.Property).ReturnType()); }
public void GetExpressionChain_NonPropertyOrFieldType_ThrowsException() { Assert.Throws <MemberExpressionException>(() => MemberExpressions.GetExpressionChain <DeepClass>(c => c.String.Length + 1)); }
public void GetExpressionChain_NonGeneric_NullMember_ThrowsException() { Assert.Throws <ArgumentNullException>(() => MemberExpressions.GetExpressionChain(null)); }
public void GetMemberInfo_Generic_NullMember_ThrowsException() { Assert.Throws <ArgumentNullException>(() => MemberExpressions.GetMemberInfo <DeepClass>(null)); }