22.3. Invoking Methods and Setting Fields and Properties

So far we have seen how to query, search, and retrieve specific type members or a collection of type members. The true power of reflection lies in invoking those methods and constructors and setting those properties and fields. Listing 22.4 shows invoke operations on the System.String class.

Listing 22.4. Invoke Operations Using Reflection (C#)
 using System; using System.Reflection; class InvocationTest { public static void Main(string[] args) { Type t = typeof (System.String); CallConstructor(t); CallMethod(t); GetProperty(t); } private static void CallConstructor(Type t) { //Tried to get the default no-args public constructor. // String s = new String(char[] c); ConstructorInfo constructor ...

Get .NET for Java Developers: Migrating to C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.