本文整理汇总了C#中MemberMapper类的典型用法代码示例。如果您正苦于以下问题:C# MemberMapper类的具体用法?C# MemberMapper怎么用?C# MemberMapper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MemberMapper类属于命名空间,在下文中一共展示了MemberMapper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MappingConditionIsRespectedForNestedCollectionMembers
//[TestMethod]
public void MappingConditionIsRespectedForNestedCollectionMembers()
{
var mapper = new MemberMapper();
int i = 10;
mapper.CreateMapProposal<SourceTypeCollection, DestinationTypeCollection>()
.ForMember(dest => dest.Nested).OnlyIf(src => i == 0)
.FinalizeMap();
var source = new SourceTypeCollection
{
Nested = new List<NestedSourceType>
{
new NestedSourceType
{
Foo = "Bla"
}
}
};
var result = mapper.Map<SourceTypeCollection, DestinationTypeCollection>(source);
Assert.IsNull(result.Nested);
//i = 0;
result = mapper.Map<SourceTypeCollection, DestinationTypeCollection>(source);
Assert.AreEqual("Bla", result.Nested.Single().Foo);
}
示例2: NestedPropertyWithCustomMappingWorks
public void NestedPropertyWithCustomMappingWorks()
{
var mapper = new MemberMapper();
mapper.CreateMap<Address, AddressDto>(src => new AddressDto
{
CountryName = src.Country.Name
});
var projection = mapper.Project<CustomerSingleAddress, CustomerSingleAddressDto>();
var method = projection.Compile();
var result = method(new CustomerSingleAddress
{
Address =
new Address
{
Country = new Country
{
Name = "X"
}
}
});
Assert.AreEqual("X", result.Address.CountryName);
}
示例3: WithListStringToWithListString
public void WithListStringToWithListString()
{
var mapper = new MemberMapper();
mapper.Options.Debug.DebugInformationEnabled = true;
var source = new WithListofString { Foo = new List<string> { "test" } };
var result = mapper.Map(source, new WithListofString());
Assert.IsTrue(object.ReferenceEquals(source.Foo, result.Foo));
var x = result;
result.Foo = x.Foo;
var newResult = mapper.Map(source, result);
Assert.IsTrue(object.ReferenceEquals(result, newResult));
Assert.IsTrue(object.ReferenceEquals(result.Foo, newResult.Foo));
Assert.AreEqual(1, newResult.Foo.Count);
var otherSource = new WithListofString { Foo = new List<string> { "test1" } };
mapper.Map(otherSource, result);
Assert.IsTrue(object.ReferenceEquals(result.Foo, newResult.Foo));
Assert.AreEqual(2, result.Foo.Count);
}
示例4: BaseClassMappingAndInheritedClassMappingIsRespected
public void BaseClassMappingAndInheritedClassMappingIsRespected()
{
var mapper = new MemberMapper();
mapper.Options.Strictness.ThrowWithoutCorrespondingSourceMember = true;
mapper.CreateMap<SourceBase, DestinationBase>(customMapping: src => new DestinationBase
{
Test = src.ID
});
mapper.CreateMap<SecondSourceInherited, SecondDestinationInherited>(customMapping: src => new SecondDestinationInherited
{
Bar = src.Foo
});
var result = mapper.Map<SecondSourceInherited, SecondDestinationInherited>(new SecondSourceInherited
{
ID = 10,
Foo = "test"
});
Assert.AreEqual(10, result.Test);
Assert.AreEqual("test", result.Bar);
}
示例5: MappingConditionIsRespectedForNestedMembers
public void MappingConditionIsRespectedForNestedMembers()
{
var mapper = new MemberMapper();
mapper.CreateMapProposal<SourceType, DestinationType>()
.ForMember(dest => dest.Nested).OnlyIf(src => src.ID == 0)
.FinalizeMap();
var source = new SourceType
{
ID = 10,
Name = "X",
Nested = new NestedSourceType
{
Foo = "Bla"
}
};
var result = mapper.Map<SourceType, DestinationType>(source);
Assert.IsNull(result.Nested);
source.ID = 0;
result = mapper.Map<SourceType, DestinationType>(source);
Assert.AreEqual("Bla", result.Nested.Foo);
}
示例6: ClearMapCacheIsRespected
public void ClearMapCacheIsRespected()
{
var mapper = new MemberMapper();
mapper.CreateMap<SourceType, DestinationType>(customMapping: src => new
{
ID = src.ID * 10,
Name = src.Name + src.Name
});
var source = new SourceType
{
ID = 10,
Name = "x"
};
var result = mapper.Map<DestinationType>(source);
Assert.AreEqual(100, result.ID);
Assert.AreEqual("xx", result.Name);
mapper.ClearMapCache();
result = mapper.Map<DestinationType>(source);
Assert.AreEqual(10, result.ID);
Assert.AreEqual("x", result.Name);
}
示例7: DefaultNestingDepthWorksAsExpected
public void DefaultNestingDepthWorksAsExpected()
{
var mapper = new MemberMapper();
var nested = new NestedA
{
Bar = "A",
Nested = new NestedB
{
Bar = "B",
Nested = new[]
{
new NestedC
{
Bar = "C",
Nested = new NestedD
{
Bar = "D"
}
}
}
}
};
var clone = mapper.DeepClone(nested);
Assert.IsNotNull(clone.Nested);
Assert.IsNotNull(clone.Nested.Nested);
Assert.IsNull(clone.Nested.Nested.Single().Nested);
}
示例8: ChangedNestingDepthWorksAsExpected
public void ChangedNestingDepthWorksAsExpected()
{
var mapper = new MemberMapper();
var nested = new NestedA
{
Bar = "A",
Nested = new NestedB
{
Bar = "B",
Nested = new[]
{
new NestedC
{
Bar = "C",
Nested = new NestedD
{
Bar = "D"
}
}
}
}
};
mapper.Options.Cloning.MaxCloneDepth = 1;
var clone = mapper.DeepClone(nested);
Assert.IsNotNull(clone.Nested);
Assert.IsNull(clone.Nested.Nested);
}
示例9: lock
/// <summary>
/// Returns a mapper for a certain profile, and creates it if the mapper does not exist yet.
/// </summary>
/// <param name="profile">The profile for which you want to have a mapper.</param>
/// <returns></returns>
public IMemberMapper this[string profile]
{
get
{
IMemberMapper mapper;
if (!mappers.TryGetValue(profile, out mapper))
{
mapper = new MemberMapper { Profile = profile };
mapper.MapRepository = this.MapRepository;
mapper.Options = this.Options;
lock (mappers)
{
if (!mappers.ContainsKey(profile))
{
CreateMapper(profile, mapper);
}
}
}
return mapper;
}
set
{
CreateMapper(profile, value);
}
}
示例10: RecursiveRelationshipsAreMappedCorrectly
public void RecursiveRelationshipsAreMappedCorrectly()
{
var map = new MemberMapper();
var source = new SourceType
{
ID = 10,
Children = new List<SourceType>
{
new SourceType
{
ID = 11,
},
new SourceType
{
ID = 12,
Children = new List<SourceType>
{
new SourceType
{
ID = 13
}
}
}
}
};
var result = map.Map<SourceType, DestinationType>(source);
}
示例11: CustomMappingIsRespected
public void CustomMappingIsRespected()
{
var mapper = new MemberMapper();
mapper.CreateMap<Customer, SimpleCustomerDto>(customMapping: c => new
{
FullName = c.FirstName + " " + c.LastName.ToString(),
AddressLine = c.Address.Street + " " + c.Address.HouseNumber,
EmailAddress = c.EmailAddress.Address
});
var customer = new Customer
{
FirstName = "Test",
LastName = "Test",
EmailAddress = new EmailAddress
{
Address = "[email protected]"
},
CustomerID = 1,
Address = new Address
{
Street = "test",
HouseNumber = "10",
ZipCode = "1111AB",
City = "test"
}
};
var dto = mapper.Map<Customer, SimpleCustomerDto>(customer);
}
示例12: ComplexTypeMappingRespectsExistingMapping
public void ComplexTypeMappingRespectsExistingMapping()
{
var mapper = new MemberMapper();
var proposed = mapper.CreateMap(typeof(ComplexSourceType), typeof(ComplexDestinationType),
(s, p, option) =>
{
if (s.Name == "Name")
{
option.IgnoreMember();
}
});
proposed.FinalizeMap();
var source = new ComplexSourceType
{
ID = 5,
Complex = new NestedSourceType
{
ID = 10,
Name = "test"
}
};
var destination = mapper.Map<ComplexSourceType, ComplexDestinationType>(source);
Assert.AreEqual(destination.ID, 5);
Assert.IsNotNull(destination.Complex);
Assert.AreNotEqual(destination.Complex.Name, source.Complex.Name);
}
示例13: NonNullableGetsMappedToNullableNonPrimitiveVT
public void NonNullableGetsMappedToNullableNonPrimitiveVT()
{
var mapper = new MemberMapper();
var result = mapper.Map<NonNullableSourceNonPrimitiveVT, NullableDestinationNonPrimitiveVT>(new NonNullableSourceNonPrimitiveVT { ID = 10 });
Assert.AreEqual(10, result.ID.Value);
}
示例14: InvokingMapWithoutParameterAsMapWithParameterThrows
public void InvokingMapWithoutParameterAsMapWithParameterThrows()
{
var mapper = new MemberMapper();
mapper.CreateMap<SourceType, DestinationType>();
var result = mapper.Map(new SourceType(), new DestinationType(), 1);
}
示例15: IntIsMappedToEnum
public void IntIsMappedToEnum()
{
var mapper = new MemberMapper();
var result = mapper.Map<IntClass, EnumClass>(new IntClass { Foo = 2 });
Assert.AreEqual(Foo.B, result.Foo);
}