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 语言中枚举是一级类型,它拥有在其他语言中只有类才拥有的一些特性,比如实例方法,实例构造器等. 枚举声明的 ...
随机推荐
- 素数距离问题_ny_24.java
素数距离问题 时间限制: 3000 ms | 内存限制: 65535 KB 难度: 2 描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距 ...
- html基础标签-1-pre预格式标签
pre预格式标签 code,tt标签 1 <!doctype html> 2 <html lang='zh-cn'> 3 <head> 4 <meta cha ...
- .aspx.cs传值与取值
1:.aspx中post传值 $.post("ABP_ExchangeRatelz.aspx", { option: "isdelete", Ori_Curre ...
- oracle 使用 decode函数 或 case when 实现行转列
----创建测试表 create table student_score( name varchar2(20), subject varchar2(20), score number(4,1) ); ...
- R与数据分析旧笔记(十四) 动态聚类:K-means
动态聚类:K-means方法 动态聚类:K-means方法 算法 选择K个点作为初始质心 将每个点指派到最近的质心,形成K个簇(聚类) 重新计算每个簇的质心 重复2-3直至质心不发生变化 kmeans ...
- ZOJ 3209 Treasure Map 精确覆盖
题目链接 精确覆盖的模板题, 把每一个格子当成一列就可以. S忘记初始化TLE N次, 哭晕在厕所...... #include<bits/stdc++.h> using namespac ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- python xlrd对excel的读取功能
工作簿 xlrd.open_workbook('test.xls') workbook.dump() workbook.nsheets workbook.sheets() workbook.sheet ...
- Canvas 雾玻璃
Demo http://lumixraku.github.io/demos/Fog/Fog.html Canvas tutorial 给大家安利一个学习Canvas的站点 http://www.htm ...
- sql server 修改表自增列的值
Create PROCEDURE [dbo].[SP_UpdateIdentityId] ( ) , @beforeId INT , @afterId INT ) AS BEGIN IF @befor ...