Note that there are some explanatory texts on larger screens.

plurals
  1. POInvalidCastException - Cannot cast string to custom object even with operators
    primarykey
    data
    text
    <p>I am probably trying to do something that isn't possible. I am working with Castle.ActiveRecord / Nhibernate. I have been happy so far, but one thing I have always wanted to able to so is have something like:</p> <pre><code>[Property(ColumnType = "StringClob")] public IDictionary&lt;string, string&gt; MetaData { get; set; } </code></pre> <p>This, for obvious reasons, this isn't possible at the moment. I started playing around with things to make this work and have come down to the following.</p> <p>I have created a custom class that extends Dictionary</p> <pre><code>public class SerializableDictionary&lt;TK, TV&gt; : Dictionary&lt;TK, TV&gt; { public static explicit operator SerializableDictionary&lt;TK, TV&gt;(System.String serialized) { return JsonConvert.DeserializeObject&lt;SerializableDictionary&lt;TK, TV&gt;&gt;(serialized); } public override string ToString() { return JsonConvert.SerializeObject(this); } } </code></pre> <p>And in my ActiveRecord model I have this:</p> <pre><code>[Property(ColumnType = "StringClob")] public SerializableDictionary&lt;string, string&gt; MetaData { get; set; } </code></pre> <p>This works extremely well when I save the data. It gets serialized nicely and stores as a string. When I try to load a model though, I get the following error:</p> <pre><code>Unable to cast object of type 'System.String' to type 'MyNamespace.SerializableDictionary`2[System.String,System.String]'. </code></pre> <p>I have looked as deep as possible (using dotPeek) into where this is happening. The stack trace led me to:</p> <pre><code>NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues(Object target, Object[] values) </code></pre> <p>I am at a complete loss. I have scoured google to see if what I am trying to do is even possible. Any help is much appreciated.</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.
 

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