DEV Community

Discussion on: Coding Concepts - Reflection

Collapse
 
usmansabir profile image
Usman Sabir

2nd code example doesn't work

// Using Reflection to get information from an Assembly:

System.Reflection.Assembly integerTypeAssembly = typeof(System.Int32).Assembly;

System.Console.WriteLine(integerTypeAssembly); // mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

// We can then Create a new Instance of whatever this type may be, and call it's methods.
object integerInstance = Activator.CreateInstance(integerTypeAssembly);
// Now we can use integerInstance as if it was an Intger Type, calling its MaxCValue function.
System.Console.WriteLine(integerInstance.MaxValue());

Collapse
 
chris_bertrand profile image
Chris Bertrand

Hey Usman,

You're very much correct, I should of tested the snippet in .Net Fiddle before posting it. I wrote is as pseudo code, but can see how it could be misinterpreted. I've updated the example with one that actually works. Although the Integer class is a very boring example!