public void Should_pick_a_ctor_which_best_matches() { Mapper.CreateMap<SourceClass, DestinationClass>(); var source = new SourceClass(); Mapper.Map<DestinationClass>(source); }
// [TestMethod] public void TestString() { SourceClass source = new SourceClass(); TargetClass target = new TargetClass(); BindingBase binding = new BindingBase(target, "Title", source, "Text", BindingMode.OneWay); binding.Bind(); source.Text = "Text!"; Assert.AreEqual( target.Title, source.Text ); }
public void TestListBinding2() { SourceClass source = new SourceClass(); TargetClass target = new TargetClass(); BindingBase binding = new BindingBase(target, "Items", source, "SourceItems", BindingMode.OneWay); source.SourceItems.Add("1"); binding.Bind(); Assert.IsTrue(target.Items[0] == "1"); source.SourceItems.Remove("1"); Assert.IsTrue(target.Items.Count == 0); }
public void Should_map_correctly() { var source = new SourceClass { Type = "Hello" }; var result = Mapper.Map<DestinationClass>(source); result.Type.ShouldEqual(source.Type); }
public void Converter() { TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(SourceClass)); var source = new SourceClass { FirstName = "First", LastName = "Last" }; var target = (TargetClass)typeConverter.ConvertFrom(source); Assert.Equal(string.Format("{0} {1}", source.FirstName, source.LastName), target.FullName); }
public void Converter() { TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(SourceClass)); var source = new SourceClass { Yes = "Y", No = "N" }; var target = (TargetClass)typeConverter.ConvertFrom(source); Assert.True(target.Yes); Assert.False(target.No); }
public static void TestConversion() { SourceClass source = new SourceClass(); TargetClass target = new TargetClass(); BindingBase binding = new BindingBase(target, "TargetInt", source, "SourceStr", BindingMode.OneWay); BindingBase binding2 = new BindingBase(target, "TargetStr", source, "SourceInt", BindingMode.OneWay); binding.Bind(); binding2.Bind(); source.SourceInt = 5; source.SourceStr = "4"; Assert.AreEqual(target.TargetInt, 4); Assert.AreEqual(target.TargetStr, "5"); }
public void Map() { TypeAdapterConfig.GlobalSettings.Clear(); TypeAdapterConfig.GlobalSettings.NewConfig<SourceClass, DestClass>() .IgnoreNullValues(true) .Compile(); var source = new SourceClass(); var dest = source.Adapt<DestClass>(); Assert.AreEqual("Hello", dest.Title); Assert.NotNull(dest.Sub); Assert.NotNull(dest.List); }
// [TestMethod] public void TestMethod1() { SourceClass source = new SourceClass( ); TargetClass target = new TargetClass( ); BindingBase binding = new BindingBase( target, "TargetStr", source, "SourceInt" ); BindingResult lastResult = null; binding.OnBinding += result => { lastResult = result; }; binding.Bind( ); target.TargetStr = "5"; Assert.IsTrue( source.SourceInt == 5 ); target.TargetStr = "invalid int"; Assert.IsTrue(source.SourceInt == 0); Assert.IsTrue( lastResult.hasConversionError ); }
public void MappingWithNoChangesReturnsFalse() { var src = new SourceClass { A = 1, B = true, C = "hello", Excluded = 42.0 }; var reference = new RefClass { A = 1, C = "hello" }; var dest = new DestClass(); bool changes = ObjectDeltaMapper.Map(src, reference, dest, "Excluded"); Assert.False(changes); }
public static void Main() { #region 委托 //1.普通委托 var print1 = new Print(delegatemethod); print1("这是普通方式创建的委托"); //2.匿名委托 Print print2 = delegate(string str) { Console.WriteLine(str); }; print2("这是匿名方式创建的委托"); //3.lambda委托 Print print3 = (string str) => { Console.WriteLine(str); }; print3("这是lambda方式创建的委托"); #endregion #region 事件 SourceClass source = new SourceClass("我的事件被触发"); source.Width = 5.0; source.Height = 3.0; //一、委托方式允许source.Clicked(source, new My_EventArgs("我使用委托方式调用的Args"));调用 source.Clicked = new SourceClass.EventHandler(source_RightClick); source.Clicked += new SourceClass.EventHandler(source_LeftClick); source.ClickedAsync(); source.Clicked(source, new My_EventArgs("我使用委托方式调用的Args")); //二、很明显用户希望通过使用source.ClickedAsync();去调用函数,而非直接使用上行代码去调用,所以在这里需要使用Event关键字进行申明 //注1.事件方式不允许source.Clicked(source, new My_EventArgs("我使用委托方式调用的Args"));直接调用 //注2.事件不允许 source.Click = new SourceClass.EventHandler(source_LeftClick);直接赋值 source.Click += new SourceClass.EventHandler(source_LeftClick); source.ClickAsync(); //注销事件和注册事件 source.Click -= new SourceClass.EventHandler(source_LeftClick); source.Click += new SourceClass.EventHandler(source_RightClick); source.ClickAsync(); Console.ReadLine(); #endregion }
public void Setup() { sourceTest = new SourceClass(); sourceTest.Name = "SourceTest"; sourceTest.Alias = new List <string>(); sourceTest.Alias.Add("SourceTestA"); sourceTest.Alias.Add("SourceTestB"); sourceTest.Lucky = new SourceSubClass() { LuckyNumbers = new List <int>() }; sourceTest.Lucky.LuckyNumbers.Add(7); sourceTest.Lucky.LuckyNumbers.Add(11); sourceTest.BirthDate = DateTime.Now.AddYears(-40); sourceMultipleTest = new SourceClass[2]; sourceMultipleTest[0] = sourceTest; sourceMultipleTest[1] = sourceTest; mapPath = Path.Combine(TestContext.CurrentContext.TestDirectory, MAP_FILE); GenerateMap(); }
public void MappingWithChangeCopiesChangedProperties() { var src = new SourceClass { A = 1, B = true, C = "hello", Excluded = 42.0 }; var reference = new RefClass { A = 4, C = "hello" }; var dest = new DestClass(); bool changes = ObjectDeltaMapper.Map(src, reference, dest, "Excluded"); Assert.True(changes); Assert.Equal(1, dest.A); Assert.Null(dest.C); }
public void SuffixNameAndTypeConventionTest() { var source = new SourceClass(); var target = new TargetClassDto(); var mapper = new Mapper(cfg => { cfg.Conventions.GetOrAdd <DefaultConvention>(convention => { convention.MatchingRules .GetOrAdd <TypeMatchingRule>(ruleConfig => ruleConfig.AllowImplicitConversions = true) .GetOrAdd <ExactNameMatching>(ruleConfig => ruleConfig.IgnoreCase = true) .GetOrAdd <SuffixMatching>(ruleConfig => ruleConfig.IgnoreCase = true); }); }); mapper.Map(source, target); Assert.IsTrue(source.A == target.ADto); Assert.IsTrue(source.B == target.BDataTransferObject); Assert.IsTrue(source.C == target.Cdto); Assert.IsTrue(source.D == target.Ddatatransferobject); Assert.IsTrue(source.E == target.E); }
public void Setup() { _sourceClass = new SourceClass { Age = 1, FirstName = "foo", NoOfDependants = 3, Title = "bar", }; _sourceDynamic = new { Age = 1, FirstName = "foo", NoOfDependants = 3, Title = "bar", }; _sourceStringDynamic = new { Age = "1", FirstName = "foo", NoOfDependants = "3", Title = "bar", }; }
public void ExactNameAndTypeConventionTest() { var source = new SourceClass(); var target = new TargetClass(); var config = new Configuration(cfg => { cfg.Conventions.GetOrAdd <DefaultConvention>(conventionConfig => { conventionConfig.MatchingRules.GetOrAdd <TypeMatchingRule>(ruleConfig => { ruleConfig.AllowImplicitConversions = false; ruleConfig.AllowExplicitConversions = false; }); }); }); var mapper = new Mapper(config); mapper.Map(source, target); Assert.IsTrue(source.A != target.A); Assert.IsTrue(source.B != target.B); }
public void Test1() { // arrange - configure the automapper Mapper.Initialize((config => { config.CreateMap <SourceClass, TargetClass>() .ForMember( dest => dest.ProgressBarCssClass, opt => opt.MapFrom(src => DetermineProgressBarState(src)) ); })); // arrange - create a var source = new SourceClass() { PercentComplete = 100 }; // act - map source to target var target = Mapper.Map <TargetClass>(source); // assert - verify the result target.ProgressBarCssClass.Should().Be("progress-bar-success"); }
// [TestMethod] public void TestMethod1() { SourceClass source = new SourceClass( ); TargetClass target = new TargetClass( ); BindingBase binding = new BindingBase( target, "S", source, "Str" ); binding.Adapter = new MyAdapter( ); binding.Bind(); source.Str = "123"; Assert.IsTrue( target.GetTargetStr( ) == "123" ); target.SetTargetStr( "456" ); Assert.IsTrue(source.Str == "456"); binding.Unbind( ); source.Str = "123"; Assert.IsTrue(target.GetTargetStr() == "456"); }
public void Should_pick_a_ctor_which_best_matches() { var source = new SourceClass(); Mapper.Map <DestinationClass>(source); }
public void AddSource(SourceClass tempSource) { sourceList.Add(tempSource.GetXrefName(), tempSource); }
public void AddSource(SourceClass tempSource) { }
public void SetUp() { _source = new SourceClass(); _mapper = new ObjectMapper(); }
} //Подписка на public abstract void AddSource(SourceClass source, string name);
//事件处理方法2 static void source_RightClick(object sender, My_EventArgs args) { SourceClass source = sender as SourceClass; Console.WriteLine("目标面积:" + source.Width * source.Height); }
public void NullSourcePropertyIsNotAChange() { var src = new SourceClass { A = null, C = "hello" }; var reference = new RefClass { A = 4, C = "hello" }; var dest = new DestClass(); bool changes = ObjectDeltaMapper.Map(src, reference, dest); Assert.False(changes); }
// [TestMethod] public void TestValidation( ) { SourceClass source = new SourceClass(); TargetClass target = new TargetClass(); BindingBase binding = new BindingBase(target, "TargetStr", source, "SourceInt", BindingMode.OneWay); binding.UpdateSourceIfBindingFails = false; binding.Bind(); target.TargetInt = 1; source.SourceStr = "invalid int"; Assert.IsTrue( target.TargetInt == 1 ); }
public void TestMethod1() { TargetClass target = new TargetClass( ); SourceClass source = new SourceClass( ); BindingBase binding = new BindingBase( target, "Text", source, "Val" ); binding.Converter = new DoubleToStringConverter( ); binding.Bind( ); source.Val = 3.0f; Assert.IsTrue( target.Text == "3" ); target.Text = "0.5"; Assert.IsTrue( source.Val == 0.5 ); }
public async Task <DestinationClass> DoWork(SourceClass source) { return(await mapper.Map <DestinationClass>(source)); }