Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are some classes:</p> <pre><code>public class A { public int Id { get; set; } public B BValue { get; set; } public A() { this.Id = 1000; this.BValue = new B(); } } public class B { public string Name { get; set; } public C CValue { get; set; } [NoLog] public string Secret { get; set; } public B() { this.Name = "Empty Name"; this.CValue = new C(); } } public class C { public string Description { get; set; } public C() { this.Description = "Empty Description"; } } </code></pre> <p>and NoLog custom attribue:</p> <pre><code>public class NoLogAttribute : Attribute { } </code></pre> <p>According to your question you use sth like this to pretty print:</p> <pre><code>public static void PrintProperties(object obj, int indent = 4, char intendCharacter = ' ') { if (obj == null) return; string indentString = new string(intendCharacter, indent); Type objType = obj.GetType(); foreach (var pName in GetPropertiesToLogWithCaching(obj)) { var property = objType.GetProperty(pName); object propValue = property.GetValue(obj, null); if (property.PropertyType.Assembly == objType.Assembly) { Console.WriteLine("{0}{1}:", indentString, property.Name); PrintProperties(propValue, indent + 2); } else { Console.WriteLine("{0}{1}: {2}", indentString, property.Name, propValue); } } } </code></pre> <p>I changed the way how we access properties (by name) and added for this two methods:</p> <pre><code>public static List&lt;string&gt; GetPropertiesToLogWithCaching(object obj) { List&lt;string&gt; propertiesToLog = new List&lt;string&gt;(); if (obj != null) { string key = obj.GetType().FullName; propertiesToLog = CacheLayer.Get&lt;List&lt;string&gt;&gt;(key); if (propertiesToLog == null) { propertiesToLog = GetPropertiesToLog(obj); CacheLayer.Add&lt;List&lt;string&gt;&gt;(propertiesToLog, key); } } return propertiesToLog; } public static List&lt;string&gt; GetPropertiesToLog(object obj) { List&lt;string&gt; propertiesToLog = new List&lt;string&gt;(); if (obj != null) { foreach (var p in obj.GetType().GetProperties().Where(prop =&gt; !Attribute.IsDefined(prop, typeof(NoLogAttribute)))) { propertiesToLog.Add(p.Name); } } return propertiesToLog; } </code></pre> <p>where GetPropertiesToLogWithCaching is wrapper of GetPropertiesToLog. ChacheLayer object you can find here <a href="http://www.deanhume.com/home/blogpost/object-caching----net-4/37" rel="nofollow">http://www.deanhume.com/home/blogpost/object-caching----net-4/37</a></p> <p>Now it is recursive and it has caching. As I wrote in first version of answer. The idea is to chache list of properties names and than GetProperty by name without testing attributes as a key for cache we can use FullName of obj Type. Now you need to check how it works.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload