Nevermind, it looks like a good nights sleep took care of everything. I had
been using Lutz Roeder's .Net Reflector and handn't been looking at IL to
view the results of my Emit code. I hand been looking at C# output. I
switched to IL mode and from what you said, I was able to compare the IL I
was trying to get against other code I had written, and that did it.
Since I wasn't able to find any other examples of this anywhere, here's the
solution:
I had actually been trying to call the protected base constructor from an
inherited class. The base constructor had a parameter that I wanted to hide
but I wanted to expose the other parameters in a public constructor. This is
what I wanted to see:
public myClass(string SomeParam) : base ( typeof( someClass ), SomeParam ){}
Here's the Emit code to achieve the result:
------------------------------ BEGIN
CODE -------------------------------------------
//build a constructor which passes an argument of type Type to base
constructor by calling typeof
ConstructorBuilder cb = tb.DefineConstructor(MethodAttributes.Public,
CallingConventions.Standard, new Type[1]{typeof(string)});
ILGenerator ilGen = cb.GetILGenerator();
ilGen.Emit(OpCodes.Ldarg_0);
ilGen.Emit(OpCodes.Ldtoken, typeof(int));
ilGen.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle", new
Type[1]{typeof(RuntimeTypeHandle)}));
ilGen.Emit(OpCodes.Ldarg_1);
//pass arguments to base class constructor
ilGen.Emit(OpCodes.Call,
typeof(myParent).GetConstructor(BindingFlags.Insta nce |
BindingFlags.NonPublic, null, new Type[2]{typeof(Type), typeof(string)},
null));
ilGen.Emit(OpCodes.Ret);
------------------------------ END
CODE -----------------------------------------------
Here's the IL (using Lutz Roeder's Reflector):
----------------------- BEGIN IL ----------------------------------------
..method public specialname rtspecialname instance void .ctor(string) cil
managed
{
// Code Size: 18 byte(s)
..maxstack 3
L_0000: ldarg.0
L_0001: ldtoken int32
L_0006: call [mscorlib]System.Type
[mscorlib]System.Type::GetTypeFromHandle([mscorlib]System.RuntimeTypeHandle)
L_000b: ldarg.1
L_000c: call instance void
[HelloWorld-Emit]HelloWorld_Emit.myParent::.ctor([mscorlib]System.Type,
string)
L_0011: ret
}
--------------------END IL -----------------------------------------------
"Mark Miller" <bl***@blank.com> wrote in message
news:Oy**************@TK2MSFTNGP09.phx.gbl...
I'm having trouble translating your il into Emit commands. I didn't say
before but I am new to the Reflection.Emit namespace and so if you could
interpret the IL for me I would appreciate it.
thanks,
Mark