Tutorial by Examples

For an instance of a type: var theString = "hello"; var theType = theString.GetType(); From the type itself: var theType = typeof(string);
using System; using System.Reflection; using System.Linq; public class Program { public static void Main() { var members = typeof(object) .GetMembers(BindingFlags.Public | BindingFlags.Static | ...
Get Instance method and invoke it using System; public class Program { public static void Main() { var theString = "hello"; var method = theString .GetType() .GetMethod("Substring", ...
Basic usage: PropertyInfo prop = myInstance.GetType().GetProperty("myProperty"); // get the value myInstance.myProperty object value = prop.GetValue(myInstance); int newValue = 1; // set the value myInstance.myProperty to newValue prop.setValue(myInstance, newValue); Setting rea...
Find properties with a custom attribute - MyAttribute var props = t.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).Where( prop => Attribute.IsDefined(prop, typeof(MyAttribute))); Find all custom attributes on a given property va...
Type type = obj.GetType(); //To restrict return properties. If all properties are required don't provide flag. BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; PropertyInfo[] properties = type.GetProperties(flags); foreach (PropertyInfo property in properties) { Console...
If you have an instance of a generic type but for some reason don't know the specific type, you might want to determine the generic arguments that were used to create this instance. Let's say someone created an instance of List<T> like that and passes it to a method: var myList = new List&lt...
Let's say you have class with generic methods. And you need to call its functions with reflection. public class Sample { public void GenericMethod<T>() { // ... } public static void StaticMethod<T>() { //... } } Let's say we want to...
var baseType = typeof(List<>); var genericType = baseType.MakeGenericType(typeof(String)); var instance = Activator.CreateInstance(genericType); var method = genericType.GetMethod("GetHashCode"); var result = method.Invoke(instance, new object[] { });
If you want your application to support a plug-in system, for example to load plug-ins from assemblies located in plugins folder: interface IPlugin { string PluginDescription { get; } void DoWork(); } This class would be located in a separate dll class HelloPlugin : IPlugin { ...
The simplest way is to use the Activator class. However, even though Activator performance have been improved since .NET 3.5, using Activator.CreateInstance() is bad option sometimes, due to (relatively) low performance: Test 1, Test 2, Test 3... With Activator class Type type = typeof(BigInteg...
To do this you need a reference to the assembly which contains the type. If you have another type available which you know is in the same assembly as the one you want you can do this: typeof(KnownType).Assembly.GetType(typeName); where typeName is the name of the type you are looking for (incl...
When performance is a concern, invoking a method via reflection (i.e. via the MethodInfo.Invoke method) is not ideal. However, it is relatively straightforward to obtain a more performant strongly-typed delegate using the Delegate.CreateDelegate function. The performance penalty for using reflecti...

Page 1 of 1