mc312
mc312

Reputation: 31

Get Class Property List With String Value

I have a class following message. I need get property list from here of runtime with string. For example:

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

But this code does not return my UNH object properties, it returns all properties.
Any help would be appreciated.

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; }
    }
}

Upvotes: 2

Views: 1683

Answers (2)

mc312
mc312

Reputation: 31

var unhTypes = classObject.GetType().GetProperty("UNH").PropertyType.GetProperties();

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186823

To get (public) properties names of UMH class:

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

note, that you have no need in creating the instance (classObject in your code) of object of interest.

Upvotes: 1

Related Questions