title: What is the required parameter for casting C#
body: <p>Minimal reproducible example
  Base abstract class:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Xml.Linq;
  
  namespace YangHandlerTool
  {
  
      public abstract class YangNode
      {
          public string Name { get; set; }
  
          /// &lt;summary&gt;
          /// This is here to force YangNode constructor with Name parameter.
          /// &lt;/summary&gt;
          private YangNode() { }
          public YangNode(string name) { Name = name; }
      }
  }
  </code></pre>
  <p>Child that adds &quot;Type&quot; property</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Xml.Linq;
  
  namespace YangHandlerTool
  {
      public class Leaf : YangNode
      {
          public string Type { get; set; }
          public Leaf(string leafname) : base(leafname){ }
  
      }
  }
  </code></pre>
  <p>Other Child that adds &quot;Type&quot; property</p>
  <pre><code>using System;
  
  namespace YangHandlerTool
  
  {
      public class LeafList : YangNode
      {
          public string Type { get; set; }
          public LeafList(string leafname) : base(leafname) { }
  
      }
  }
  </code></pre>
  <p>Main</p>
  <pre><code>using System;
  using System.Collections.Generic;
  
  namespace YangHandlerTool
  
  {
      class Program
      {
          static void Main(string[] args)
          {
              List&lt;YangNode&gt; yangnodes = new List&lt;YangNode&gt;();
              yangnodes.Add(new Leaf(&quot;leafname&quot;));
              for (int i = 0; i &lt; yangnodes.Count; i++)
              {
                  if (IsLeaf(i))
                  {
                      ///I want to call SetTypeOfNode with the intention to cast to Leaf
                      SetTypeOfNode(yangnodes[i]);
                  }
                  if (IsLeafList(i))
                  {
                      ///I want to call SetTypeOfNode with the intention to cast to LeafList
                      SetTypeOfNode(yangnodes[i]);
                  }
              }
          }
  
          private static void SetTypeOfNode(YangNode inputnode)
          {
              ///Desired
              //Replace GIVENANYCLASSNAME with any given classname as parameter
              //((GIVENANYCLASSNAME)inputnode).Type = &quot;value&quot;;
  
                ((Leaf)inputnode).Type = &quot;value&quot;;
          }
  
          /// &lt;summary&gt;
          /// It is 100% guaranteed that the item is a Leaf.
          /// &lt;/summary&gt;
          private static bool IsLeaf(int index)
          {
              return index == 0;
          }
          private static bool IsLeafList(int index)
          {
              return index == 1;
          }
  
      }
  }
  </code></pre>
  <p>In the function &quot;private static void SetTypeOfNode(YangNode inputnode)&quot; I want to be able to give a Class as parameter what to cast to. In order to spare 100+ line in my actual program of casting like:</p>
  <pre><code>((Leaf)inputnode).Type = &quot;value&quot;;
  ((LeafList)inputnode).Type = &quot;value&quot;;
  ((AnothertypeInheritedfromYangnode)inputnode).Type = &quot;value&quot;;
  ((AnothertypeInheritedfromYangnode2)inputnode).Type = &quot;value&quot;;
  </code></pre>
  <p>...</p>
  <p>How can you pass a Classname as parameter that can be given into the casting parameter?</p>