Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieve NHibernate.Collections.Generic.PersistentGenericBag as IList<T> using Reflection
    primarykey
    data
    text
    <p>I'm attempting to compare a transient object graph to an NHibernate-persisted object graph. Unfortunately my code breaks where properties of type <code>IList&lt;T&gt;</code> are concerned. The code below works fine with instances of <code>List&lt;T&gt;</code>, because <code>List&lt;T&gt;</code> implements both <code>IList&lt;T&gt;</code> AND <code>IList</code>. Unfortunately, NHibernate's PersistentGenericBag only implements <code>IList&lt;T&gt;</code>.</p> <pre><code>IList list1 = (IList)prop1.GetValue(object1, null); IList list2 = (IList)prop2.GetValue(object2, null); </code></pre> <p>If either object1 or object2 is a PersistentGenericBag, I get an error such as:</p> <pre><code>System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation. ----&gt; System.InvalidCastException : Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag`1[MyNamespace.MyClass]' to type 'System.Collections.Generic.List`1[MyNamespace.MyClass]'. </code></pre> <p>Is there a reliable way to retrieve the PersistentGenericBag instance as an IList&lt;T&gt; using reflection?</p> <p>I had hoped the popular <a href="http://comparenetobjects.codeplex.com/" rel="nofollow noreferrer">Compare .NET Objects</a> class would help, but it fails with the exact same error.</p> <p><strong>Edit:</strong> All the answers below are on the right track. The problem was that the getter on the problematic <code>IList&lt;T&gt;</code> property was attempting a cast to <code>List&lt;T&gt;</code>, which obviously can't be done to a PersistentGenericBag. So, my fault for the misguided question.</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.
 

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