Grasshopper

algorithmic modeling for Rhino

Passing Custom Class Objects from one component to another in C#

Hey all,

Can do this all the time with the VB scriptable components, but when I try doing it with c#, it asks for a cast from the system.object.  When I do that

1. [A]Custom_Object cannot be cast to [B]Custom_Object. Type A originates from 'e233189a-7b7e-4eab-8fdf-1bdb1cc50365, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\Users\epiermar\AppData\Local\Temp\e233189a-7b7e-4eab-8fdf-1bdb1cc50365.dll'. Type B originates from '7a7dbe0c-0813-4244-845e-7821e9daef59, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\Users\epiermar\AppData\Local\Temp\7a7dbe0c-0813-4244-845e-7821e9daef59.dll'. (line: 75)

I've seen that David said in a post a few years ago there's not passing of objects from one component to another in C#.  Is this still the case?  Thanks for the help.

EAP

Views: 2502

Replies to This Discussion

If you don't output a type which implements IGH_Goo, Grasshopper will try its best to convert your data into goo. When it fails, it will wrap it up in a GH_ObjectWrapper, which is a sort of bare-bones goo type.

If you don't want your data messed with, then you should put it in a GH_ObjectWrapper yourself and cut out the middle man.

What you cannot do is share instances of a type which is not known to both the sender code and the recipient code. Outputting an instance of a type which is defined in the sender cannot be converted into that type by the receiver because the receiver doesn't have access to that type definition. So that's the big constraint you have to work around. You can choose to use an existing type, maybe share data as a String or a Dictionary or something.

I've also encountered this error when using referencing a .gha component inside a C# block (even when using IGH_Goo implementations), that casting such an object from a C# block to a custom component will give this kind of error. The workaround is what david describes below: take the generic data out from the .gha file and put it in a seperate dll.

I'd suggest you simply use VS (or VS Express) and compile a simple library where you keep any tools or custom classes that you use with any frequency. Then you just reference your .dll wherever you want to use your classes.

here is a very simple example of what David suggests:

build this as dll from VS:

namespace GHscriptHelper
{
   public class passObjectAround
   {
      public int num;
      public string text;

      public passObjectAround(int val, string val2)
      {
         num = val;
         text = val2;
      }
   }
}

then reference the dll in both c#-components in the attached gh file.

Attachments:

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service