Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Object does not match target type.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading