Polyline and PolylineCurves, issue with invalid cast

Hello.
I´m very unsure how to deal with curves of the type Polyline and PolylineCurves.
I did this simple grafting component to reproduce the error I get in another script I’m working on.
For my understanding, if I pass a Referenced Polyline Curve to my Grasshopper component, it is of a Polyline Type. So I convert it with ToPolylineCurve () to a PolylineCurve type, so I could use the methods of the Curve Class. Now I graft it and output a tree of PolylineCurves.
I get the error -> 1. Invalid cast: Curve » PolylineCurve
Does anybody know what I’m doing wrong?

    protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
    {
        pManager.AddCurveParameter("iPolyline", "iPolyline", "iPolyline", GH_ParamAccess.list);
    }

    protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
    {
        pManager.AddCurveParameter("oPolylineCurves", "oPolylineCurves", "oPolylineCurves", GH_ParamAccess.tree);
    }

    protected override void SolveInstance(IGH_DataAccess DA)
    {
        List<Polyline> iPolylines = new List<Polyline>();
        DA.GetDataList("iPolylines", iPolylines);

        List<PolylineCurve> temp = new List<PolylineCurve>();

        for (int i = 0; i < iPolylines.Count; i++)
        {
            temp.Add(iPolylines[i].ToPolylineCurve());
        }

        DataTree<PolylineCurve> oPolylineCurves = new DataTree<PolylineCurve>();
        
        for(int i = 0; i < temp.Count; i++)
        {
            oPolylineCurves.AddRange(new List<PolylineCurve>() { temp[i] }, new GH_Path(i));
        }

        DA.SetDataTree(0, oPolylineCurves);

    }

Thanks for reading my post.
Best wishes,
Oliver

Most probably not all of your geometry are polylines. It’s always a good idea to never assume things, and test for validity. Try testing which polyline is actually a polyline and then call the ToPolylineCurve method. You can also have your list of type Curve, and after you call the ToPolylineCurve method cast it to a Curve using the as keyword . But if you send the rhino file I can help you solve the problem.

Thank you Nicholas for your tip.
The ToPolylineCurve Method does the trick.
Best wishes,
Oliver