為了在 Entity Framework Core 中播種型別表,我在函式“StopLightColorTypeList”中為我需要播種的每個資料庫型別表重寫了相同的代碼。我正在尋找創建一個通用方法。
modelBuilder.Entity<StopLightColorType>
().HasData(WLProgramSeed.StopLightColorTypeList());
StopLightColorTypeList 將每個列舉成員創建一個新的 StopLightColorType 物件并將其添加到之前的串列中:
將物件 ID 屬性設定為 Enum 的 int 值 將 Name 屬性設定為 Enum 的字串值
我希望以我可以的方式使用泛型
- 指定列舉型別(在本例中為 StopLightColorsEnum)
- 指定回傳型別物件(在本例中為 StopLightColorType)
- 指定要為 ID 設定的屬性(必須是整數)(在本例中為 StopLightColorID)
- 指定要為名稱設定的屬性(必須是字串)(在本例中為 StopLightColorName)
- 可選地指定一個函式來決議列舉名稱
using System;
using System.Collections.Generic;
using System.Linq;
namespace GenericEnumToTypeList
{
class Program
{
static void Main(string[] args)
{
List<StopLightColorType> stopLightColorTypes = EFTools.StopLightColorTypes();
foreach(var stopLightColorType in stopLightColorTypes)
{
Console.WriteLine("ID: {0} Name: {1}", stopLightColorType.StopLightColorID, stopLightColorType.StopLightColorName);
}
Console.ReadLine();
}
}
public enum StopLightColorsEnum
{
Red = 1,
Yellow = 2,
Green = 3
}
public class StopLightColorType
{
public StopLightColorsEnum StopLightColorID { get; set; }
public string StopLightColorName { get; set; }
}
public class EFTools
{
public static List<StopLightColorType> StopLightColorTypes()
{
List<StopLightColorType> stopLightColorTypes = new List<StopLightColorType>();
foreach(StopLightColorsEnum stopLightColor in System.Enum.GetValues(typeof(StopLightColorsEnum))
{
StopLightColorType stopLightColorType = new StopLightColorType
{
StopLightColorID = stopLightColor,
StopLightColorName = ParseCapitalizedEnumName(stopLightColor.ToString())
};
stopLightColorTypes.Add(stopLightColorType);
}
return stopLightColorTypes;
}
public static string ParseUnderScoreName(string stringName)
{
return stringName.Replace("_", " "); ;
}
public static string ParseCapitalizedEnumName(string enumName)
{
string typeName = enumName[0].ToString();
for(int i = 1; i <= enumName.Length; i )
{
if(Char.ToUpper(enumName[i]) == enumName[i])
{
typeName = typeName " " enumName[i].ToString();
}
}
return typeName;
}
}
}
我為每種資料庫型別重新撰寫了這段代碼,我正在尋找一個通用的來處理這個操作。
uj5u.com熱心網友回復:
您需要的核心功能是:
IEnumerable<TResult> ListEnumMembers<TEnum, TResult>(string valueName, string nameName)
where TEnum : System.Enum
{
var enumInfos = Enum.GetValues(typeof(TEnum)).Cast<int>()
.Zip(Enum.GetNames(typeof(TEnum)), (i, s) => (Value: i, Name: s));
var typ = typeof(TResult);
var piValue = typ.GetProperty(valueName);
var piName = typ.GetProperty(nameName);
return enumInfos.Select(i =>
{
var instance = (TResult)Activator.CreateInstance(typ);
piValue.SetValue(instance, i.Value);
piName.SetValue(instance, i.Name);
return instance;
});
}
使用這種通用結果型別...
class EnumInfo
{
public int Value { get; set; }
public string Name { get; set; }
}
...和這段代碼:
var enumInfos = ListEnumMembers<DayOfWeek, EnumInfo>("Value", "Name").ToList();
你得到這個結果:
| 價值 | 名稱 |
|---|---|
| 0 | 星期日 |
| 1 | 周一 |
| 2 | 周二 |
| 3 | 周三 |
| 4 | 周四 |
| 5 | 星期五 |
| 6 | 周六 |
我想這會讓你走上正軌。當然,代碼需要對屬性名稱和型別進行幾次檢查。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/408807.html
標籤:
上一篇:如何將物件及其子類作為物件的引數
