C# 存储所有记录的额外阵列,加上记住映射的代码+数据st,然后是确保所有内容同步的单元测试。如果考试通过,我就好了。如果测试失败-那么我需要重写一些代码。显然是YMMV-但是在我工作过的每一个需要类似东西的组织(我猜你在某种形式的医疗保健中),我们在他们自己的
C# 存储所有记录的额外阵列,加上记住映射的代码+数据st,然后是确保所有内容同步的单元测试。如果考试通过,我就好了。如果测试失败-那么我需要重写一些代码。显然是YMMV-但是在我工作过的每一个需要类似东西的组织(我猜你在某种形式的医疗保健中),我们在他们自己的,c#,reflection,.net-3.5,enums,C#,Reflection,.net 3.5,Enums,存储所有记录的额外阵列,加上记住映射的代码+数据st,然后是确保所有内容同步的单元测试。如果考试通过,我就好了。如果测试失败-那么我需要重写一些代码。显然是YMMV-但是在我工作过的每一个需要类似东西的组织(我猜你在某种形式的医疗保健中),我们在他们自己的表中为性别设置ANSI值,任何引用性别的都使用该表的引用(Sex=1)例如,而不是(Sex=“MALE”),或直接从该表中获取值。不过,我不确定这是否是你的选择。谢谢,就快到了!我需要改变的一点是,我希望获得一个描述属性数组,而不是枚举类型的名
存储所有记录的额外阵列,加上记住映射的代码+数据st,然后是确保所有内容同步的单元测试。如果考试通过,我就好了。如果测试失败-那么我需要重写一些代码。显然是YMMV-但是在我工作过的每一个需要类似东西的组织(我猜你在某种形式的医疗保健中),我们在他们自己的表中为性别设置ANSI值,任何引用性别的都使用该表的引用(Sex=1)例如,而不是(Sex=“MALE”),或直接从该表中获取值。不过,我不确定这是否是你的选择。谢谢,就快到了!我需要改变的一点是,我希望获得一个描述属性数组,而不是枚举类型的名称。
GetNames
和GetValues
的文档非常清楚地表明,它们已经按照枚举值的顺序返回了值和名称,因此无需在此处进行排序。谢谢,在实际的表中,大约有十几个描述性名称确实有帮助。是的,我代码中的一点反映正在从描述属性中提取值,因此结果是“男性”、“女性”等等。嗯。。。这看起来很有趣。那么,免费版能解决这个问题吗,还是我需要购买专业版的许可证?
id | mnemonic | description
0 | U | Unknown sex
1 | M | Male
2 | F | Female
3 | T | Trans-gender
public enum SexType
{
/// <summary>Unknown sex.</summary>
[Description("U")]
Unknown = 0,
/// <summary>Male sex.</summary>
[Description("M")]
Male = 1,
/// <summary>Female sex.</summary>
[Description("F")]
Female = 2
/// <summary>Trans-gender sex.</summary>
[Description("T")]
TransGender = 3,
}
// Here we get id for some record from the database.
public static void IsMaleOrFemale(int sexId)
{
if (!Enum.IsDefined(typeof(SexType), sexId))
{
string message = String.Format("There is no sex type with id {0}.",
sexId);
throw new ArgumentException(message, "sexId");
}
var sexType = (SexType) sexId;
return sexType == SexType.Male || sexType == SexType.Female;
}
var choices = Enumerable.Zip(
Enum.GetNames(typeof(SexType)),
Enum.GetValues(typeof(SexType)).Cast<SexType>(),
(name, value) => Tuple.Create(name, value));
public struct SexType
{
public string Type;
public string Code;
public int Value;
}
<#@ template debug="true" hostSpecific="true" #>
<#@ output extension=".generated.cs" #>
<#@ Assembly Name="System.Data" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#
string tableName = Path.GetFileNameWithoutExtension(Host.TemplateFile);
string path = Path.GetDirectoryName(Host.TemplateFile);
string columnId = "ID";
string columnName = "NAME";
string connectionString = "[your connection string]";
#>
using System;
using System.CodeDom.Compiler;
namespace Your.NameSpace.Enums
{
/// <summary>
/// <#= tableName #> auto generated enumeration
/// </summary>
[GeneratedCode("TextTemplatingFileGenerator", "10")]
public enum <#= tableName #>
{
<#
SqlConnection conn = new SqlConnection(connectionString);
string command = string.Format("select {0}, {1} from {2} order by {0}", columnId, columnName, tableName);
SqlCommand comm = new SqlCommand(command, conn);
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
bool loop = reader.Read();
while(loop)
{
#>
/// <summary>
/// <#= reader[columnName] #> configuration setting.
/// </summary>
<#= reader[columnName] #> = <#= reader[columnId] #><# loop = reader.Read(); #><#= loop ? ",\r\n" : string.Empty #>
<# }
#> }
}
<#+ private string Pascalize(object value)
{
Regex rx = new Regex(@"(?:^|[^a-zA-Z]+)(?<first>[a-zA-Z])(?<reminder>[a-zA-Z0-9]+)");
return rx.Replace(value.ToString(), m => m.Groups["first"].ToString().ToUpper() + m.Groups["reminder"].ToString().ToLower());
}
private string GetSubNamespace()
{
Regex rx = new Regex(@"(?:.+Services\s)");
string path = Path.GetDirectoryName(Host.TemplateFile);
return rx.Replace(path, string.Empty).Replace("\\", ".");
}
#>
public IEnumerable<Tuple<string, int>> GetEnumValuePairs(Type enumType)
{
if(!enumType.IsEnum)
{
throw new ArgumentException();
}
List<Tuple<string, int>> result = new List<Tuple<string, int>>();
foreach (var value in Enum.GetValues(enumType))
{
string fieldName = Enum.GetName(enumType, value);
FieldInfo fieldInfo = enumType.GetField(fieldName);
var descAttribute = fieldInfo.GetCustomAttributes(false).Where(a => a is DescriptionAttribute).Cast<DescriptionAttribute>().FirstOrDefault();
// ideally check if descAttribute is null here
result.Add(Tuple.Create(descAttribute.Description, (int)value));
}
return result;
}
List<Tuple<int, string>> pairs = new List<Tuple<int,string>>();
Type enumType = typeof(SexType);
foreach (SexType enumValue in Enum.GetValues(enumType))
{
var customAttributes = enumType.GetField(Enum.GetName(enumType, enumValue)).
GetCustomAttributes(typeof(DescriptionAttribute), false);
DescriptionAttribute descriptionAttribute =
(DescriptionAttribute)customAttributes.FirstOrDefault(attr =>
attr is DescriptionAttribute);
if (descriptionAttribute != null)
pairs.Add(new Tuple<int, string>((int)enumValue, descriptionAttribute.Description));
}