我有一个枚举:
public enum MyColours
{
Red,
Green,
Blue,
Yellow,
Fuchsia,
Aqua,
Orange
}
Run Code Online (Sandbox Code Playgroud)
我有一个字符串:
string colour = "Red";
Run Code Online (Sandbox Code Playgroud)
我希望能够回归:
MyColours.Red
Run Code Online (Sandbox Code Playgroud)
从:
public MyColours GetColour(string colour)
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有:
public MyColours GetColours(string colour)
{
string[] colours = Enum.GetNames(typeof(MyColours));
int[] values = Enum.GetValues(typeof(MyColours));
int i;
for(int i = 0; i < colours.Length; i++)
{
if(colour.Equals(colours[i], StringComparison.Ordinal)
break;
}
int value = values[i];
// I know all the information about the matched enumeration
// but how do i convert this information into returning a
// …Run Code Online (Sandbox Code Playgroud) 我想要做的是这样的:我有枚举标记值的枚举.
public static class EnumExtension
{
public static bool IsSet<T>( this T input, T matchTo )
where T:enum //the constraint I want that doesn't exist in C#3
{
return (input & matchTo) != 0;
}
}
Run Code Online (Sandbox Code Playgroud)
那么我可以这样做:
MyEnum tester = MyEnum.FlagA | MyEnum.FlagB
if( tester.IsSet( MyEnum.FlagA ) )
//act on flag a
Run Code Online (Sandbox Code Playgroud)
不幸的是,C#的通用,其中约束没有枚举限制,只有类和结构.C#不会将枚举视为结构(即使它们是值类型),因此我无法像这样添加扩展类型.
有没有人知道解决方法?
我正在读取文件内容,并在这样的确切位置获取字符串
string fileContentMessage = File.ReadAllText(filename).Substring(411, 3);
Run Code Online (Sandbox Code Playgroud)
输出将始终为Ok或Err
在另一边我MyObject这有ContentEnum这样的
public class MyObject
{
public enum ContentEnum { Ok = 1, Err = 2 };
public ContentEnum Content { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在,在客户端我想使用这样的代码(将我的字符串转换fileContentMessage为Content属性)
string fileContentMessage = File.ReadAllText(filename).Substring(411, 3);
MyObject myObj = new MyObject ()
{
Content = /// ///,
};
Run Code Online (Sandbox Code Playgroud) 我经常想知道为什么C#还没有实现Generic Enum.Parse
可以说我有
enum MyEnum
{
Value1,
Value2
}
Run Code Online (Sandbox Code Playgroud)
从XML文件/数据库条目我想创建一个枚举.
MyEnum val = (MyEnum)Enum.Parse(typeof(MyEnum), "value1", true);
Run Code Online (Sandbox Code Playgroud)
难道它没有被实现为类似的东西
MyEnum cal = Enum.Parse<MyEnum>("value1");
Run Code Online (Sandbox Code Playgroud)
这似乎是一个小问题,但它似乎是一个被忽视的问题.
有什么想法吗?
我有一个枚举:
public enum Color
{
Red,
Blue,
Green,
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我将这些颜色作为XML文件中的文字字符串读取,我该如何将其转换为枚举类型Color.
class TestClass
{
public Color testColor = Color.Red;
}
Run Code Online (Sandbox Code Playgroud)
现在,当使用像这样的文字字符串设置该属性时,我会收到编译器发出的非常严厉的警告.:D无法从字符串转换为颜色.
有帮助吗?
TestClass.testColor = collectionofstrings[23].ConvertToColor?????;
Run Code Online (Sandbox Code Playgroud) 可能重复:
创建将T限制为枚举的通用方法
我们有什么理由不能在C#中做到这一点吗?而且,如果可能的话,我该怎么做类似的事情!
我想要的是 :
public class<T> ATag where T : enum {
[Some code ..]
}
public class<T> classBase where T : enum {
public IDictionary<T, string> tags { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
所以,当它到了调用它的时候,我只能得到一个我的枚举值.
public class AClassUsingTag : classBase<PossibleTags> {
public void AMethod(){
this.tags.Add(PossibleTags.Tag1, "Hello World!");
this.tags.Add(PossibleTags.Tag2, "Hello Android!");
}
}
public enum PossibleTags {
Tag1, Tag2, Tag3
}
Run Code Online (Sandbox Code Playgroud)
错误信息 : "Constraint cannot be special class 'System.Enum'"
谢谢!
关于将字符串转换为枚举值有很多问题.一般来说,答案看起来像这个问题的答案:
StatusEnum MyStatus = (StatusEnum) Enum.Parse( typeof(StatusEnum), "Active", true );
Run Code Online (Sandbox Code Playgroud)
虽然这是一个非常合理的答案,并且您可以编写一个方法来简化调用,但它并没有回答为什么 Enum.Parse()返回一个object而不是相应的枚举值的问题.为什么我要把它投到StatusEnum?
编辑:
基本上,问题是为什么这样的函数不是Enum类的一部分?
public static T Parse<T>(string value) where T: struct
{
return (T)Enum.Parse(typeof (T), value);
}
Run Code Online (Sandbox Code Playgroud)
这个功能完美无缺,完全符合您的期望. StatusEnum e = Enum.Parse<StatusEnum>("Active");.
我正在DataGridView中绘制我的行,如下所示:
private void AdjustColors()
{
foreach (DataGridViewRow row in aufgabenDataGridView.Rows)
{
AufgabeStatus status = (AufgabeStatus)Enum.Parse(typeof(AufgabeStatus), (string)row.Cells["StatusColumn"].Value);
switch (status)
{
case (AufgabeStatus.NotStarted):
row.DefaultCellStyle.BackColor = Color.LightCyan;
break;
case (AufgabeStatus.InProgress):
row.DefaultCellStyle.BackColor = Color.LemonChiffon;
break;
case (AufgabeStatus.Completed):
row.DefaultCellStyle.BackColor = Color.PaleGreen;
break;
case (AufgabeStatus.Deferred):
row.DefaultCellStyle.BackColor = Color.LightPink;
break;
default:
row.DefaultCellStyle.BackColor = Color.White;
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我在OnLoad方法中调用它:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
AdjustColors();
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢OnLoad到OnPaint或者其他东西......因为OnPaint经常被调用.
问题:为什么改变每行的背景需要大约100-200毫秒?早,我是doint CellPaint ..但滚动时刷新我有问题..
我正在开发Azure移动服务,在我的模型中,一些关系是可选的,使得表示它的属性可以为空.
例如,我的模型类中的Message实体是这样的:
public partial class Message
{
public Message()
{
this.Messages = new HashSet<Message>();
}
public int Id { get; set; }
public int CreatedById { get; set; }
public int RecipientId { get; set; }
public Nullable<int> ParentId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int MessageTypeId { get; set; }
public Nullable<MessageType> Type { get; set; }
public Nullable<bool> Draft { get; set; }
public Nullable<bool> …Run Code Online (Sandbox Code Playgroud) 我正在创建 ASP.NET Core Web API。
我想要一个像这样的帖子正文:
{
"Name": "XYX",
"Status":"Waiting"
}
Run Code Online (Sandbox Code Playgroud)
状态是一个枚举:
public enum Status
{
[Description("Waiting")]
Waiting = 1,
[Description("Occuring")]
Occuring = 2,
[Description("Stopping")]
Stopping = 3,
}
Run Code Online (Sandbox Code Playgroud)
我应该怎么做才能不出现错误:
The JSON value could not be converted to PostgreSql.Enums.StatusEnum. Path:
Run Code Online (Sandbox Code Playgroud)
PS:尝试了这里提到的方法,但它对我不起作用。
c# ×10
enums ×7
string ×3
.net ×1
asp.net-core ×1
automapper ×1
casting ×1
class ×1
datagridview ×1
enumeration ×1
flags ×1
generics ×1
linq ×1
templates ×1
winforms ×1