我想知道是否可以获取枚举值的属性而不是枚举本身的属性?例如,假设我有以下枚举:
using System.ComponentModel; // for DescriptionAttribute
enum FunkyAttributesEnum
{
[Description("Name With Spaces1")]
NameWithoutSpaces1,
[Description("Name With Spaces2")]
NameWithoutSpaces2
}
Run Code Online (Sandbox Code Playgroud)
我想要的是枚举类型,产生2元组的枚举字符串值及其描述.
价值很容易:
Array values = System.Enum.GetValues(typeof(FunkyAttributesEnum));
foreach (int value in values)
Tuple.Value = Enum.GetName(typeof(FunkyAttributesEnum), value);
Run Code Online (Sandbox Code Playgroud)
但是如何获取描述属性的值,以填充Tuple.Desc?如果属性属于枚举本身,我可以想到如何做到这一点,但我不知道如何从枚举的值中获取它.
我想定义一个用于EF5的枚举,以及一个相应的查找表.我知道EF5现在支持枚举,但开箱即用,它似乎只在对象级别支持此功能,并且默认情况下不会为这些查找值添加表格.
例如,我有一个用户实体:
public class User
{
int Id { get; set; }
string Name { get; set; }
UserType UserType { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和UserType枚举:
public enum UserType
{
Member = 1,
Moderator = 2,
Administrator = 3
}
Run Code Online (Sandbox Code Playgroud)
我希望数据库生成创建一个表,如:
create table UserType
(
Id int,
Name nvarchar(max)
)
Run Code Online (Sandbox Code Playgroud)
这可能吗?
我有一节课:
public class AccountDetail
{
public DetailScope Scope
{
get { return scope; }
set { scope = value; }
}
public string Value
{
get { return this.value; }
set { this.value = value; }
}
private DetailScope scope;
private string value;
public AccountDetail(DetailScope scope, string value)
{
this.scope = scope;
this.value = value;
}
}
Run Code Online (Sandbox Code Playgroud)
和一个枚举:
public enum DetailScope
{
Private,
Business,
OtherDetail
}
Run Code Online (Sandbox Code Playgroud)
最后,我有一个.xaml文件:
<Window x:Class="Gui.Wpf.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test"
SizeToContent="WidthAndHeight">
<Grid>
<ComboBox
Name="ScopeComboBox"
Width="120"
Height="23"
Margin="12" /> …Run Code Online (Sandbox Code Playgroud) 我有一个文件,我的格式是通过python脚本改变的.我在这个文件中有几个camel cased字符串,我只想在大写字母之前插入一个空格 - 所以"WordWordWord"变成"Word Word Word".
我有限的正则表达式经验只是让我感到困惑 - 有人可能会想到一个像样的正则表达式,或者(更好的是)是否有更多的pythonic方法来做到这一点,我错过了?
我想采用驼峰式的类名或枚举名称,并以普通文本显示给用户.我怎样才能以编程方式执行此操作?
样本输入:
MainPageBackgroundColor
预期产量:
主页背景颜色
要么
主页背景颜色
我想拿一个像"CountOfWidgets"这样的pascal-cased字符串,并将其转换为更加用户友好的东西,如C#中的"小部件数量".多个相邻的大写字符应保持不变.最有效的方法是什么?
我试图得到一个字符串,在字符串中的大小写更改的地方有一个下划线'_',以使用户更清楚.例如,如果我们有一个String'个人ID号码',我想把它变成'personal_ID_number'.这是在C#中.
谢谢,我感谢任何帮助,建议.
-sid
如何使用C#将"ThisIsMyTestString"转换为"This Is My Test String"?
有快速的方法吗?
我一直在想一个伪代码,但它复杂而丑陋:
String s = "ThisIsMyTestString";
List<String> strList = new List<String>();
for(int i=0; i < str->Length ; i++)
{
String tmp = "";
if (Char.IsUpper(str[i]))
{
tmp += str[i];
i++;
}
while (Char::IsLower(str[i]))
{
tmp += str[i];
i++;
}
strList .Add(tmp);
}
String tmp2 = "";
for (uint i=0 ; i<strList.Count(); i++)
{
tmp2 += strList[i] + " ";
}
Run Code Online (Sandbox Code Playgroud) c# ×7
regex ×3
string ×3
combobox ×1
data-binding ×1
enums ×1
python ×1
reflection ×1
text-files ×1
wpf ×1
xaml ×1