CSharp · Console
// C# Extension Method
// Doc: https://csharp-extension.com/en/method/1002629/type-createinstance
// @nuget: Z.ExtensionMethods
using System;
public class Program
{
public static void Main()
{
string parrot = "Parrot";
string owl = "Owl";
Type parrotType = Type.GetType(parrot);
Type owlType = Type.GetType(owl);
// C# Extension Method: Type - CreateInstance
var parrotObj = parrotType.CreateInstance<Parrot>(true) as Bird;
var owlObj = owlType.CreateInstance<Owl>(true) as Bird;
Console.WriteLine(parrotObj);
Console.WriteLine(owlObj);
}
}
public abstract class Bird
{
public override string ToString() { return "Type: " + GetType().Name; }
}
public class Parrot : Bird
{
}
public class Owl : Bird
{
}