How to use a GeometryBase class's object?

I have a question about using GeometryBase class’s object.

I’ d like to make a simply volume measuring component by C# for study.
I made a following script and I got volume about Closed Breps and Meshs.
However, the script couldn’t get Sphere’s and Box’s volume and got the message: “error:object reference not set to an instance of an object”.
Do you know better way to solve?
Should I add statements to cast a GeometryBase class’s object to Sphere or Box one?

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
    {
        pManager.AddGeometryParameter("GeometryBase", "G", "", GH_ParamAccess.item);
    }

    protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
    {
        pManager.AddNumberParameter("Volume", "V", "", GH_ParamAccess.item);
    }

    protected override void SolveInstance(IGH_DataAccess DA)
    {
        GeometryBase shape = null;

        double d = new double();
        if (!DA.GetData<GeometryBase>(0, ref shape)) return;

        if (shape is Brep)
        {
            Brep brep = new Brep();
            brep = shape as Brep;
            d = brep.GetVolume();
        }
        else
        {
            Mesh mesh = new Mesh();
            mesh = shape as Mesh;
            d = mesh.Volume();
        }
        
        DA.SetData(0, d);
    }

See attached.

GeometryBase_AV_V1.3dm (486.6 KB) GeometryBase_AV_V1.gh (8.7 KB)

BTW: Speaking about GB class you may also find this useful (includes a query challenge):

GeometryBaseUnbox_V1A.3dm (704.0 KB) GeometryBaseUnbox_V1A.gh (123.7 KB)

Thanks to you, I’ m starting understand about GeometryBase.

And, I noticed a difference between components made on GH and one made on Visual studio.
I made same components on GH and Visual studio.
These components simply return the type of an input object.
The difference is that GH component regard Sphere as Brep but Visual studio one couldn’t do that.
This is why I couldn’t measure the volume of sphere by Visual studio one.

Do you know why the difference made?

I upload a zip file include following files.
geometrybase.gh : This file include the two components.
test.gba : This file include the component made on Visual studio. Please set this on your components library before you open gh file.
test.sin : This is project file on Visual studio. There is a script in this file.

GeometryBase_test.zip (7.8 KB)

Well … for simple/medium complexity things - say code around the 200-300 lines mark - I can hardly justify the usage of VS (But the build-in editor is indeed 100% crap).

Hi, not sure if there is a simpler way, but you could cast from IGH_GeometryGoo to GeometryBase and then to Brep or Mesh.

 protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddGeometryParameter("GeometryBase", "G", "", GH_ParamAccess.item);
        }

        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
            pManager.AddNumberParameter("Volume", "V", "", GH_ParamAccess.item);
        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            IGH_GeometricGoo shape = null;           
            if (!DA.GetData<IGH_GeometricGoo>(0, ref shape)) return;

            IGH_GeometricGoo geoGoo = shape;
            if (shape is Mesh || shape is GH_Mesh)
            {
                var geobase = GH_Convert.ToGeometryBase(geoGoo);
                Mesh mesh = geobase as Mesh;
                DA.SetData(0, mesh.Volume());                
            }
            else
            {
                var geobase = GH_Convert.ToGeometryBase(geoGoo);
                Brep brep = geobase as Brep;
                DA.SetData(0, brep.GetVolume());
            }
        }

PS. You should paste this ones:

using GH_IO;
using GH_IO.Serialization;
using Grasshopper;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;

As @Baris mentioned, GH componets use IGH_GeometricGoo and IGH_Goo which handle a lot of cases etc. Although, David did say that GH2 is getting rid of this IGH_ stuff.

Thanks everyone!
I’m ashamed to say, I didn’t know IGH_GeometricGoo and IGH_Goo classes.
It might be useless by GH2 but I’ll look how these classes work!