小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

c# – 使用ConstructServicesUsing的AutoMapper自定義類(lèi)型轉(zhuǎn)換

 印度阿三17 2019-05-29

根據(jù)AutoMapper Documentation,我應(yīng)該能夠使用以下方法創(chuàng)建和使用自定義類(lèi)型轉(zhuǎn)換器的實(shí)例:

var dest = Mapper.Map<Source, Destination>(new Source { Value = 15 },
    opt => opt.ConstructServicesUsing(childContainer.GetInstance));

我有以下源和目標(biāo)類(lèi)型:

public class Source {
    public string Value1 { get; set; }
    public string Value2 { get; set; }
    public string Value3 { get; set; }
}

public class Destination {
    public int Value1 { get; set; }
    public DateTime Value2 { get; set; }
    public Type Value3 { get; set; }
}

以下類(lèi)型的轉(zhuǎn)換器:

public class DateTimeTypeConverter : ITypeConverter<string, DateTime> {
    public DateTime Convert(ResolutionContext context) {
        return System.Convert.ToDateTime(context.SourceValue);
    }
}

public class SourceDestinationTypeConverter : ITypeConverter<Source, Destination> {
    public Destination Convert(ResolutionContext context) {
        var dest = new Destination();
        // do some conversion
        return dest;
    }
}

這個(gè)簡(jiǎn)單的測(cè)試應(yīng)斷言其中一個(gè)日期屬性被正確轉(zhuǎn)換:

[TestFixture]
public class CustomTypeConverterTest {
    [Test]
    public void ShouldMap() {
        Mapper.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());
        Mapper.CreateMap<Source, Destination>().ConstructUsingServiceLocator();

        var destination =
        Mapper.Map<Source, Destination>(
        new Source { Value1 = "15", Value2 = "01/01/2000", }, 
            options => options.ConstructServicesUsing(
                type => new SourceDestinationTypeConverter())
        ); // exception is thrown here

        Assert.AreEqual(destination.Value2.Year, 2000);
    }
}

但是在斷言發(fā)生之前我已經(jīng)得到了一個(gè)異常:

System.InvalidCastException:無(wú)法將類(lèi)型為“SourceDestinationTypeConverter”的對(duì)象強(qiáng)制轉(zhuǎn)換為“Destination”.

我現(xiàn)在的問(wèn)題是,如何使用ConstructServicesUsing()使用自定義類(lèi)型轉(zhuǎn)換器?

解決方法:

我測(cè)試了這段代碼并使用以下代碼完成了這項(xiàng)工作:

public void TestMethod1()
    {
        Mapper.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());
        Mapper.CreateMap<string, Type>().ConvertUsing(new StringTypeConverter());
        Mapper.CreateMap<string, int>().ConvertUsing(new StringIntConverter());
        Mapper.CreateMap<Source, Destination>();

        var destination =
        Mapper.Map<Source, Destination>(
        new Source { Value1 = "15", Value2 = "01/01/2000", Value3 = "System.String" },
            options => options.ConstructServicesUsing(type => new SourceDestinationTypeConverter())
        );

        Assert.AreEqual(destination.Value2.Year, 2000);
    }

額外的轉(zhuǎn)換器:

 public class StringTypeConverter : ITypeConverter<string, Type>
{
    public Type Convert(ResolutionContext context)
    {
        return Type.GetType(context.SourceValue.ToString());
    }
}

public class StringIntConverter : ITypeConverter<string, int>
{
    public int Convert(ResolutionContext context)
    {
        return Int32.Parse(context.SourceValue.ToString());
    }
}

Automapper缺少String到Type和String到Int的映射.
此外,我不得不刪除以下行

Mapper.CreateMap<Source, Destination>().ConstructUsingServiceLocator();

并替換它

Mapper.CreateMap<Source, Destination>();

我不知道“ConstructUsingServiceLocator()”選項(xiàng),但是在這種情況下將它排除在外……(我不知道是否將其刪除會(huì)為你帶來(lái)其他問(wèn)題.到目前為止我還沒(méi)有使用過(guò)它使用Automapper時(shí)的選項(xiàng).)

請(qǐng)注意我必須添加“Value3”參數(shù),因?yàn)檗D(zhuǎn)換會(huì)失敗…將NULL值轉(zhuǎn)換為T(mén)ype可能非常難……(而且我不知道這里發(fā)生了什么樣的轉(zhuǎn)換… .)

來(lái)源:http://www./content-1-214251.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀(guān)點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多