c# - How to cast string to class property
问题描述
I have a model for my database (using efcore to translate between model and database) that contains a string that can be three values.
In order to better control the string values going into the db, I created an enum of sorts (which you can't do in C# so I followed this model).
public class MyModelClass
{
public int Id { get; protected set; }
public string MyString { get; protected set; }
public MyModelClass(int id, MyTranslatingType type)
{
Id = id;
MyString = type.Value;
}
public class MyTranslatingType
{
private MyTranslatingType(string value) { Value = value; }
public string Value { get; set; }
public static MyTranslatingType FirstType
=> new MyTranslatingType(nameof(FirstType));
public static MyTranslatingType SecondType
=> new MyTranslatingType (nameof(SecondType));
public static MyTranslatingType ThirdType
=> new MyTranslatingType (nameof(ThirdType));
}
}
However, when I try to grab values from the database, I can't just cast them, or use typeof()
because they are properties and not a traditional enum/type.
I have tried
// error saying it can't convert [PropertyInfo][2] to MyModelClass.MyTranslatingType
MyModelClass.MyTranslatingType myType = typeof(MyModelClass.MyTranslatingType).GetProperty("ThirdType");
// error saying it can't convert string to MyModelClass.MyTranslatingType
MyModelClass.MyTranslatingType myType = (MyModelClass.MyTranslatingType)"ThirdType";
How can I cast a string of "ThirdType"
to a property like MyModelClass.MyTranslatingType.ThirdType
?
In other words, I want myType
to equal MyModelClass.MyTranslatingType.ThirdType
for
var myType = (MyModelClass.MyTranslatingType)"ThirdType"
解决方案
I think you are looking for a factory pattern of sorts. You can do it something like this:
public class MyTranslatingType
{
// A dictionary of methods to get the relevant type
private static Dictionary<string, Func<MyTranslatingType>> _typeFactory =
new Dictionary<string, System.Func<MyModelClass.MyTranslatingType>>
{
{ nameof(FirstType), () => new MyTranslatingType(nameof(FirstType)) },
{ nameof(SecondType), () => new MyTranslatingType(nameof(SecondType)) },
{ nameof(ThirdType), () => new MyTranslatingType(nameof(ThirdType)) },
};
// Method to get the type required
public static MyTranslatingType GetMyTranslatingType(string type)
{
if (_typeFactory.TryGetValue(type, out var getType))
{
return getType();
}
throw new ArgumentOutOfRangeException(nameof(type), $"Cannot get type for {type}");
}
}
And you use it like this:
var myType = MyModelClass.MyTranslatingType.GetMyTranslatingType("FirstType");
This method is cleaner than yours as we now no longer have a bunch of public static methods to get types.
Here is a full version using const
s instead of properties:
public class MyTranslatingType
{
private const string FirstType = "FirstType";
private const string SecondType = "SecondType";
private const string ThirdType = "ThirdType";
private MyTranslatingType(string value) { Value = value; }
public string Value { get; set; }
// A dictionary of methods to get the relevant type
private static Dictionary<string, Func<MyTranslatingType>> _typeFactory =
new Dictionary<string, System.Func<MyModelClass.MyTranslatingType>>
{
{ FirstType, () => new MyTranslatingType(FirstType) },
{ SecondType, () => new MyTranslatingType(SecondType) },
{ ThirdType, () => new MyTranslatingType(ThirdType) },
};
// Method to get the type required
public static MyTranslatingType GetMyTranslatingType(string type)
{
if (_typeFactory.TryGetValue(type, out var getType))
{
return getType();
}
throw new ArgumentOutOfRangeException(nameof(type), $"Cannot get type for {type}");
}
}
推荐阅读
- sql - Oracle SQL Developer 使用“WITH AS”、“INSERT INTO”、“SELECT”组合
- excel - 您如何在 VBA 中按行分组,以使 x 行中的值是唯一的,并且 y 行中的值相加?
- python - 在 Jupyter 笔记本中添加链接
- networking - 无法访问美国 GCP 以外的某些网站
- c++ - 用任何定义的点绘制矩形
- python - 使用 Seaborn 将散点图上对应的 x 和 y 事件用一条线连接起来
- r - 如何访问数据框中的单元格,数据框包含在 r 的列表中
- python - 如何计算可能组合的数量
- node.js - 如何在nodejs中解析websocket
- javascript - 无法获取 Vue 状态的元素 (Vue.js/JS/TS)