Class extensions C# component

Hi guys,
I am curious to know what is the proper way to extend a RhinoCommon class. I know the general c# procedure, but I am particularly interested in knowing if I extend a class, for example Vector3d, if the c# component in gh will still be able to read the extension.

Thanks!

Nicholas

If you refer to the dll/.gha with “using” inside a GH Script component, then you will be able to access (from GH) anything you hack together with C# and compile into your own dll/gha, no problem.

// Rolf

Hi @n.rawitscher954,

You to “add” methods to existing types without creating a new derived ones with Extension Methods. For example, here is an extension method that extends the Vector3d struct:

public static class ExtensionMethods
{
  /// <summary>
  /// Returns the triple product.
  /// The scalar triple product (also called the mixed product, box product, or triple scalar product) 
  /// is defined as the dot product of one of the vectors with the cross product of the other two.
  /// </summary>
  /// <param name="a">This vector.</param>
  /// <param name="b">The second vector.</param>
  /// <param name="c">The third vector.</param>
  /// <returns>The triple product.</returns>
  public static double TripleProduct(this Vector3d a, Vector3d b, Vector3d c)
  {
    return a.X * 
            (b.Y * c.Z - b.Z * c.Y) + a.Y * 
            (b.Z * c.X - b.X * c.Z) + a.Z * 
            (b.X * c.Y - b.Y * c.X);
  }
}

Keep on mind that Extension Methods are a special kind of static method - they are called as if they were instance methods on the extended type. Thus, they don’t actually modify the base object they are extending. Thus, any extension method you create will only be useable by use, unless you provide this method in a sharable assembly.

Does this help?

– Dale

1 Like

Hi Dale,
Thank you for your reply. Yes I know the protocol, of extending a class, but I cant do it within a c# component because its already within another class. So I am curious to know the workflow of extending a class within Visual Studio so the c# component can recognize the extended class. I have done this many times within generic c# files in Visual Studio and also in Unity 3D.

Thank you for your time!

Nicholas

Hi Rolf,
thanks also for your reply. If I understood you correctly, I can’t define a using statement with the component, could you please clarify a little what was the steps you were explaining?

Thanks!

Nicholas

Hi @n.rawitscher954,

I guess I need clarification on this because you can declare classes in a C# component.

Perhaps more information on what you want to do (and why) would be helpful?

– Dale

1 Like

That’s wonderful! I never knew you could also declare classes up there!. That’s is all I wanted to know

Thank you for your time,

Nicholas