diff --git a/src/AST/TypeExtensions.cs b/src/AST/TypeExtensions.cs
index af22e298..d94551cf 100644
--- a/src/AST/TypeExtensions.cs
+++ b/src/AST/TypeExtensions.cs
@@ -439,18 +439,13 @@
             return false;
         }
 
-        public static bool IsTemplate(this Type type)
+        public static bool IsTemplateParameterType(this Type type)
         {
             if (type is TemplateParameterType or TemplateParameterSubstitutionType)
                 return true;
 
-            var ptr = type;
-            while (ptr is PointerType pType)
-            {
-                ptr = pType.Pointee;
-                if (ptr is TemplateParameterType or TemplateParameterSubstitutionType)
-                    return true;
-            }
+            if (type is PointerType pt)
+                return pt.GetFinalPointee() is TemplateParameterType or TemplateParameterSubstitutionType;
 
             return false;
         }
diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs
index dea8aa5d..eec484e0 100644
--- a/src/Generator/Generators/CSharp/CSharpMarshal.cs
+++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs
@@ -644,7 +644,7 @@ namespace CppSharp.Generators.CSharp
                 else
                 {
                     Context.Before.Write($"var {arg} = ");
-                    if (pointer.Pointee.IsTemplate())
+                    if (pointer.Pointee.IsTemplateParameterType())
                         Context.Before.Write($"(({Context.Parameter.Type}) (object) {Context.Parameter.Name})");
                     else
                         Context.Before.WriteLine(Context.Parameter.Name);
@@ -810,7 +810,7 @@ namespace CppSharp.Generators.CSharp
 
         private void MarshalValueClass()
         {
-            if (Context.Parameter.Type.IsTemplate())
+            if (Context.Parameter.Type.IsTemplateParameterType())
                 Context.Return.Write($"(({Context.Parameter.Type}) (object) {Context.Parameter.Name})");
             else
                 Context.Return.Write(Context.Parameter.Name);
diff --git a/src/Generator/Types/Std/Stdlib.CSharp.cs b/src/Generator/Types/Std/Stdlib.CSharp.cs
index c197a665..a2db882d 100644
--- a/src/Generator/Types/Std/Stdlib.CSharp.cs
+++ b/src/Generator/Types/Std/Stdlib.CSharp.cs
@@ -330,7 +330,7 @@ namespace CppSharp.Types.Std
                     Helpers.InternalStruct}.{assign.Name}(new {
                     typePrinter.IntPtrType}(&{
                     ctx.ReturnVarName}), ");
-                if (ctx.Parameter.Type.IsTemplate())
+                if (ctx.Parameter.Type.IsTemplateParameterType())
                     ctx.Return.Write("(string) (object) ");
                 ctx.Return.Write($"{ctx.Parameter.Name})");
                 ctx.ReturnVarName = string.Empty;
@@ -343,7 +343,7 @@ namespace CppSharp.Types.Std
 
                 ctx.Before.Write($@"{qualifiedBasicString}Extensions.{
                     assign.Name}({varBasicString}, ");
-                if (ctx.Parameter.Type.IsTemplate())
+                if (ctx.Parameter.Type.IsTemplateParameterType())
                     ctx.Before.Write("(string) (object) ");
                 ctx.Before.WriteLine($"{ctx.Parameter.Name});");