lundi 3 janvier 2022

Get property value of a complex type with reflection in c#

I have an object like this

oldEntity
     Active: true
    ActiveDirectoryName: ""
    ArchiveConceptValueList: null
    AsgScndCnpRangeDictionary: Count = 0
    AssignedRuleList: {NHibernate.Collection.Generic.PersistentGenericBag<GTS.Clock.Model.AssignedRule>}
    AssignedScndCnpRangeList: {NHibernate.Collection.Generic.PersistentGenericBag<GTS.Clock.Model.AssignedScndCnpRange>}
    AssignedScndCnpRangePeresentList: {NHibernate.Collection.Generic.PersistentGenericBag<GTS.Clock.Model.AssignedScndCnpRange>}
    AssignedWGDShiftList: {NHibernate.Collection.Generic.PersistentGenericBag<GTS.Clock.Model.AssignedWGDShift>}
    AssignedWorkGroupList: {GTS.Clock.Model.PersonWorkGroup, GTS.Clock.Model.PersonWorkGroup}
    Assistant: null
    BarCode: "0451343786"
    BasicTrafficController: {GTS.Clock.Model.Concepts.BasicTrafficController}
    BasicTrafficList: {}
    BudgetList: null
    CFPNeedUpdate: false
    CalcDateZone: null
    CardNum: "2465"
    CartOrgan: {GTS.Clock.Model.Charts.Department}
    CheckEnterAndExitInRequest: false
    CostCenter: {GTS.Clock.Model.BaseInformation.CostCenter}
    CurrentActiveContract: null
    CurrentActiveDateRangeGroup: null
    CurrentActiveRuleGroup: null
    CurrentActiveWorkGroup: null
    CurrentRangeAssignment: null
    CurrentYearBudgetList: {NHibernate.Collection.Generic.PersistentGenericBag<GTS.Clock.Model.Concepts.CurrentYearBudget>}
    DelayCartableList: {}
    Department: {GTS.Clock.Model.Charts.Department}
    DepartmentID: 0
    ...

And I want the name fiel of Department. Besides Department in oldEntity like this: enter image description here and properies of Department field like this: enter image description here

once I use below code to get name field of Department with Reflection ,I got this erro

oldEntity.GetType().GetProperty("Department").GetValue("Name", null);

Object does not match target type.





Aucun commentaire:

Enregistrer un commentaire