For an experiment, i am trying to read the method body (using GetILAsByteArray()) from source type and adding it to the new Type (Using CreateMethodBody()).
My source class is simply this
public class FullClass
{
public string Test(string data)
{
return data;
}
public string Test2(string data)
{
return data;
}
public string Test5(string data, string data1)
{
return data + data1;
}
}
The IL generated for this code (taken using reflector)
.method public hidebysig instance string Test(string data) cil managed
{
.maxstack 1
.locals init (
[0] string CS$1$0000)
L_0000: nop
L_0001: ldarg.1
L_0002: stloc.0
L_0003: br.s L_0005
L_0005: ldloc.0
L_0006: ret
}
But the IL generated from my new type looks like this
.method public hidebysig virtual instance string Test(string) cil managed
{
.maxstack 0
L_0000: nop
L_0001: ldarg.1
L_0002: stloc.0
L_0003: br.s L_0005
L_0005: ldloc.0
L_0006: ret
}
Differences are maxstack value & .locals directive. I dont understand why my the actual class generates the locals, though it doesnt have any local variables??
And why the differences in .maxstack value,since am using the same IL from the source to create the new Type.?
Due to this am getting an error "Common Language Runtime detected an invalid program" while calling the method.
My code creating the Dynamic type looks like this
public static class Mixin<Target>
{
public static Target compose<TSource>()
{
Type newType = null;
AppDomain currentDom = Thread.GetDomain();
AssemblyName DAssembly = new AssemblyName();
DAssembly.Name = "DynamicTypesAssembly";
AssemblyBuilder DAssemblyBldr = currentDom.DefineDynamicAssembly(
DAssembly,
AssemblyBuilderAccess.RunAndSave);
ModuleBuilder DModuleBldr = DAssemblyBldr.DefineDynamicModule(DAssembly.Name, DAssembly.Name + ".dll", false);
// var DInterface = EmitInterface(DModuleBldr);
TypeBuilder TypeBldr = DModuleBldr.DefineType("WorkOut.DType",
TypeAttributes.Public | TypeAttributes.BeforeFieldInit | TypeAttributes.Serializable
,typeof(object), new[] { typeof(Target) });
//TypeBldr.AddInterfaceImplementation(typeof(DInterface));
var methodCol = typeof(Target).GetMethods(BindingFlags.Public| BindingFlags.Instance);
foreach (var ms in methodCol)
{
var paramCol = ms.GetParameters();
var paramTypeArray = paramCol.Select(x => x.ParameterType).ToArray();
var paramNameArray = paramCol.Select(x=>x.Name).ToArray();
MethodBuilder MthdBldr = TypeBldr.DefineMethod(ms.Name,
MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig,
ms.ReturnType,
paramTypeArray);
for(int i=0;i<paramCol.Count();i++)
{
MthdBldr.DefineParameter(i+1, ParameterAttributes.None, paramNameArray[i]);
}
MethodInfo[] methodInfos = typeof(TSource).GetMethods(BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Static | BindingFlags.Instance);
for (int i = 0; i < methodInfos.Count(); i++)
{
var paramSrc = methodInfos[i].GetParameters();
var paramSrcTypeArray = paramSrc.Select(x => x.ParameterType).ToArray();
if (methodInfos[i].Name == ms.Name && methodInfos[i].ReturnType == ms.ReturnType && paramSrc.Count() == paramCol.Count() && paramTypeArray.SequenceEqual(paramSrcTypeArray))
{
var ILcodes = methodInfos[i].GetMethodBody().GetILAsByteArray();
var ilGen = MthdBldr.GetILGenerator();
//ilGen.Emit(OpCodes.Ldarg_0); //Load the 'this' reference onto the evaluation stack
//ilGen.Emit(OpCodes.Initobj);
MthdBldr.CreateMethodBody(ILcodes, ILcodes.Length);
//ilGen.Emit(OpCodes.Ret);
break;
}
}
}
newType = TypeBldr.CreateType();
DAssemblyBldr.Save("a.dll");
return (Target)Activator.CreateInstance(newType);
}
And code for invoking this is
var resMix = Mixin<ITest>.compose<FullClass>();
var returned1 = resMix.Test("sam");
Edit: And the ITest (Target) interface is
public interface ITest
{
string Test(string data);
}
EDIT:
when commenting this line
//var ilGen = MthdBldr.GetILGenerator();
maxstack becomes .maxstack 16
I ran a check against the new dll against PEverify tool, this gives following error
WorkOut.DType::Test][offset 0x00000002] Unrecognized local variable number.
Any help really appreciated.... :)