简体   繁体   English

如果字段类型没有默认构造函数,如何使用ValueResolver?

[英]How to use ValueResolver if field type does not have a default constructor?

I have model with property: 我有财产模型:

public class MyModel{
       public SelectList PropertyTypeList { get; set; }
}

And I have ValueResolver 我有ValueResolver

public class MyPropertyValueResolver : ValueResolver<ProductProperty, SelectList>
{
    protected override SelectList ResolveCore(ProductProperty source)
    {
        myList = .......;
        return new SelectList(myList, "Value", "Text");
    }
}

Then I configure mapping 然后我配置映射

    Mapper.CreateMap<Source, Destination>()
          .ForMember(s => s.PropertyTypeList, opt => opt.ResolveUsing<MyPropertyValueResolver>());

But it says me that 但它告诉了我

Type 'System.Web.Mvc.SelectList' does not have a default constructor 

What I should to do to make it work? 我应该做些什么才能让它发挥作用?

Rather than automapping to a SelectList, have you considered automapping to a simple Array, and then using a Get-only property to wrap this as a SelectList? 您是否考虑自动化到一个简单的数组,然后使用Get-only属性将其包装为SelectList,而不是自动转移到SelectList?

This answer describes the approach. 这个答案描述了这种方法。

Also, from the same SO question, there is the ConstructedBy idea, as well as a way to use MapFrom to do this directly. 此外,从相同的SO问题,有ConstructedBy的想法,以及使用MapFrom直接执行此操作的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Autofac:类型“ MyController”没有默认的构造函数 - Autofac: Type 'MyController' does not have a default constructor 自动映射错误:类型没有默认构造函数 - Automapping error: The type does not have a default constructor 如何模拟自定义 ValueResolver 构造函数参数 - How to mock custom ValueResolver constructor parameters 类型&#39;MvcApplication1.Controllers.ValuesController&#39;没有默认的构造函数 - Type 'MvcApplication1.Controllers.ValuesController' does not have a default constructor 类型&#39;Project1.DTO.MailDTO&#39;没有默认的构造函数 - Type 'Project1.DTO.MailDTO' does not have a default constructor struct 有默认构造函数吗? - Does struct have default constructor? 其余例外:无法找到用于类型“ myclass”的构造函数。 一个类应具有默认构造函数 - Rest exception: Unable to find a constructor to use for type “myclass”. A class should either have a default constructor int字段的默认值是0吗? - Does an int field have a default of 0? 如何添加默认构造函数并让它调用另一个构造函数并使用默认值? - How can I add a default constructor and have it call another constructor and use the default values? 窗口的默认构造函数是否必须公开? - Does a window's default constructor have to be public?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM