Type, MethodInfo, activation, custom attributes
Get Type objects, locate MethodInfo, invoke methods, create instances with Activator, and define/read custom attributes.
Type, MethodInfo, activation, custom attributes is a free C# Academy lesson on CoddyKit. This is lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 3 lessons in total.
Overview
Goal: Use reflection safely.
- Get Type
- Find MethodInfo
- Create via Activator
- Define & read attributes
Get Type
Get Type from typeof, an instance with GetType(), or by name via Type.GetType.
using System;
public class Sample { }
public class Program
{
public static void Main(string[] args)
{
// 1) From a compile-time type
Type t1 = typeof(Sample);
// 2) From an instance
Sample s = new Sample();
Type t2 = s.GetType();
// 3) From a full name (needs assembly-qualified name for external types)
Type t3 = Type.GetType("System.String");
Console.WriteLine(t1.Name + ", " + t2.FullName + ", " + (t3 == null ? "null" : t3.Name));
}
}
MethodInfo.Invoke
Use GetMethod to locate a method and Invoke to call it. Pass instance and argument array.
using System;
using System.Reflection;
public class Greeter
{
public string Hello(string name) { return "Hello, " + name; }
}
public class Program
{
public static void Main(string[] args)
{
Greeter g = new Greeter();
Type t = typeof(Greeter);
MethodInfo mi = t.GetMethod("Hello"); // public instance method
object result = mi.Invoke(g, new object[] { "Ada" }); // call dynamically
Console.WriteLine(result); // Hello, Ada
}
}
Activator basics
Activator.CreateInstance(Type) constructs an object using the public parameterless constructor.
using System;
public class Person
{
public string Name { get; set; }
public Person() { Name = "Unknown"; } // parameterless ctor
}
public class Program
{
public static void Main(string[] args)
{
Type tp = typeof(Person);
object o = Activator.CreateInstance(tp); // uses public parameterless ctor
Person p = (Person)o;
p.Name = "Alan";
Console.WriteLine("Created: " + p.Name);
}
}
Custom attributes
Create a small Attribute class and retrieve it via GetCustomAttributes on the type or method.
using System;
using System.Reflection;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class LabelAttribute : Attribute
{
public string Text { get; private set; }
public LabelAttribute(string text) { Text = text; }
}
[Label("Utility class")]
public class Utils
{
[Label("Adds two ints")]
public static int Add(int a, int b) { return a + b; }
}
public class Program
{
public static void Main(string[] args)
{
Type t = typeof(Utils);
// Read class attribute
object[] classAttrs = t.GetCustomAttributes(typeof(LabelAttribute), false);
if (classAttrs.Length > 0)
Console.WriteLine("Class label: " + ((LabelAttribute)classAttrs[0]).Text);
// Read method attribute
MethodInfo mi = t.GetMethod("Add");
object[] methodAttrs = mi.GetCustomAttributes(typeof(LabelAttribute), false);
if (methodAttrs.Length > 0)
Console.WriteLine("Method label: " + ((LabelAttribute)methodAttrs[0]).Text);
}
}
Tips & safety
Tips:
- Check for null from Type.GetType and missing members.
- Prefer typeof when possible (faster, safer).
- Keep reflection usage centralized and well-documented.
- Use BindingFlags for non-public or static lookups.
Activation basics
Recap
Recap: Get a Type, find MethodInfo, create with Activator, and read attributes using GetCustomAttributes.
Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “Type, MethodInfo, activation, custom attributes” lesson free?
Yes — the full text of “Type, MethodInfo, activation, custom attributes” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 3 lessons in total.
What will I learn in “Type, MethodInfo, activation, custom attributes”?
Get Type objects, locate MethodInfo, invoke methods, create instances with Activator, and define/read custom attributes. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.
How long does the “Type, MethodInfo, activation, custom attributes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Type, MethodInfo, activation, custom attributes
- Source-level info (Caller attributes)
- Light metaprogramming scenarios