简体   繁体   English

如何识别具有枚举返回类型的公共类属性?

[英]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.

相关问题 如何实现此公共可访问枚举 - How do I implement this public accesible enum 如何使用 Roslyn 将 JsonIgnore 属性添加到 class 中具有特定返回类型的所有属性? - How do I add a JsonIgnore attribute to all properties with a specific return type in a class using Roslyn? C#泛型类,如何使用类型参数的派生类的公共属性? - C# Generic Class, how can I use public properties of derived class of type parameter? 如何在下拉列表中显示枚举属性? - How do I display Enum Properties in a Dropdownlist? 如果在C#中的字符串中有类型名称,如何获取对象的公共属性? - How do I get the public properties of an Object if I have the type name in a string in C#? 如何在类中找到DateTime类型的所有属性? - How do I find all properties of type DateTime in an class? 如何列出具有属性的类型类的列表? - How do I make a list of type class, which has properties? 将属性或公共字段移动到另一个类后如何重命名? - How do I rename properties or public fields after moving them to another class? 如何在公共方法上验证枚举参数? - How do I validate an enum parameter on a public method? 如何更改枚举的类型? - How do I change the type of an enum?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM