草庐IT

my_enum_convert

全部标签

c# - MVC5 : Enum radio button with label as displayname

我有这些枚举publicenumQuestionStart{[Display(Name="Repeattillcommonmatchisfound")]RepeatTillCommonIsFound,[Display(Name="Repeatonce")]RepeatOnce,[Display(Name="Norepeat")]NoRepeat}publicenumQuestionEnd{[Display(Name="CancelInvitation")]CancelInvitation,[Display(Name="Planwithparticipantsonfirstavailab

C#:Enum.IsDefined 在组合标志上

我有这个枚举:[Flags]publicenumExportFormat{None=0,Csv=1,Tsv=2,Excel=4,All=Excel|Csv|Tsv}我正在尝试对这个(或任何,实际上)枚举进行包装,以通知更改。目前它看起来像这样:publicclassNotifyingEnum:INotifyPropertyChangedwhereT:struct{privateTvalue;publiceventPropertyChangedEventHandlerPropertyChanged;publicNotifyingEnum(){if(!typeof(T).IsEnum)th

c# - "Class of <T> where T : Enum"不工作

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:CreateGenericmethodconstrainingTtoanEnum为什么我们不能在C#中执行此操作?而且,如果可能的话,我怎样才能做类似的事情!我想要什么:publicclassATagwhereT:enum{[Somecode..]}publicclassclassBasewhereT:enum{publicIDictionarytags{get;set;}}因此,当需要调用它时,我很确定只会获得我的枚举值之一。publicclassAClassUsingTag:classBase{publi

c# - 将 VB 转换为 C# - My.Application.Info.DirectoryPath

以下VB(VB.NET、VisualBasic)语句的最佳C#(csharp)等价物是什么:My.Application.Info.DirectoryPathMy.Computer.ClipboardMy.Computer.Audio.PlaySystemSound()My.Application.Shutdown() 最佳答案 应用程序.ExecutablePath系统.Windows.Forms.剪贴板系统.媒体.*应用程序.退出 关于c#-将VB转换为C#-My.Applicati

c# - Convert.ToBoolean ("1") 在 C# 中抛出 System.Format 异常

为什么Convert.ToBoolean("1")抛出一个System.FormatException?我应该如何进行此转换? 最佳答案 是的,这是asdocumented:[throws]FormatException[if]valueisnotequaltoTrueStringorFalseString.TrueString为“True”,FalseString为“False”。如果你想检测一个字符串是否为“1”,使用这个代码:boolfoo=text=="1"; 关于c#-Conv

c# - 为什么编译器允许将 Convert.ToString() 分配给整数?

我不小心在我的代码中偶然发现了类似于下面的东西,它编译得很好,但在运行时显然是炸弹:dynamicfiftySixDynamic=56;intfiftySixInt=System.Convert.ToString(fiftySixDynamic);Convert.ToString()的每次重载都会返回一个字符串,所以这肯定不应该编译?这是怎么回事? 最佳答案 您正在使用dynamic作为Convert.ToString的输入。因为输入是动态,方法绑定(bind)和类型检查被推迟到运行时,所以编译器看不到唯一可能的返回类型是strin

c# - Enum.GetValues() 上的 OrderBy

我正在从enum中填充MVC4中的DropDownList,我想对枚举值从大到小进行排序。但是,似乎没有直接的方法来解决这个问题。目前,我正在使用此代码添加到字典中,键是ID,值是显示文本:varpriorities=Enum.GetValues(typeof(Models.Priority)).OfType().ToList();for(inti=priorities.Count-1;i>=0;i--){Models.Prioritypriority=priorities[i];prioritiesDictionary.Add((int)priority,"Priority"+((i

c# - 最好使用 int.Parse 或 Convert.ToInt32

这个问题在这里已经有了答案:关闭12年前。PossibleDuplicate:Whatsthemaindifferencebetweenint.Parse()andConvert.ToInt32我想知道使用Convert.ToInt32VSint.Parse的优点和缺点是什么。这里是我使用的语法示例:intmyPageSize=Convert.ToInt32(uxPageSizeUsersSelector.SelectedValue);intmyPageSize=int.Parse(uxPageSizeUsersSelector.SelectedValue);我也找到了这些文章,也许他

c# - Enum.GetValues 和 Enum.GetNames 之间的区别

我看到Enum.GetValues返回基本Array类型,Enum.GetNames返回一个string数组。但我不明白这是多么重要。无论如何,对于enum,值都是字符串。但是,还有一个相关的问题。DataGridViewComboBox列数据绑定(bind)到一个enum如果我给DataSource=Enum.GetValues,但是当我将Enum.GetNames作为DataSource时,它不会进行数据绑定(bind)。 最佳答案 GetValues将为枚举中的每个项目返回一个基础整数值数组。GetNames将返回枚举中项目名

c# - "Enum as immutable rich-object": is this an anti-pattern?

我经常看到并使用带有附加属性的枚举来做一些基本的事情,例如提供显示名称或描述:publicenumMovement{[DisplayName("TurnedRight")]TurnedRight,[DisplayName("TurnedLeft")][Description("Execute90degreeturntotheleft")]TurnedLeft,//...}并且有一组扩展方法来支持属性:publicstaticstringGetDisplayName(thisMovementmovement){...}publicstaticMovementGetNextTurn(thi