[英]How do I check enum property when the property is obtained from dynamic in C#?
Suppose I know that property Color
of an object returns an enumeration that looks like this one: 假设我知道对象的属性Color
返回一个如下所示的枚举:
enum ColorEnum {
Red,
Green,
Blue
};
and I want to check that a specific object of unknown type (that I know has Color
property) has Color
set to Red
. 我想检查一个未知类型的特定对象(我知道有Color
属性)将Color
设置为Red
。 This is what I would do if I knew the object type: 如果我知道对象类型,这就是我要做的事情:
ObjectType thatObject = obtainThatObject();
if( thatObject.Color == ColorEnum.Red ) {
//blah
}
The problem is I don't have a reference to the assembly with ColorEnum
and don't know the object type. 问题是我没有使用ColorEnum
对程序集的引用,也不知道对象类型。
So instead I have the following setup: 所以我有以下设置:
dynamic thatObject = obtainThatObject();
and I cannot cast because I don't know the object type (and the enum type). 而我无法施放,因为我不知道对象类型(和枚举类型)。 How should I check the Color
? 我该如何检查Color
?
if( thatObject.Color.ToString() == "Red" ) {
//blah
}
does work but it looks like the worst examples of cargo cult code I've seen in "The Daily WTF". 确实有效,但它看起来像我在“每日WTF”中看到的货物崇拜代码中最糟糕的例子。
How do I do the check properly? 我该如何正确检查?
How about parsing the Color property to your enum first 如何首先将Color属性解析为枚举
if ((ColorEnum) Enum.Parse(typeof (ColorEnum), thatObject.Color.ToString()) == ColorEnum.Red)
{
// do something
}
In the side assembly: 在侧组件中:
enum ColorEnum
{
Red,
Green,
Blue
};
We know that Red exists, but nothing about other colors. 我们知道Red存在,但没有其他颜色。 So we redefine the enum in our assembly with known values only. 因此,我们仅使用已知值重新定义程序集中的枚举。
enum KnownColorEnum // in your assembly
{
Red
};
Therefore we can perform parsing: 因此我们可以执行解析:
public static KnownColorEnum? GetKnownColor(object value)
{
KnownColorEnum color;
if (value != null && Enum.TryParse<KnownColorEnum>(value.ToString(), out color))
{ return color; }
return null;
}
Examples: 例子:
// thatObject.Color == ColorEnum.Red
// or
// thatObject.Color == "Red"
if (GetKnowColor(thatObject.Color) == KnownColorEnum.Red) // true
{ }
// thatObject.Color == ColorEnum.Blue
if (GetKnowColor(thatObject.Color) == KnownColorEnum.Red) // false
{ }
One possible (unorthodox) way is: Force your dynamic object (whatever object it is) to be a ExpandoObject (with this extension method): 一种可能的(非正统)方式是:强制您的动态对象(无论它是什么对象)是ExpandoObject(使用此扩展方法):
public static dynamic ToDynamic(this object value)
{
IDictionary<string, object> expando = new ExpandoObject();
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value.GetType()))
expando.Add(property.Name, property.GetValue(value));
return expando as ExpandoObject;
}
Convert your obtainedObject: 转换您获得的对象:
var obtainedObject = (object)obtainThatObject();
var myexpando = obtainedObject.ToDynamic(); // now you have an ExpandoObject
So you can get the properties in an IDictionary 因此,您可以在IDictionary中获取属性
IDictionary<string, object> dictionary= (IDictionary<string, object>) myexpando;
if(dictionary.ContainsKey("Color"))
{
var myValue = dictionary["Color"];
string color = myValue.ToString();
if(color == "Green")
{
// blah
}
}
going this way you don't have to care what object it is... 走这条路,你不必关心它是什么对象......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.