Master advanced metaprogramming with reflection, custom attributes, and professional unit testing patterns.
Explore runtime type inspection, metadata, and test-driven development.
Type type = typeof(Person);
// or at runtime:
Type type = obj.GetType();
2. List properties and methods:
PropertyInfo[] props = type.GetProperties();
MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
3. Get and set property values dynamically:
PropertyInfo prop = type.GetProperty("Name");
prop.SetValue(obj, "Alice");
string name = (string)prop.GetValue(obj);
4. Invoke methods dynamically:
MethodInfo method = type.GetMethod("Greet");
object result = method.Invoke(obj, new object[] { "World" });
5. Create instances dynamically: Use Activator.CreateInstance.BindingFlags to filter public/private, static/instance membersGetProperties() returns only public properties by defaultMethodInfo when possibleType: Person
Properties: Name, Age
Methods: Greet, ToString
Name value: Alice
Age value: 30
Greet result: Hello, World!
typeof(Person) or obj.GetType()type.GetProperties()type.GetMethods(BindingFlags.DeclaredOnly | ...)method.Invoke(obj, new object[] { args })Review feedback below
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class InfoAttribute : Attribute
{
public string Author { get; }
public string Version { get; set; }
public InfoAttribute(string author) { Author = author; }
}
2. Apply attribute to class and methods:
[Info("Alice", Version = "1.0")]
public class Calculator
{
[Info("Bob")]
public int Add(int a, int b) => a + b;
}
3. Read attributes at runtime:
var attr = type.GetCustomAttribute<InfoAttribute>();
if (attr != null) Console.WriteLine(attr.Author);
4. Check for attribute presence: Use IsDefined or GetCustomAttributes.AttributeUsage controls where attribute can be appliedAllowMultiple = true lets same attribute be applied multiple timesGetCustomAttribute<T>() for cleaner codeClass attribute: Author=Alice, Version=1.0
Method 'Add' has InfoAttribute: True
Method attribute: Author=Bob, Version=
class MyAttr : Attribute { ... }[AttributeUsage(AttributeTargets.Class)][MyAttr("value")]type.GetCustomAttribute<MyAttr>()Review feedback below
static class Assert
{
public static void AreEqual<T>(T expected, T actual, string msg = "")
{
if (!Equals(expected, actual))
throw new Exception($"Assert failed: {msg} Expected {expected}, got {actual}");
}
public static void IsTrue(bool condition, string msg = "") { ... }
public static void Throws<T>(Action action) where T : Exception { ... }
}
2. Write tests with Arrange-Act-Assert:
static void TestAdd()
{
// Arrange
var calc = new Calculator();
// Act
int result = calc.Add(2, 3);
// Assert
Assert.AreEqual(5, result, "Add should return sum");
}
3. Test edge cases: Zero, negative numbers, boundaries.Running tests...
✓ TestAdd passed
✓ TestAddNegative passed
✓ TestDivide passed
✓ TestDivideByZeroThrows passed
4/4 tests passed
Assert.AreEqual(expected, actual)Assert.Throws<DivideByZeroException>(() => ...)Review feedback below