By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

Urgent help needed on Emit Dynamic Assembly

P: n/a
Hi,All MS Tech Gurus:
I need your help!!! It is the second time I post this message, I need get
some feedback ASAP, Please Help!!
Thanks a lot in advance.
John

I have a csharp method, using emit to dynamically generate classes & method
depends on the meta table in the database,here is my situation
1) In One method I generated the code Dynamic IL Code (Depends on the data
pass to it), The generated code works for one dataset but does not work for
another one, when it does not work, it generate the following error
Object reference not set to an instance of an object
2) The generate method which is not working on one machine is working fine
on a differennt computer with not code change
3) The generate method which is not working on one machine is working fine
on the same machine if run under using visual studio.net (both
debug mode & release mode), I have remove & reinstall VS 2004, it is still
the same.
4) I put some trace in the generated method, looks like the error happened
when the method was called, but has not execute anything on the method yet
(the was
not trace out
5) I am using the following code to call the generated method
public delegate void SetValue( DataRow rw, Object objrtn);
public SetValue delemethod=null;

private static void ExecSetValue(ArrayList arr, Object objrtn,CTask
objtask,DataRow rw,string task_id)
{
if (objtask.delemethod==null)
objtask.delemethod=BuildStaticDelegateMethod(arr,o bjrtn,task_id);

if (objtask.delemethod==null)
Trace.WriteLine("delemethod is NULLLLLLLLLLLL","ERROR");
objtask.delemethod(rw,objrtn);
}

}

6) I am using the following code to emit the IL code
static public SetSQLParam BuildStaticDelegateSQL(ArrayList objlstsp,Object
O,string task_id)
{

string strClassName="SQLExecSetSQL" + task_id;
TypeBuilder myTypeBuilder = myModule.DefineType(strClassName,
TypeAttributes.Public);

string strMethodName="SetValue" +task_id;

MethodBuilder ParMethod = myTypeBuilder.DefineMethod(strMethodName,
MethodAttributes.Public | MethodAttributes.Static , typeof(void),
new Type[] { typeof(Session),
typeof(Object),
typeof(SqlParameter[]),
typeof(CTask),
typeof(String)});

Label[] arrLoadValueString=new Label[objlstsp.Count];
Label[] arrSetValue=new Label[objlstsp.Count];
ILGenerator ILParaFunc= ParMethod.GetILGenerator(); // Create local
variables.
MethodInfo targetTrace = typeof(Trace).GetMethod("WriteLine", new Type[]
{typeof(String)} );

MethodInfo Methodgetitem = typeof(ArrayList).GetMethod("get_Item", new
Type[] {typeof(int)} );
MethodInfo MethodStringEq = typeof(String).GetMethod("op_Equality", new
Type[] {typeof(String),typeof(String)} );
MethodInfo MethodSetSPValue = typeof(SqlParameter).GetMethod("set_Value",
new Type[] {typeof(Object)} );
MethodInfo MethodXMLList = typeof(SQLExec).GetMethod("GetXMLInput", new
Type[]
{typeof(CTask),typeof(String),typeof(Object)} );
Type objType=typeof(Object);
if (O!=null)
objType=O.GetType();

FieldInfo fieldmap=objType.GetField("mapfield_name");
FieldInfo fieldstruct=objType.GetField("structurename");
for (int i=0;i<objlstsp.Count;i++)
{
arrLoadValueString[i]=ILParaFunc.DefineLabel();
arrSetValue[i]=ILParaFunc.DefineLabel();

CSqlParameter objsp=(CSqlParameter)objlstsp[i];
FieldInfo fieldvar;

ILParaFunc.Emit(OpCodes.Ldarg_2);
ILParaFunc.Emit(OpCodes.Ldc_I4,i);
ILParaFunc.Emit(OpCodes.Ldelem_Ref);
if (objsp.mapfield_name=="XMLLIST")
{
ILParaFunc.Emit(OpCodes.Ldarg_3);
ILParaFunc.Emit(OpCodes.Ldarg,4);
ILParaFunc.Emit(OpCodes.Ldarg_1);
ILParaFunc.Emit(OpCodes.Call, MethodXMLList );
ILParaFunc.Emit(OpCodes.Callvirt,MethodSetSPValue) ;
continue;

}
if (objsp.structurename=="Session")
{
fieldvar=typeof(Session).GetField(objsp.mapfield_n ame);
ILParaFunc.Emit(OpCodes.Ldarg_0);
}
else
{
fieldvar=objType.GetField(objsp.mapfield_name);
ILParaFunc.Emit(OpCodes.Ldarg_1);
}
ILParaFunc.Emit(OpCodes.Ldfld,fieldvar);
LocalBuilder objvalue= ILParaFunc.DeclareLocal(fieldvar.FieldType);
ILParaFunc.Emit(OpCodes.Stloc,objvalue);
ILParaFunc.Emit(OpCodes.Ldloc,objvalue);
if (fieldvar.FieldType.BaseType==typeof(System.ValueT ype) )
{
ILParaFunc.Emit(OpCodes.Box,fieldvar.FieldType);
}
else
{
ILParaFunc.Emit(OpCodes.Brtrue,arrLoadValueString[i]);
ILParaFunc.Emit(OpCodes.Ldstr,"");

ILParaFunc.Emit(OpCodes.Br,arrSetValue[i]);
ILParaFunc.MarkLabel(arrLoadValueString[i]);

ILParaFunc.Emit(OpCodes.Ldloc,objvalue);
}
ILParaFunc.MarkLabel(arrSetValue[i]);
ILParaFunc.Emit(OpCodes.Callvirt,MethodSetSPValue) ;
}
ILParaFunc.Emit(OpCodes.Ret );
return (SetSQLParam)Delegate.CreateDelegate(
typeof(SetSQLParam),
myTypeBuilder.CreateType(),
strMethodName );
7) The Generated IL code that is not working on one machines is

