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

Time:01-03

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.

CodePudding user response:

You're trying to get the value of a property as if it were a property of string - the first argument to GetValue needs to be the object you're fetching the property from (oldEntity in this case). So you can get the department like this:

object department = oldEntity.GetType().GetProperty("Department").GetValue(oldEntity, null);

To get the Name property (which I hope is a property and not a public field) you need to do the same kind of thing again:

object name = department.GetType().GetProperty("Name").GetValue(department, null);

However, you could do this all somewhat more simply using dynamic typing:

dynamic entity = oldEntity;
string name = entity.Department.Name;

Note that there's no compile-time checking for the Department or Name parts (or that the resulting type is string) but that's the same as for the reflection-based code anyway. The dynamic typing would just do the reflection for you at execution time.

  • Related