Activator.CreateInstance and beyond

Q: Assume we have 2000 unknown types; (however) we know each type has a constructor with integer as its’ only parameter type. How to create objects 10000 times for each type (and make such late-new fast)? Activator.CreateInstance comes to my fingers first. It is just so convenient to use: calling Activator.CreateInstance(type, new object[] {100}) in…

12

LateNew source

The following code is for the post “Activator.CreateInstance and beyond“ **All source code is provided as is, with no warranties intended or implied. Use at your own risk.**Instruction perl sample.pl > sample.cs csc /t:library sample.cs csc /o+ latenew.cs To run: latenew sample.dll 1000 1000 sample.plfor $i (1..2000) { $name = “C$i”; print “public class $name…

0

DebuggerVisualizer for DynamicMethod (Show me the IL)

Have you ever tried DynamicMethod, one of the coolest features in the .NET 2.0? Hope you have; otherwise, you can try it now with the examples 1, 2. Like emitting IL to MethodBuilder, we first get ILGenerator from a DynamicMethod, and make a series of ILGenerator.Emit calls to emit the IL instructions. If the emit…

11

Read IL from MethodBody

Reflection in .NET 2.0 ships with a new class MethodBody, which “provides access to information about the local variables and exception-handling clauses in a method body, and to the Microsoft intermediate language (MSIL) that makes up the method body”. (Thanks to Glenn, who wrote this in the MSDN doc; I simply copy & paste it)….

4

Nested Types in Generic Classes

In C#, nested types can be defined in generic classes just like they would be in non-generic classes. For example: class G<T> {   public class NestedC { }   public enum NestedEnum { A, B } } Inside the nested type NestedC, we can still use type parameter T, which was “declared” with class…

1

No LocalVariableInfo.Name?

This was one ladybug in MSDN product feedback center. First of all, thanks to those who filed bugs or suggestions through it, which really help improve our product quality. No Name property in the LocalVariableInfo class is “by design” (possibly forever). Inside a .Net assembly, there is no metadata (or table) keeping this information. However,…

0

Get Names and Values of Enum Members

Yes, reflection can get this done. Assume we have an Enum type – Colors, the following code will print the name and value for each member of it. foreach (FieldInfo fi in typeof(Colors).GetFields(BindingFlags.Public | BindingFlags.Static)) {    Console.WriteLine(fi.Name);    Console.WriteLine(fi.GetValue(null)); } Note we need to specify BindingFlags.Static explicitly here; Otherwise, both static and instance fields…

2

Reflection and Nullable<T>

You might have already read this: VS2005 made the last-minute DCR related to boxed Nullable<T>. Runtime now treats Nullable<T> differently from other generic value types when boxing: Int32? x = null; object y = x;  // y is simply null, not a truly boxed “nullable<int> struct” (which then tells it has no value) And Int32…

4

Type.GetType(string typeName) returns null !?

Type.GetType gives us the ability to get type back from a string. We pass a well-written type name string to it, and expect it return that type. Sometimes, to your surprise, it returns null. For example, Type.GetType(“System.Data.SqlClient.SqlException”). If the assembly name is specified in the typeName string, Type.GetType() will search inside this assembly only; otherwise,…

7