0

Is there any way I can get the complete path of the property from the property itself ?

I have code design below as something:

class A
{
   B BProperty { get; set; }
   D DProperty { get; set; }
}

class B
{
   D DProperty { get; set; }
}

class D
{
   int Value { get; set; }
}
       
class Verification
{
   public static void VerifyAProperty(A source, A dest)
   {
      VerifyBProperty(source.BProperty, dest.BProperty);
      VerifyDProperty(source.DProperty, dest.DProperty);
   }

   public static void VerifyBProperty(B source, B dest)
   {
      VerifyDProperty(source.DProperty, dest.DProperty);
   }

   public static void VerifyDProperty(D source, D dest)
   {
      //// Here I want to verify source.value with dest.value and if they do not match I want to show an error message. 
      //// I have requirement to show the complete path of property that is under verification.
      //// i.e either A->D->value or A->B->D->value
   }
}

This is just a small part of my problem. I have number of similar verification to be done on number of different properties that can be at multiple places in the hierarchy.

I need to verify the source property with the destination property and in case if both do not match, show an error message that provides the path of the property that did not match.

I have tried something by passing a string property to VerifyProperty() function that will be appended as we go down the hierarchy. I just want to know if there is any better way to achieve this.

Jasmeet
  • 1,315
  • 11
  • 23

1 Answers1

1

You can build an example where a property have multiple path:

var d = new D();
var b = new B { DProperty = d };
var a = new A { BProperty = b, DProperty = d };

In this example, the reference is the same in d, a.DProperty and a.BProperty.DProperty. It is the same reference stored at multiple places, hence it can't have an unique path.

That why there is no concept of (unique) property path in C#.

You have to add the path information by your own:

class Verification
{
  public static void VerifyAProperty(A source, A dest, string path = null)
  {
    path += "->A";
    VerifyBProperty(source.BProperty, dest.BProperty, path);
    VerifyDProperty(source.DProperty, dest.DProperty, path);
  }

  public static void VerifyBProperty(B source, B dest, string path = null)
  {
    path += "->B";
    VerifyDProperty(source.DProperty, dest.DProperty, path);
  }

  public static void VerifyDProperty(D source, D dest)
  {
    path += "->D";
    if (source.Value != dest.Value)
      Console.WriteLine($"Different values at: {path}");
  }
}

An other option is reflection.

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
Orace
  • 7,822
  • 30
  • 45
  • 2
    You can use an expression tree to capture the "path". This is a messy solution. – Kieran Devlin Feb 24 '20 at 08:44
  • This is what I currently have. As I mentioned in the question 'I have tried something by passing a string property to VerifyProperty() function that will be appended as we go down the hierarchy'. I just thought there might be a better way to do so. – Jasmeet Feb 24 '20 at 08:44
  • @KieranDevlin, expression tree are a good option but you have to specify each and every paths while my proposition allow code factorization. – Orace Feb 24 '20 at 08:48
  • @Orace It's completely same to what you have written in your answer. – Jasmeet Feb 24 '20 at 08:49
  • @Jasmeet, and if you have show us the code, you may have saved me 5 minutes. – Orace Feb 24 '20 at 08:51