How to get tree access input in grasshopper component development?

Hi all,

can someone tell me how to access a tree as an input in visual studio.
I tried

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddGenericParameter("Tree", "Tr", "Tree", GH_ParamAccess.tree);
        }

Output:

protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("T", "T", "Tree", GH_ParamAccess.tree);
        }

but if i try to output the tree a error appears that ‘object’ cant be used as a type parameter ‘T’.
The Type should be strings ,doubles or integers so i thought generic is a good idea but there is no Grasshopper.Kernel.Types.GH_Generic.
As a python noob and c# supernoob all these type things are really confusing.

protected override void SolveInstance(IGH_DataAccess DA)
        {
            
            Grasshopper.DataTree<object> tr = new Grasshopper.DataTree<object>();

            if(!DA.GetDataTree<object>(0, out tr))return;

            Grasshopper.DataTree<object> tr_out = tr;

            DA.SetDataTree(0, tr_out);
        }

Is there any example code of a component where the input is a data tree ?.
If the c# script component is used there is no problem


Any tip how the datatree input can be accesses and outpouted as a component would be great.

You can’t use DataTree<T> in proper components, you have to use GH_Structure<T>, and here T has an IGH_Goo constraint. But you don’t have a choice, the type for T must match the type of the parameter, which is either going to be GH_Structure<IGH_Goo> or GH_Structure<GH_GenericData> (can’t remember which, almost certain it’s the first).

This seams to work thanks:

protected override void SolveInstance(IGH_DataAccess DA)
        {
            
            Grasshopper.Kernel.Data.GH_Structure < Grasshopper.Kernel.Types.IGH_Goo > tr = new Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.IGH_Goo>();

            if(!DA.GetDataTree<Grasshopper.Kernel.Types.IGH_Goo>(0, out tr))return;

            Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.IGH_Goo> tr_out = tr;

            DA.SetDataTree(0, tr_out);
        }

It’s an out parameter so you don’t have to construct it ahead of time.

protected override void SolveInstance(IGH_DataAccess DA)
{           
  GH_Structure<IGH_Goo> tree;
  DA.GetDataTree(0, out tree);
  DA.SetDataTree(0, tree);
}

Thanks