C# Reflection Notes

Below are some useful code snippets when using reflection in c#

How to pass in Parameters into an Activator

Stack Overflow

(T)Activator.CreateInstance(typeof(T), param1, param2);

Get a property value from a string

var value = (string)GetType().GetProperty("SomeProperty").GetValue(this, null);

Get all types that are in a namespace

var q = from t in Assembly.GetExecutingAssembly().GetTypes()
        where t.IsClass && t.Namespace == @"my.namespace"
        select t;
q.ToList().ForEach(t => Console.WriteLine(t.Name));

how-to-dynamically-create-generic-c-sharp-object-using-reflection

Dynamically created a generic c# object

var d1 = typeof(Task<>);
Type[] typeArgs = { typeof(Item) };
var makeme = d1.MakeGenericType(typeArgs);
object o = Activator.CreateInstance(makeme);