Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you could do is have a <code>Dictionary</code> with a type as the key and relevant properties as the values. You would then only need to search through the properties once for the ones you are interested in (by the looks of things <code>IEnumerable</code> and <code>string</code>) - after all, the properties the types have aren't going to change (unless you're doing some funky <a href="http://msdn.microsoft.com/en-us/library/3y322t50.aspx" rel="nofollow noreferrer">Emit</a> stuff but I'm not too familiar with that)</p> <p>Once you have this you could simply iterate all the properties in the <code>Dictionary</code> using the objects type as the key.</p> <p>Somehting like this (I haven't actually tested it but it does complile :) )</p> <p><code></p> <pre><code> private static Dictionary&lt;Type, List&lt;PropertyInfo&gt;&gt; _properties = new Dictionary&lt;Type, List&lt;PropertyInfo&gt;&gt;(); private static void ExtractProperties(List&lt;PropertyInfo&gt; list, Type type) { if (type == null || type == typeof(object)) { return; // We've reached the top } // Modify which properties you want here // This is for Public, Protected, Private const BindingFlags PropertyFlags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; foreach (var property in type.GetProperties(PropertyFlags)) { if (!property.CanRead || !property.CanWrite) continue; if ((property.PropertyType == typeof(string)) || (property.PropertyType.GetInterface("IEnumerable") != null)) { if (!property.Name.Contains("password")) { list.Add(property); } } } // OPTIONAL: Navigate the base type ExtractProperties(list, type.BaseType); } private static void ConvertToUpper(object entity, Hashtable visited) { if (entity != null &amp;&amp; !visited.ContainsKey(entity)) { visited.Add(entity, entity); List&lt;PropertyInfo&gt; properties; if (!_properties.TryGetValue(entity.GetType(), out properties)) { properties = new List&lt;PropertyInfo&gt;(); ExtractProperties(properties, entity.GetType()); _properties.Add(entity.GetType(), properties); } foreach (PropertyInfo propertyInfo in properties) { object propertyValue = propertyInfo.GetValue(entity, null); Type propertyType = propertyInfo.PropertyType; if (propertyType == typeof(string)) { propertyInfo.SetValue(entity, ((string)propertyValue).ToUpper(), null); } else // It's IEnumerable { foreach (object value in (IEnumerable)propertyValue) { ConvertToUpper(value, visited); } } } } } </code></pre> <p></code></p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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