[英]How do I identify class properties that are public with an enum return type?
I have automatically generated service references and I need to analyze them to identify classes with public properties that return enums
.我已自动生成服务引用,我需要分析它们以识别具有返回
enums
的公共属性的类。 This is for an asp.net project that was originally serializing enums as int
and was converted to string
, so I'm trying to produce a list of enums and their int/string values.这是一个最初将枚举序列化为
int
并转换为string
的 asp.net 项目,因此我试图生成一个枚举列表及其 int/string 值。
I just copied & pasted all of the service references into a .NET console project and I'm able to enumerate the properties and I am focusing on a known enum for testing, but the output from the below code is not identifying it as an enum when it is:我刚刚将所有服务引用复制并粘贴到一个 .NET 控制台项目中,我能够枚举属性,我专注于一个已知的枚举进行测试,但下面代码的输出没有将其识别为枚举几时:
[AxdEntity_PurchTable_1\Property\PurchStatus]: Nullable`1; False
How can I correctly identify it as an enum
?我怎样才能正确地将它识别为
enum
?
My Reflection Code:我的反射代码:
var q = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsClass && t.Namespace == "MyNamespace"
&& t.Name == "AxdEntity_PurchTable_1" // I plan to remove this, but focusing on one class
select t;
foreach (var refClass in q.ToList())
{
foreach (var prop in refClass.GetProperties())
{
if (prop.Name == "PurchStatus")
{
// How do I determine if the return type is an enum?
Console.WriteLine($"[{refClass.Name}\\Property\\{prop.Name}]: {prop.PropertyType.Name}; {prop.PropertyType.BaseType.IsEnum}");
// OUTPUT: [AxdEntity_PurchTable_1\Property\PurchStatus]: Nullable`1; False
// prop.PropertyType.BaseType.IsEnum == false?
}
}
}
Automatically generated service reference sample code:自动生成服务参考示例代码:
namespace MyNamespace
{
// <... Many other generated classes in this namespace>
public partial class AxdEntity_PurchTable_1
{
private System.Nullable<AxdEnum_PurchStatus> purchStatusField;
// <... Many other properties in this class ...>
public System.Nullable<AxdEnum_PurchStatus> PurchStatus
{
get
{
return this.purchStatusField;
}
set
{
this.purchStatusField = value;
}
}
}
// <... Many other enums in this namespace ...>
public enum AxdEnum_PurchStatus
{
None,
Backorder,
Received,
Invoiced,
Canceled,
}
}
You have to check if the generic type is Nullable<>
and if its parameter is an enum.您必须检查泛型类型是否为
Nullable<>
以及它的参数是否为枚举。 This should detect normal and nullable enum properties:这应该检测正常和可为 null 的枚举属性:
foreach (var refClass in q.ToList())
{
foreach (var prop in refClass.GetProperties())
{
if(
prop.PropertyType.IsEnum
|| (prop.PropertyType.IsGenericType
&& prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)
&& prop.PropertyType.GetGenericArguments()[0].IsEnum))
{
Console.Write(prop);
}
}
}
PurchStatus
property type is not just enum, but nullable enum. PurchStatus
属性类型不仅是枚举,而且是可为空的枚举。 Use Nullable.GetUnderlyingType
(returns null
if type is not nullable so coalesce with type itself):使用
Nullable.GetUnderlyingType
(如果类型不可为空则返回null
,因此与类型本身合并):
foreach (var refClass in q.ToList())
{
foreach (var prop in refClass.GetProperties())
{
var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
if (type.IsEnum)
{
// do something
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.