How to send C# class another components

I want to make a particle class that moves every second.
However, I don’t know how to exchange data within a class between components.
Is this a bad type specification?

—1st Components -----

List<Particle> particles = new List<Particle>();

for(int i=0; i<_pTotal; i++)
{
   var tmp = new Particle();
   Vector3d v = new Vector3d(1,1,1);
   tmp.setup(v);
   particles.Add(tmp);
}

A = particles;

—2nd Components -----
this code is error…

A = _p[0].getPos();

My Grasshopper file is :

CS_test.gh (11.1 KB)

Hi Fumi Anzai
You need to use reflection,like this


CS_test.gh (11.3 KB)

2 Likes

Dear NARUTO

Thanks your time and reply.
I didn’t understand about reflection in C#.
Your sample files are very helpful.
Thank you very much for your valuable response !

best,
Fumi

@603419608
Dear NARUTO

Today , I study reflection in C#.
Then I tried to create some functions and proceed.

bool getState(int _i)
{
   var types = _p.Select(p => p.GetType().GetMethod("getState")).ToList();
   bool b =  (bool)types[0].Invoke(_p[_i],null);
   return b;
}

However.
It is not possible to know the number of arrays
like a …

 _p.Length

and …
It takes time to learn how to write Invoke with arguments.
Is there any other way than C# reflection ?

Particle p = (Particle)_p[0];

Is it possible to cast like the above?
Please your advice.

best,
Fumi

2 Likes

@Dani_Abalde
Thanks your addvice !
I try your link of topics too.
using anonymous method and dll.

Hi,

the reason why you cannot pass classes from one script component to another is due to the fact that it does not know the exact type because it’s missing the right ‘using’ reference…

Any script component has a name with a unique ID. Something like (…)ScriptComponent325346346346(…)MyClass… i believe

Now you can try to find that. It’s in the memory and accessible. Maybe I’ll can try to give an example later, or you are using Tuples and Func<>/Action<> delegates to be passed over. Last but not least you can use Reflection, which is in use by both examples… This is terrible for performance critical use-cases like ‘Particles’. There is also a couple of boxing and unboxing going on if you work with ‘object’.

However, something to note: Generally seen it is about making a bad hack in any case. Write a custom plugin or a library instead, and access your object directly from there…

1 Like

@TomTom
Thanks your reply and time!
Your comment was very helpful :grinning: