C# Convert an enum to other type of enum
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的更多相关文章
- 【转载】#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) ...
- Python笔记 #01# Convert Python values into any type
源:DataCamp datacamp 的 DAILY PRACTICE + 日常收集. How much is your $100 worth after 7 years? Guess the t ...
- TypeError: can't convert console.log(...) to primitive type
一.背景 火狐浏览器提示这个错误,谷歌没有. 二.出错代码 var eventHandlers = { 'succeeded': function(e){ console.log('send succ ...
- WPF -Enum的三种绑定方法
一.使用ObjectDataProvider <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentat ...
- c#中enum的用法小结
转自:http://blog.csdn.net/moxiaomomo/article/details/8056356 enums枚举是值类型,数据直接存储在栈中,而不是使用引用和真实数据的隔离方式来存 ...
- Enum Helper
public static class EnumHelper { #region get /// <summary> /// 获得枚举类型所包含的全部项的列表 /// </summa ...
- Entity Framework入门教程(16)---Enum
EF DbFirst模式中的枚举类型使用 这一节介绍EF DbFirst模式中的Enum(枚举类型),CodeFirst模式中的Enum会在以后的EF CoreFirst系列中介绍.EF5中添加了对E ...
- WPF一组Radio与enum绑定
工作中用到了一组RadioButton对应一组数据的情况,这个时候最好能将一组RadioButton绑定Enum. 步骤 1.MyControl库中Type.cs中定义Enum namespace M ...
- Swift Enum 枚举
前言 枚举是一种自定义的数据类型,在 Swift 中枚举类型拥有相当高的自由度.在 Swift 语言中枚举是一级类型,它拥有在其他语言中只有类才拥有的一些特性,比如实例方法,实例构造器等. 枚举声明的 ...
随机推荐
- 【Cocos2d TestCpp实例模仿一】-- ActionsTest
转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/25252539 CCActionInterval(持续性动作) 位置性变化动作以To结束 ...
- C++编程技术之 异常处理(上)
增强错误恢复能力是提高代码健壮性的最有力途径之一 之所以平时编写代码的时候不愿意去写错误处理,主要是由于这项工作及其无聊并可能导致代码膨胀,导致的结果就是本来就比较复杂的程序变得更加复杂.当然了,前面 ...
- Unity5UGUI 官方教程学习笔记(三)UI BUTTON
Button Interactable :为了避免与该按钮产生交互,可以设置它为false Transition: 管理按钮在正常情况 ,按下,经过时的显示状态 None 按钮整正常工作 但是在按 ...
- 基于jQuery带标题的图片3D切换焦点图
今天给大家分享一款基于jQuery带标题的图片3D切换焦点图.这款焦点图适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗. 实现的代码. htm ...
- Let's Format Css Documents
每次想参考一些好看网站的时候,打开css文档都是一行的,琢磨了下就自己写了块短短的代码,各路Java大神别笑我呀.^_^ 复制粘贴控制台的输出就好了.(瞬间觉得跟上大神的脚步了←_←) package ...
- 一周学会Mootools 1.4中文教程:(1)Dom选择器
利器: 君欲善其事须先利其器,好吧因为我们的时间比较紧迫,只有六天而已,那么六天的时间用死记硬背的方式学会Mt犹如天方夜谭,因此我们需要借鉴一下Editplus的素材栏帮我们记忆就好了,当我们需要用到 ...
- NodeJS 从0开始
查看Node 基本配置$ npm config ls -l $npm help install将展开install的help文档 初始化目录 npm init 根据提示完成 将生成package.js ...
- Windows Server 2012 R2 Standard序列号
备用一个吧,免得用起来的时候找不到. NB4WH-BBBYV-3MPPC-9RCMV-46XCB
- java开发工具比较(16个工具修订版)
1.JDK (Java Development Kit)Java开发工具集 SUN的Java不仅提了一个丰富的语言和运行环境,而且还提了一个免费的Java开发工具集(JDK).开发人员和最终用户可以利 ...
- java程序错误类型及异常处理
一.程序的错误类型 在程序设计中,无论规模是大是小,错误总是难免的.程序的设计很少有能够一次完成,没有错误的(不是指HelloWorld这样的程序,而是要实现一定的功能,具备一定实用价值的程序),在编 ...