Overriding attributes of another component using C#

I came across this animation on the old Grasshopper forums, where it appears that the CanInsertParameter and CanRemoveParameter methods of the left component are being dynamically overridden by a C# script.

How would I go about achieving something similar? Can the methods of any component be overriden?

And what does the ‘overrides’ section in the C# scripting window actually affect? Using Reflection shows that this is of type Grasshopper.Kernel.GH_ScriptInstance, with attributes AfterRunScript, BeforeRunScript, ClippingBox, DrawViewportMeshes, DrawViewportWires etc. and inheriting directly from System.Object.
I can’t find any reference to it in the official SDK, or how to access the enclosing Component_CSNET_Script object in the manner of ghenv.Component in Python.

Any pointers in the right direction would be much appreciated! :slight_smile:

I have no idea.

No, typically it’s not possible at all. Maybe the python component is special in some way that enables this?

You can invoke methods on other objects, and sometimes modify values if those components expose public setters or if you’re willing to hack into it using Reflection.

The buttons that insert the override methods (which is what I assume you’re referring to) merely insert the correct signature for certain methods marked virtual in the script base class. By overriding them in your script, you can provide additional functionality beyond just the RunScript() method. If you wish to perform some sort of logic before the first call to RunScript or after the last call to RunScript (clearing caches, computing sum total, something like that) then you must override those methods. You could just type them by hand if you knew their signatures off the top of your head, but the buttons help you out here.

You’re only allowed to override methods that are marked virtual or abstract in the base class you’re deriving from. Methods in other classes are off limits.

A lot of classes and methods are omitted form the docs since they feature no xml comments.

Thanks for the reply!

I attempted to to override the C# component’s CanInsertParameter by inserting this in the ‘Additional Code’ section of the C# script:

public override bool CanInsertParameter(GH_ParameterSide side, int index){ 
    return false;

However this doesn’t work because the GH_ScriptInstance base class isn’t the one implementing IGH_VariableParameterComponent, and I can’t find a way of accessing the ‘parent’ component from within the script, in the same way I can do so in ghpython by calling:

this = ghenv.Component # => returns the GhPython.Component.ZuiPythonComponent instance

Yeah this is not possible, you do not have the rights within the script to change that method. It has already been compiled by the time your script is interpreted.

You can access the parent component via the Component field. This gives you access to an IGH_Component instance. However you’re not allowed to start overriding methods on that instance, it already exists. It cannot be changed.