获取具有字符串值的类属性列表

时间:2015-09-01 14:11:59

标签: c# reflection

我有一个课后面的消息。我需要从运行时获取属性列表和字符串。例如:

var classObject = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("edifactProdat.PRODAT");
var unhTypeNames = classObject.GetType().GetProperty("UNH").GetType().GetProperties();

但是这段代码不返回我的UNH对象属性,它返回所有属性 任何帮助将不胜感激。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace edifactProdat
{
    public class PRODAT
    {
        public UNH UNH { get; set; }
        public BGM BGM { get; set; }
        public DTM DTM { get; set; }
        public NAD NAD { get; set; }
    }

    public class UNH
    {
        public string MessageReferenceNumber { get; set; }
        public string MessageTypeIdentifier { get; set; }
        public string MessageTypeVersionNumber { get; set; }
        public string MessageTypeReleaseNumber { get; set; }
        public string ControllingAgency { get; set; }
        public string AssociationAssignedCode { get; set; }
    }

    public class BGM
    {
        public string DocumentMessageNameCoded { get; set; }
        public string CodeListQualifier { get; set; }
        public string CodeListResponsibleAgencyCoded { get; set; }
        public string DocumentMessageNumber { get; set; }
        public string MessageFunctionCoded { get; set; }
    }

    public class DTM
    {
        public string DateTimePeriodQualifier { get; set; }
        public string DateTimePeriod { get; set; }
        public string DateTimePeriodFormatQualifier { get; set; }
    }

    public class NAD
    {
        public string PartyQualifier { get; set; }
        public string PartyIdIdentification { get; set; }
        public string CodeListQualifier { get; set; }
        public string CodeListResponsibleAgencyCoded { get; set; }
    }
}

2 个答案:

答案 0 :(得分:1)

获取public类的UMH个属性名称:

  String[] propNames = typeof(edifactProdat.PRODAT.UMH)
    .GetProperties()
    .Select(property => property.Name)
    .ToArray();

请注意,您无需在中创建感兴趣对象的实例(代码中为classObject)。

答案 1 :(得分:1)

def enforce_bio(self):
        bio = self.cleaned_data['bio']
        rows = bio.split('\n')
        if len(rows) > 10:
            raise forms.ValidationError("bio too long!")

        else:
            return bio
相关问题