简体   繁体   English

自动映射器将typeof对象转换为枚举

[英]automapper convert typeof object to enum

I would like to use Automapper to specify a column using ForMember(?) to be an enum value of the type of the object that is being mapped. 我想使用Automapper使用ForMember(?)指定一列作为要映射的对象类型的枚举值。

For example I have 例如我有

class base
class A : base
class B : base

enum objTypeEnum
{
    istypebase,
    istypea,
    istypeb
}

base constains a property 基地包含财产

objTypeEnum TypeEnum;

During the mapping I want to resolve the type of the object being mapped to the TypeEnum property: 在映射期间,我想解析要映射到TypeEnum属性的对象的类型:

.ForMember(dest => dest.TypeEnum, opt => opt.MapFrom()) .ForMember(目的地=> dest.TypeEnum,opt => opt.MapFrom())

Can't figure out the resolver, and how to use it. 无法确定解析器以及如何使用它。 Using the resolver I created, the MapFrom complains that it needs a source definition. 使用我创建的解析器,MapFrom抱怨它需要源定义。

Yes, you can ForMember method and you define an expression where the argument is the source . 是的,您可以使用ForMember方法,并定义一个以参数为source的表达式。 So, use the source to provide what you want to set on the destination property. 因此,使用源提供要在destination属性上设置的内容。

.ForMember(dest => dest.TypeEnum, 
           option => option.MapFrom(source => {
                // some logic here based on source object to convert it to enum
                // for sample:
                if (source.Prop == null)
                    return TypeEnum.A;
                else
                    return TypeEnum.B;
           }))

将枚举转换为列表<object><div id="text_translate"><p>首先,为了避免下意识的<em>“这是重复的”</em>陈述:关于序列化枚举有很多关于 SO 的问题。 但没有一个(我发现)处理具有描述的枚举,或者尝试将枚举转换为特定的 object。 (如果你能找到我错过的一个,那么我会接受这是重复的。)</p><p> 给定一个看起来像这样的枚举:</p><pre> public enum OptionType { [Description("Option A")] OptionA = 1, [Description("Option B")] OptionB = 2, Description("Option C")] OptionC= 3, }</pre><p> 我想将其转换为 JSON 字符串,如下所示:</p><pre> [ { "Id": "1", "Description": "Option A"}, { "Id": "2", "Description": "Option B"}, { "Id": "3", "Description": "Option C"} ]</pre><p> 我有这两个类:</p><pre> public class EnumList { public List&lt;EnumNameValue&gt; EnumNamesValues { get; set; } } public class EnumNameValue { public int Id { get; set; } public string Description { get; set; } }</pre><p> 我有一个看起来像这样的调用方法:</p><pre> EnumList enumList = EnumSerializer.EnumToJson&lt;OptionType&gt;();</pre><p> 最后(这是我需要帮助的地方):</p><pre> public static class EnumSerializer { public static EnumList EnumToJson&lt;T&gt;() where T: Enum { Type enumType = typeof(T); // &lt;--- WORKS.; Enum thisEnum1 = (T)Activator.CreateInstance(enumType); // &lt;--- DOES NOT WORK Enum thisEnum2 = (Enum)Activator,CreateInstance(enumType); // &lt;--- DOES NOT WORK // If I can get this far, I *THINK* I can figure it out from here } }</pre><p> 我已经有一个获取描述的 Enum 扩展,所以我不需要帮助。 我遇到的问题是thisEnum1和thisEnum2最终的值为0</p><p> <a href="https://i.stack.imgur.com/zRatA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zRatA.png" alt="在此处输入图像描述"></a></p><p> ( DistrictType是 Enum 的真实名称。)</p><p> 如何获得可以循环的枚举的实际实例?</p></div></object> - Convert an Enum to a List<object>

暂无
暂无

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

相关问题 AutoMapper - Map 从 object 到枚举 - AutoMapper - Map from object to enum 使用AutoMapper将DataRow转换为Object - Convert DataRow to Object with AutoMapper 使用自动映射器将 map 整个 object 用于枚举 - Use automapper to map entire object to an enum 自动映射器无法转换对象的类型 - Automapper can not convert type of object 自动映射器转换系统对象列表 - Automapper convert list of system object 将枚举转换为列表<object><div id="text_translate"><p>首先,为了避免下意识的<em>“这是重复的”</em>陈述:关于序列化枚举有很多关于 SO 的问题。 但没有一个(我发现)处理具有描述的枚举,或者尝试将枚举转换为特定的 object。 (如果你能找到我错过的一个,那么我会接受这是重复的。)</p><p> 给定一个看起来像这样的枚举:</p><pre> public enum OptionType { [Description("Option A")] OptionA = 1, [Description("Option B")] OptionB = 2, Description("Option C")] OptionC= 3, }</pre><p> 我想将其转换为 JSON 字符串,如下所示:</p><pre> [ { "Id": "1", "Description": "Option A"}, { "Id": "2", "Description": "Option B"}, { "Id": "3", "Description": "Option C"} ]</pre><p> 我有这两个类:</p><pre> public class EnumList { public List&lt;EnumNameValue&gt; EnumNamesValues { get; set; } } public class EnumNameValue { public int Id { get; set; } public string Description { get; set; } }</pre><p> 我有一个看起来像这样的调用方法:</p><pre> EnumList enumList = EnumSerializer.EnumToJson&lt;OptionType&gt;();</pre><p> 最后(这是我需要帮助的地方):</p><pre> public static class EnumSerializer { public static EnumList EnumToJson&lt;T&gt;() where T: Enum { Type enumType = typeof(T); // &lt;--- WORKS.; Enum thisEnum1 = (T)Activator.CreateInstance(enumType); // &lt;--- DOES NOT WORK Enum thisEnum2 = (Enum)Activator,CreateInstance(enumType); // &lt;--- DOES NOT WORK // If I can get this far, I *THINK* I can figure it out from here } }</pre><p> 我已经有一个获取描述的 Enum 扩展,所以我不需要帮助。 我遇到的问题是thisEnum1和thisEnum2最终的值为0</p><p> <a href="https://i.stack.imgur.com/zRatA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zRatA.png" alt="在此处输入图像描述"></a></p><p> ( DistrictType是 Enum 的真实名称。)</p><p> 如何获得可以循环的枚举的实际实例?</p></div></object> - Convert an Enum to a List<object> 使用 automapper 将枚举映射到枚举 - Map enum to enum with automapper 将枚举 object 转换为等效枚举 object - Convert enum object to equivalent enum object 将对象转换为通用类型-automapper静态扩展 - convert object to type generic - automapper static extension 使用Automapper将ExpandoObject转换为抽象对象 - Convert ExpandoObject to abstract object using Automapper
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM