Using same custom datatype in two plugins - Grasshopper2024-03-28T12:34:39Zhttps://www.grasshopper3d.com/forum/topics/using-same-custom-datatype-in-two-plugins?commentId=2985220%3AComment%3A1765687&x=1&feed=yes&xn_auth=noThe third option is the techn…tag:www.grasshopper3d.com,2017-06-07:2985220:Comment:17656872017-06-07T21:29:32.208ZMateusz Zwierzyckihttps://www.grasshopper3d.com/profile/MateuszZwierzycki
<p>The third option is the technically correct one (which is the best kind of correct). </p>
<p></p>
<p>I usually structure my gh-related code into 3 parts:</p>
<p>- core ("agnostic" part of the code)</p>
<p>- common (Goos and Parameters)</p>
<p>- plugin (gha, exposes the core using the common)</p>
<p></p>
<p>This way you can have many plugins (also developed by other people) pointing to the same common and core libraries, and also passing the same types between each other.</p>
<p>One catch in…</p>
<p>The third option is the technically correct one (which is the best kind of correct). </p>
<p></p>
<p>I usually structure my gh-related code into 3 parts:</p>
<p>- core ("agnostic" part of the code)</p>
<p>- common (Goos and Parameters)</p>
<p>- plugin (gha, exposes the core using the common)</p>
<p></p>
<p>This way you can have many plugins (also developed by other people) pointing to the same common and core libraries, and also passing the same types between each other.</p>
<p>One catch in this approach: Grasshopper won't expose the Parameters from the dll... The hack around this problem is to declare classes in the plugin which do nothing but inherit from Parameters of the common library.</p>
<p></p>
<p>As David says, distribution and versioning are a bit tricky, but hey let the developers do their job.</p> I tried that before, but Json…tag:www.grasshopper3d.com,2017-06-07:2985220:Comment:17655052017-06-07T18:43:55.884ZRanjeeth Mahankalihttps://www.grasshopper3d.com/profile/RanjeethMahankali
<p>I tried that before, but Json Deserializer requires a constructor with 0 arguments. So It cannot deserialize IGH_GeometricGoo. I ended up using a dictionary instead. Thank you !</p>
<p>I tried that before, but Json Deserializer requires a constructor with 0 arguments. So It cannot deserialize IGH_GeometricGoo. I ended up using a dictionary instead. Thank you !</p> After trying everything else,…tag:www.grasshopper3d.com,2017-06-07:2985220:Comment:17656572017-06-07T18:42:34.947ZRanjeeth Mahankalihttps://www.grasshopper3d.com/profile/RanjeethMahankali
<p>After trying everything else, I ended up using Dictionary. Thanks !</p>
<p>After trying everything else, I ended up using Dictionary. Thanks !</p> Just to add to David's alread…tag:www.grasshopper3d.com,2017-05-24:2985220:Comment:17570002017-05-24T18:07:17.788ZAndrew Heumannhttps://www.grasshopper3d.com/profile/AndrewHeumann
<p>Just to add to David's already-thorough answer:</p>
<p>For option 2 I often rely on Newtonsoft's Json library - it has some useful mechanisms for serializing / deserializing dot net objects - JsonConvert.SerializeObject and JsonConvert.DeserializeObject. </p>
<p>Just to add to David's already-thorough answer:</p>
<p>For option 2 I often rely on Newtonsoft's Json library - it has some useful mechanisms for serializing / deserializing dot net objects - JsonConvert.SerializeObject and JsonConvert.DeserializeObject. </p> They are the same datatype bu…tag:www.grasshopper3d.com,2017-05-23:2985220:Comment:17561182017-05-23T08:16:47.101ZDavid Ruttenhttps://www.grasshopper3d.com/profile/DavidRutten
<blockquote><p><span>They are the same datatype but in different assemblies, that is all.</span></p>
</blockquote>
<p></p>
<p>That's not the way .NET sees it. They are different types, defined in different assemblies, with different type-ids. Just because they happen to act in an identical way doesn't mean they are interchangeable.</p>
<p></p>
<p>Your options are:</p>
<ul>
<li>Only define the type once in the assembly which instantiates this type, then use Reflection or dynamic to access its…</li>
</ul>
<blockquote><p><span>They are the same datatype but in different assemblies, that is all.</span></p>
</blockquote>
<p></p>
<p>That's not the way .NET sees it. They are different types, defined in different assemblies, with different type-ids. Just because they happen to act in an identical way doesn't mean they are interchangeable.</p>
<p></p>
<p>Your options are:</p>
<ul>
<li>Only define the type once in the assembly which instantiates this type, then use Reflection or dynamic to access its fields from another assembly. This solution won't work if both assemblies need to instantiate the type.</li>
<li>Define the type in both assemblies, but make sure that there's a way to 'reconvert' it to 'itself'. For example you could override the ToString() method to deserialize the type, and add a constructor which takes a string and serializes it. This means a lot of duplication which could be a problem if your type contains <em>lots</em> of data or if your type contain unserializable data such as socket connections.</li>
<li>Create a third assembly which defines the type, and reference that assembly in both your projects (this is the official solution usually given, but obviously complicates distribution and versioning).</li>
<li>Switch to a platform data type that both assemblies already have access to, such as Dictionary or List<T> or something.</li>
</ul>