๐ŸŽ“ A1Learner
๐Ÿ”
0 of 28 lessons completed

C# Reflection Creation

Reflection can create objects and call methods at run time. Activator builds an object from a Type, and MethodInfo calls a method on an object.

Creating an object with Activator

CreateInstance builds a Counter from its Type. A direct Inc call then raises its Count.

using System;

class Counter
{
    public int Count { get; set; }

    public void Inc()
    {
        Count = Count + 1;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Counter counter = (Counter)Activator.CreateInstance(typeof(Counter));
        counter.Inc();
        Console.WriteLine(counter.GetType().Name);
        Console.WriteLine(counter.Count);
    }
}

Calling a method with Invoke

GetMethod finds Inc as a MethodInfo. Invoke calls it twice on the same counter.

using System;
using System.Reflection;

class Counter
{
    public int Count { get; set; }

    public void Inc()
    {
        Count = Count + 1;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Counter counter = new Counter();
        MethodInfo method = typeof(Counter).GetMethod("Inc");
        method.Invoke(counter, null);
        method.Invoke(counter, null);
        Console.WriteLine(counter.Count);
    }
}

Building an object through a constructor

GetConstructor picks the constructor that takes a string and an int. Invoke runs it with two arguments.

using System;
using System.Reflection;

class Book
{
    public string Title;
    public int Pages;

    public Book(string title, int pages)
    {
        Title = title;
        Pages = pages;
    }
}

class Program
{
    static void Main(string[] args)
    {
        ConstructorInfo ctor = typeof(Book).GetConstructor(new Type[] { typeof(string), typeof(int) });
        Book book = (Book)ctor.Invoke(new object[] { "Dune", 400 });
        Console.WriteLine(book.Title);
        Console.WriteLine(book.Pages);
    }
}

Example

using System;
using System.Reflection;

class Greeter
{
    public string Hello(string name)
    {
        return "Hello, " + name;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Greeter greeter = (Greeter)Activator.CreateInstance(typeof(Greeter));
        MethodInfo method = typeof(Greeter).GetMethod("Hello");
        string line = (string)method.Invoke(greeter, new object[] { "Hana" });
        Console.WriteLine(line);
    }
}

Key points

  • Activator.CreateInstance creates an object from a Type argument.
  • CreateInstance returns object, so the result is cast to the real type.
  • GetMethod returns the MethodInfo for a named method.
  • MethodInfo.Invoke calls the method on a target object.
  • Invoke takes its arguments as an object array, or null when the method has none.
  • GetConstructor selects a constructor by its parameter types.

In this example, CreateInstance builds a Greeter without a direct constructor call. Invoke calls Hello with one argument in an object array. The returned string is cast and printed.

Note

Invoke returns object, so a returned value is cast before use.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does Activator.CreateInstance take as its argument?