C# 获取具有字符串值的类属性列表
我有一个班级在留言。我需要从这里获取运行时的属性列表和字符串。例如:C# 获取具有字符串值的类属性列表,c#,reflection,C#,Reflection,我有一个班级在留言。我需要从这里获取运行时的属性列表和字符串。例如: var classObject = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("edifactProdat.PRODAT"); var unhTypeNames = classObject.GetType().GetProperty("UNH").GetType().GetProperties(); 但此代码不返回我的UNH对象属性,它返
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; }
}
}
要获取UMH
类的(public
)属性名称:
String[] propNames = typeof(edifactProdat.PRODAT.UMH)
.GetProperties()
.Select(property => property.Name)
.ToArray();
注意,您不需要创建感兴趣对象的实例(
classObject
),它返回what的所有属性?我想得到这个列表;var unhPropList=typeof(UNH.GetProperties();但我需要使用动态类名,比如var unhPropList=typeof(“stringValue”).GetProperties();您需要PropertyType
而不是第二个GetType()
。
var unhTypes = classObject.GetType().GetProperty("UNH").PropertyType.GetProperties();