followingc.method
public static void SetValue18(class [System.Data]System.Data.DataRow A_0,
object A_1) cil managed
{
// Code size 355 (0x163)
.maxstack 8
.locals init (class
[DTO]Honda.CASS.DTO.GETEMPLOYEE_RPLY_DATAGETEMPLOYEE_RP LY V_0,
object V_1,
object V_2,
object V_3,
object V_4,
object V_5,
object V_6,
object V_7)
IL_0000: ldarg.1
IL_0001: castclass
[DTO]Honda.CASS.DTO.GETEMPLOYEE_RPLY_DATAGETEMPLOYEE_RP LY
IL_0006: stloc.0
IL_0007: ldstr "Inside Set Value Method"
IL_000c: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_0011: ldarg.0
IL_0012: ldstr "branch"
IL_0017: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_001c: stloc.1
IL_001d: ldloc.1
IL_001e: pop
IL_001f: ldloc.0
IL_0020: ldloc.1
IL_0021: callvirt instance string [mscorlib]System.Object::ToString()
IL_0026: stfld string
[DTO]Honda.CASS.DTO.GETEMPLOYEE_RPLY_DATAGETEMPLOYEE_RP LY::BRANCH
IL_002b: ldstr "Inside Set Value Method"
IL_0030: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_0035: ldarg.0
IL_0036: ldstr "BranchIdx"
IL_003b: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_0040: stloc.2
IL_0041: ldloc.2
IL_0042: callvirt bool [mscorlib]System.Convert::IsDBNull(object)
IL_0047: brtrue IL_006a
IL_004c: ldloc.0
IL_004d: ldloc.2
IL_004e: callvirt instance string [mscorlib]System.Object::ToString()
IL_0053: callvirt int64 [mscorlib]System.Int64::Parse(string)
IL_0058: br IL_0065
IL_005d: pop
IL_005e: ldloc.0
IL_005f: ldloc.2
IL_0060: callvirt instance string [mscorlib]System.Object::ToString()
IL_0065: stfld int64
[DTO]Honda.CASS.DTO.GETEMPLOYEE_RPLY_DATAGETEMPLOYEE_RP LY::BRANCHIDX
IL_006a: ldstr "Inside Set Value Method"
IL_006f: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_0074: ldarg.0
IL_0075: ldstr "first_name"
IL_007a: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_007f: stloc.3
IL_0080: ldloc.3
IL_0081: pop
IL_0082: ldloc.0
IL_0083: ldloc.3
IL_0084: callvirt instance string [mscorlib]System.Object::ToString()
IL_0089: stfld string
[DTO]Honda.CASS.DTO.GETEMPLOYEE_RPLY_DATAGETEMPLOYEE_RP LY::FIRSTNAME
IL_008e: ldstr "Inside Set Value Method"
IL_0093: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_0098: ldarg.0
IL_0099: ldstr "id"
IL_009e: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_00a3: stloc.s V_4
IL_00a5: ldloc.s V_4
IL_00a7: callvirt bool [mscorlib]System.Convert::IsDBNull(object)
IL_00ac: brtrue IL_00d1
IL_00b1: ldloc.0
IL_00b2: ldloc.s V_4
IL_00b4: callvirt instance string [mscorlib]System.Object::ToString()
IL_00b9: callvirt int64 [mscorlib]System.Int64::Parse(string)
IL_00be: br IL_00cc
IL_00c3: pop
IL_00c4: ldloc.0
IL_00c5: ldloc.s V_4
IL_00c7: callvirt instance string [mscorlib]System.Object::ToString()
IL_00cc: stfld int64
[DTO]Honda.CASS.DTO.GETEMPLOYEE_RPLY_DATAGETEMPLOYEE_RP LY::ID
IL_00d1: ldstr "Inside Set Value Method"
IL_00d6: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_00db: ldarg.0
IL_00dc: ldstr "last_name"
IL_00e1: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_00e6: stloc.s V_5
IL_00e8: ldloc.s V_5
IL_00ea: pop
IL_00eb: ldloc.0
IL_00ec: ldloc.s V_5
IL_00ee: callvirt instance string [mscorlib]System.Object::ToString()
IL_00f3: stfld string
[DTO]Honda.CASS.DTO.GETEMPLOYEE_RPLY_DATAGETEMPLOYEE_RP LY::LASTNAME
IL_00f8: ldstr "Inside Set Value Method"
IL_00fd: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_0102: ldarg.0
IL_0103: ldstr "login_name"
IL_0108: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_010d: stloc.s V_6
IL_010f: ldloc.s V_6
IL_0111: pop
IL_0112: ldloc.0
IL_0113: ldloc.s V_6
IL_0115: callvirt instance string [mscorlib]System.Object::ToString()
IL_011a: stfld string
[DTO]Honda.CASS.DTO.GETEMPLOYEE_RPLY_DATAGETEMPLOYEE_RP LY::LOGINNAME
IL_011f: ldstr "Inside Set Value Method"
IL_0124: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_0129: ldarg.0
IL_012a: ldstr "status"
IL_012f: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_0134: stloc.s V_7
IL_0136: ldloc.s V_7
IL_0138: callvirt bool [mscorlib]System.Convert::IsDBNull(object)
IL_013d: brtrue IL_0162
IL_0142: ldloc.0
IL_0143: ldloc.s V_7
IL_0145: callvirt instance string [mscorlib]System.Object::ToString()
IL_014a: callvirt int64 [mscorlib]System.Int64::Parse(string)
IL_014f: br IL_015d
IL_0154: pop
IL_0155: ldloc.0
IL_0156: ldloc.s V_7
IL_0158: callvirt instance string [mscorlib]System.Object::ToString()
IL_015d: stfld bool
[DTO]Honda.CASS.DTO.GETEMPLOYEE_RPLY_DATAGETEMPLOYEE_RP LY::ISACTIVE
IL_0162: ret
} // end of method SQLExecSet18::SetValue18
6) The Generated IL code that is working on all machines is following
..method public static void SetValue55(class
[System.Data]System.Data.DataRow A_0,
object A_1) cil managed
{
// Code size 589 (0x24d)
.maxstack 8
.locals init (class
[DTO]Honda.CASS.DTO.GETCASEBYWIPBIN_RPLY_DATAGETCASEBYW IPBIN_RPLY V_0,
object V_1,
object V_2,
object V_3,
object V_4,
object V_5,
object V_6,
object V_7,
object V_8,
object V_9,
object V_10,
object V_11,
object V_12,
object V_13)
IL_0000: ldarg.1
IL_0001: castclass
[DTO]Honda.CASS.DTO.GETCASEBYWIPBIN_RPLY_DATAGETCASEBYW IPBIN_RPLY
IL_0006: stloc.0
IL_0007: ldstr "Inside Set Value Method"
IL_000c: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_0011: ldarg.0
IL_0012: ldstr "condition"
IL_0017: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_001c: stloc.1
IL_001d: ldloc.1
IL_001e: pop
IL_001f: ldloc.0
IL_0020: ldloc.1
IL_0021: callvirt instance string [mscorlib]System.Object::ToString()
IL_0026: stfld string
[DTO]Honda.CASS.DTO.GETCASEBYWIPBIN_RPLY_DATAGETCASEBYW IPBIN_RPLY::CONDITION
IL_002b: ldstr "Inside Set Value Method"
IL_0030: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_0035: ldarg.0
IL_0036: ldstr "current_location"
IL_003b: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_0040: stloc.2
IL_0041: ldloc.2
IL_0042: pop
IL_0043: ldloc.0
IL_0044: ldloc.2
IL_0045: callvirt instance string [mscorlib]System.Object::ToString()
IL_004a: stfld string
[DTO]Honda.CASS.DTO.GETCASEBYWIPBIN_RPLY_DATAGETCASEBYW IPBIN_RPLY::CURRENTLO
CATION
IL_004f: ldstr "Inside Set Value Method"
IL_0054: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_0059: ldarg.0
IL_005a: ldstr "dayspassdue"
IL_005f: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_0064: stloc.3
IL_0065: ldloc.3
IL_0066: callvirt bool [mscorlib]System.Convert::IsDBNull(object)
IL_006b: brtrue IL_008e
IL_0070: ldloc.0
IL_0071: ldloc.3
IL_0072: callvirt instance string [mscorlib]System.Object::ToString()
IL_0077: callvirt int64 [mscorlib]System.Int64::Parse(string)
IL_007c: br IL_0089
IL_0081: pop
IL_0082: ldloc.0
IL_0083: ldloc.3
IL_0084: callvirt instance string [mscorlib]System.Object::ToString()
IL_0089: stfld int64
[DTO]Honda.CASS.DTO.GETCASEBYWIPBIN_RPLY_DATAGETCASEBYW IPBIN_RPLY::DAYSPASSD
UE
IL_008e: ldstr "Inside Set Value Method"
IL_0093: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_0098: ldarg.0
IL_0099: ldstr "elm_objid"
IL_009e: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_00a3: stloc.s V_4
IL_00a5: ldloc.s V_4
IL_00a7: callvirt bool [mscorlib]System.Convert::IsDBNull(object)
IL_00ac: brtrue IL_00d1
IL_00b1: ldloc.0
IL_00b2: ldloc.s V_4
IL_00b4: callvirt instance string [mscorlib]System.Object::ToString()
IL_00b9: callvirt int64 [mscorlib]System.Int64::Parse(string)
IL_00be: br IL_00cc
IL_00c3: pop
IL_00c4: ldloc.0
IL_00c5: ldloc.s V_4
IL_00c7: callvirt instance string [mscorlib]System.Object::ToString()
IL_00cc: stfld int64
[DTO]Honda.CASS.DTO.GETCASEBYWIPBIN_RPLY_DATAGETCASEBYW IPBIN_RPLY::ID
IL_00d1: ldstr "Inside Set Value Method"
IL_00d6: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_00db: ldarg.0
IL_00dc: ldstr "first_name"
IL_00e1: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_00e6: stloc.s V_5
IL_00e8: ldloc.s V_5
IL_00ea: pop
IL_00eb: ldloc.0
IL_00ec: ldloc.s V_5
IL_00ee: callvirt instance string [mscorlib]System.Object::ToString()
IL_00f3: stfld string
[DTO]Honda.CASS.DTO.GETCASEBYWIPBIN_RPLY_DATAGETCASEBYW IPBIN_RPLY::FIRSTNAME
IL_00f8: ldstr "Inside Set Value Method"
IL_00fd: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_0102: ldarg.0
IL_0103: ldstr "id_number"
IL_0108: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_010d: stloc.s V_6
IL_010f: ldloc.s V_6
IL_0111: pop
IL_0112: ldloc.0
IL_0113: ldloc.s V_6
IL_0115: callvirt instance string [mscorlib]System.Object::ToString()
IL_011a: stfld string
[DTO]Honda.CASS.DTO.GETCASEBYWIPBIN_RPLY_DATAGETCASEBYW IPBIN_RPLY::STRID
IL_011f: ldstr "Inside Set Value Method"
IL_0124: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_0129: ldarg.0
IL_012a: ldstr "last_name"
IL_012f: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_0134: stloc.s V_7
IL_0136: ldloc.s V_7
IL_0138: pop
IL_0139: ldloc.0
IL_013a: ldloc.s V_7
IL_013c: callvirt instance string [mscorlib]System.Object::ToString()
IL_0141: stfld string
[DTO]Honda.CASS.DTO.GETCASEBYWIPBIN_RPLY_DATAGETCASEBYW IPBIN_RPLY::LASTNAME
IL_0146: ldstr "Inside Set Value Method"
IL_014b: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_0150: ldarg.0
IL_0151: ldstr "next_contact_date"
IL_0156: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_015b: stloc.s V_8
IL_015d: ldloc.s V_8
IL_015f: callvirt bool [mscorlib]System.Convert::IsDBNull(object)
IL_0164: brtrue IL_0189
IL_0169: ldloc.0
IL_016a: ldloc.s V_8
IL_016c: callvirt instance string [mscorlib]System.Object::ToString()
IL_0171: callvirt valuetype [mscorlib]System.DateTime
[mscorlib]System.DateTime::Parse(string)
IL_0176: br IL_0184
IL_017b: pop
IL_017c: ldloc.0
IL_017d: ldloc.s V_8
IL_017f: callvirt instance string [mscorlib]System.Object::ToString()
IL_0184: stfld valuetype [mscorlib]System.DateTime
[DTO]Honda.CASS.DTO.GETCASEBYWIPBIN_RPLY_DATAGETCASEBYW IPBIN_RPLY::NEXTCONTA
CTDATE
IL_0189: ldstr "Inside Set Value Method"
IL_018e: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_0193: ldarg.0
IL_0194: ldstr "oldaccno"
IL_0199: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_019e: stloc.s V_9
IL_01a0: ldloc.s V_9
IL_01a2: pop
IL_01a3: ldloc.0
IL_01a4: ldloc.s V_9
IL_01a6: callvirt instance string [mscorlib]System.Object::ToString()
IL_01ab: stfld string
[DTO]Honda.CASS.DTO.GETCASEBYWIPBIN_RPLY_DATAGETCASEBYW IPBIN_RPLY::OLDACCOUN
TNUMBER
IL_01b0: ldstr "Inside Set Value Method"
IL_01b5: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_01ba: ldarg.0
IL_01bb: ldstr "orig_branch_no"
IL_01c0: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_01c5: stloc.s V_10
IL_01c7: ldloc.s V_10
IL_01c9: pop
IL_01ca: ldloc.0
IL_01cb: ldloc.s V_10
IL_01cd: callvirt instance string [mscorlib]System.Object::ToString()
IL_01d2: stfld string
[DTO]Honda.CASS.DTO.GETCASEBYWIPBIN_RPLY_DATAGETCASEBYW IPBIN_RPLY::ORIGINATI
NGBRANCH
IL_01d7: ldstr "Inside Set Value Method"
IL_01dc: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_01e1: ldarg.0
IL_01e2: ldstr "originator"
IL_01e7: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_01ec: stloc.s V_11
IL_01ee: ldloc.s V_11
IL_01f0: pop
IL_01f1: ldloc.0
IL_01f2: ldloc.s V_11
IL_01f4: callvirt instance string [mscorlib]System.Object::ToString()
IL_01f9: stfld string
[DTO]Honda.CASS.DTO.GETCASEBYWIPBIN_RPLY_DATAGETCASEBYW IPBIN_RPLY::ORIGINATO
R
IL_01fe: ldstr "Inside Set Value Method"
IL_0203: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_0208: ldarg.0
IL_0209: ldstr "title"
IL_020e: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_0213: stloc.s V_12
IL_0215: ldloc.s V_12
IL_0217: pop
IL_0218: ldloc.0
IL_0219: ldloc.s V_12
IL_021b: callvirt instance string [mscorlib]System.Object::ToString()
IL_0220: stfld string
[DTO]Honda.CASS.DTO.GETCASEBYWIPBIN_RPLY_DATAGETCASEBYW IPBIN_RPLY::TITLE
IL_0225: ldstr "Inside Set Value Method"
IL_022a: callvirt void
[System]System.Diagnostics.Trace::WriteLine(string)
IL_022f: ldarg.0
IL_0230: ldstr "type"
IL_0235: callvirt instance object
[System.Data]System.Data.DataRow::get_Item(string)
IL_023a: stloc.s V_13
IL_023c: ldloc.s V_13
IL_023e: pop
IL_023f: ldloc.0
IL_0240: ldloc.s V_13
IL_0242: callvirt instance string [mscorlib]System.Object::ToString()
IL_0247: stfld string
[DTO]Honda.CASS.DTO.GETCASEBYWIPBIN_RPLY_DATAGETCASEBYW IPBIN_RPLY::TYPE
IL_024c: ret
} // end of method SQLExecSet55::SetValue55
Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.