Sometimes, if we want to do convert between two enums, firstly, we may think about one way:

var myGender = (MyGender)(int)TheirGender.TheirMale;

Obviously, it's dangerous, if the int value of TheirMale in TheirGender doesn't match corresponding MyGender, it will fail to convert.

So, the solution may be to use switch case, one elegent method is to written as extensions.

We can simply use like  theirGender.ToMyGender()  to invoke.

Here is the solution:

public static class TheirGenderExtensions
{
public static MyGender ToMyGender(this TheirGender value)
{
// insert switch statement here
}
} public static class MyGenderExtensions
{
public static TheirGender ToTheirGender(this MyGender value)
{
// insert switch statement here
}
}

  

ref: http://stackoverflow.com/questions/1818131/convert-an-enum-to-another-type-of-enum

C# Convert an enum to other type of enum的更多相关文章

  1. 【转载】#458 Errors While Converting Between enum and Underlying Type

    You can convert to an enum value from its underlying type by casting the underlying type (e.g. int) ...

  2. Python笔记 #01# Convert Python values into any type

    源:DataCamp datacamp 的 DAILY PRACTICE  + 日常收集. How much is your $100 worth after 7 years? Guess the t ...

  3. TypeError: can't convert console.log(...) to primitive type

    一.背景 火狐浏览器提示这个错误,谷歌没有. 二.出错代码 var eventHandlers = { 'succeeded': function(e){ console.log('send succ ...

  4. WPF -Enum的三种绑定方法

    一.使用ObjectDataProvider <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentat ...

  5. c#中enum的用法小结

    转自:http://blog.csdn.net/moxiaomomo/article/details/8056356 enums枚举是值类型,数据直接存储在栈中,而不是使用引用和真实数据的隔离方式来存 ...

  6. Enum Helper

    public static class EnumHelper { #region get /// <summary> /// 获得枚举类型所包含的全部项的列表 /// </summa ...

  7. Entity Framework入门教程(16)---Enum

    EF DbFirst模式中的枚举类型使用 这一节介绍EF DbFirst模式中的Enum(枚举类型),CodeFirst模式中的Enum会在以后的EF CoreFirst系列中介绍.EF5中添加了对E ...

  8. WPF一组Radio与enum绑定

    工作中用到了一组RadioButton对应一组数据的情况,这个时候最好能将一组RadioButton绑定Enum. 步骤 1.MyControl库中Type.cs中定义Enum namespace M ...

  9. Swift Enum 枚举

    前言 枚举是一种自定义的数据类型,在 Swift 中枚举类型拥有相当高的自由度.在 Swift 语言中枚举是一级类型,它拥有在其他语言中只有类才拥有的一些特性,比如实例方法,实例构造器等. 枚举声明的 ...

随机推荐

  1. Linux下的经常使用性能查询命令top、vmstat、gprof、pidstat之对照

    (1)查看各个CPU核的使用情况 sudo top -d 1 进入之后,按1,会出现以下的CPU使用情况,当中us列反映了各个CPU核的使用情况,百分比大说明该核在进行紧张的任务. (2)查看哪个进程 ...

  2. 【原创】重绘winform的GroupBox

    功能:重绘winform的GroupBox,以便调整边框颜色和边框宽度 using System; using System.Collections.Generic; using System.Com ...

  3. MMDrawerController 的实践,已经实现,几行简单的代码实现侧栏

    学习方法,看readme,看给的Demo 看功能怎么实现的去模仿,个人感觉模仿是最快的学习方法 废话少说,上代码 导入MMDrawerController框架我就不多少了,之后做什么才是我们才关注的事 ...

  4. WPF使用Log4net.dll库的demo(转载加个人观点)

    原文地址:http://blog.csdn.net/linraise/article/details/50547149 配置文件解析地址:http://blog.csdn.net/pfe_nova/a ...

  5. leetcode 60. Permutation Sequence(康托展开)

    描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  6. Exception和RuntimeException

    public class RuntimeExceptionDemo01 { public static void main(String[] args) {     String string=&qu ...

  7. docker10件事

    docker – 你应该知道的10件事   容器并不是一个全新的技术,但这并不妨碍Docker如风暴一样席卷整个世界. 如果你在IT圈里,你一定听说过Docker.就算与其他热门技术,如:Puppet ...

  8. MySQL 设置数据库的隔离级别

    在会话级别设置隔离级别 1.read commited :set session transaction isolation level read committed; 2.repeatable re ...

  9. hdu4491 Windmill Animation(计算几何)

    Windmill Animation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  10. php正则表达式的基本语法

    简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具.我们可以在几乎所有的基于UNIX系统的工具中找到正则表达式的身影,例 如,vi编辑器,Perl或PHP脚本语言,以及awk或sed sh ...