473,586 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Types with Reflection.Emit , how to ?

Hi,

In c#, I need to dynamically create types at runtime that will consist of
the following:
- inherits from a given interface

- will have a constructor with an int argument

- Will have an event handler (derived from the interface)

- Will have a method that will return the value of the property or
properties based on firing the event handler and the hooked to the event
will return the value.
The prototype is something like the following:
// Event args for the event handler
public class PropertyValueEv entArgs : EventArgs
{
int _recordNo;
public int RecordNo
{
get {return _recordNo;}
}

string _propertyName;
public string PropertyName
{
get {return _propertyName;}
}

Type _propertyType;
public Type PropertyType
{
get {return _propertyType;}
}

object _value;
public object Value
{
get {return _value;}
set {_value = value;}
}

public PropertyValueEv entArgs(int recordNo, string propertyName, Type
propertyType, object value)
{
_recordNo = recordNo;
_propertyName = propertyName;
_propertyType = propertyType;
_value = value;
}
}

// delegate for the event handler

public delegate void PropertyValueEv entHandler(obje ct sender,
PropertyValueEv entArgs e);

// The interfase that must be implemented by the dynamic type
public interface IDynamicDataFie lds
{
event PropertyValueEv entHandler GetPropertyValu e;
}

// note: the above two are already defined in other assembly
The prototye class that must be created at runtime :
public class LayerFields : IDynamicDataFie lds
{
// a private field
int _recordNo;

// the event
public event PropertyValueEv entHandler GetPropertyValu e;

// the constructor
public LayerFields(int recordNo)
{
_recordNo = recordNo;
}
// return value for the property based on the event
object _getPropertyVal ue(string propertyName, Type propertyType)
{
if(GetPropertyV alue == null) return null;

PropertyValueEv entArgs e = new PropertyValueEv entArgs(_record No,
propertyName, propertyType, null);
GetPropertyValu e(this, e);
return e.Value;
}
// here the list of variable number of properties of type string, int,
bool, and so on.

public string StringProp
{
get {return (string)_getPro pertyValue("Str ingProp", typeof(string)) ;}
}

public int IntProp
{
get (return (int)_getProper tyValue("IntPro p", typeof(int));}
}

... and so on
}
This is the opcode generated by ildasm.exe

********* constructor

..method public hidebysig specialname rtspecialname
instance void .ctor(int32 recordNo) cil managed
{
// Code size 14 (0xe)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object:: .ctor()
IL_0006: ldarg.0
IL_0007: ldarg.1
IL_0008: stfld int32 MapSolver.Dynam icClasses.Layer Fields::_record No
IL_000d: ret
} // end of method LayerFields::.c tor
********** _getPropertyVal ue method
..method private hidebysig instance object
_getPropertyVal ue(string propertyName,
class [mscorlib]System.Type propertyType) cil
managed
{
// Code size 51 (0x33)
.maxstack 5
.locals ([0] class [MyAssembly]MyAssembly.Prop ertyValueEventA rgs e,
[1] object CS$00000003$000 00000)
IL_0000: ldarg.0
IL_0001: ldfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0006: brtrue.s IL_000c
IL_0008: ldnull
IL_0009: stloc.1
IL_000a: br.s IL_0031
IL_000c: ldarg.0
IL_000d: ldfld int32 MapSolver.Dynam icClasses.Layer Fields::_record No
IL_0012: ldarg.1
IL_0013: ldarg.2
IL_0014: ldnull
IL_0015: newobj instance void
[MyAssembly]MyAssembly.Prop ertyValueEventA rgs::.ctor(int3 2,

string,

class [mscorlib]System.Type,

object)
IL_001a: stloc.0
IL_001b: ldarg.0
IL_001c: ldfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0021: ldarg.0
IL_0022: ldloc.0
IL_0023: callvirt instance void
[MyAssembly]MyAssembly.Prop ertyValueEventH andler::Invoke( object,

class [MyAssembly]MyAssembly.Prop ertyValueEventA rgs)
IL_0028: ldloc.0
IL_0029: callvirt instance object
[MyAssembly]MyAssembly.Prop ertyValueEventA rgs::get_Value( )
IL_002e: stloc.1
IL_002f: br.s IL_0031
IL_0031: ldloc.1
IL_0032: ret
} // end of method LayerFields::_g etPropertyValue
*************** * return the property value
..method public hidebysig specialname instance string
get_StringProp( ) cil managed
{
// Code size 31 (0x1f)
.maxstack 3
.locals ([0] string CS$00000003$000 00000)
IL_0000: ldarg.0
IL_0001: ldstr "StringProp "
IL_0006: ldtoken [mscorlib]System.String
IL_000b: call class [mscorlib]System.Type
[mscorlib]System.Type::Ge tTypeFromHandle (valuetype
[mscorlib]System.RuntimeT ypeHandle)
IL_0010: call instance object
MapSolver.Dynam icClasses.Layer Fields::_getPro pertyValue(stri ng,

class [mscorlib]System.Type)
IL_0015: castclass [mscorlib]System.String
IL_001a: stloc.0
IL_001b: br.s IL_001d
IL_001d: ldloc.0
IL_001e: ret
} // end of method LayerFields::ge t_COLONIA

*************** Is this needed to code this and how ?

..method public hidebysig newslot specialname virtual final
instance void add_GetProperty Value(class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler 'value') cil managed
synchronized
{
// Code size 24 (0x18)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.0
IL_0002: ldfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0007: ldarg.1
IL_0008: call class [mscorlib]System.Delegate
[mscorlib]System.Delegate ::Combine(class [mscorlib]System.Delegate ,

class [mscorlib]System.Delegate )
IL_000d: castclass [MyAssembly]MyAssembly.Prop ertyValueEventH andler
IL_0012: stfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0017: ret
} // end of method LayerFields::ad d_GetPropertyVa lue
*************** * this is the counterpart to add_GetProperty Value

..method public hidebysig newslot specialname virtual final
instance void remove_GetPrope rtyValue(class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler 'value') cil managed
synchronized
{
// Code size 24 (0x18)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.0
IL_0002: ldfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0007: ldarg.1
IL_0008: call class [mscorlib]System.Delegate
[mscorlib]System.Delegate ::Remove(class [mscorlib]System.Delegate ,

class [mscorlib]System.Delegate )
IL_000d: castclass [MyAssembly]MyAssembly.Prop ertyValueEventH andler
IL_0012: stfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0017: ret
} // end of method LayerFields::re move_GetPropert yValue

Up to now, I have created the following code, but I am stuck in the marked
code:
// create a dynamic assembly and module
AssemblyName an = new AssemblyName();
an.Name = "DynamicDat a";
AssemblyBuilder ab =
System.AppDomai n.CurrentDomain .DefineDynamicA ssembly(an,
AssemblyBuilder Access.Run);
ModuleBuilder module = ab.DefineDynami cModule(ab.GetN ame().Name, false);

// create a new type to hold our Main method
TypeBuilder typeBuilder = module.DefineTy pe("TableFields ",
TypeAttributes. Public | TypeAttributes. Class |
TypeAttributes. AutoClass | TypeAttributes. AnsiClass |
TypeAttributes. BeforeFieldInit | TypeAttributes. AutoLayout,
typeof(object), new Type[]{typeof(MyAssem bly.IDynamicDat aFields)});

// Define three fields for this type

// field for the record number
FieldBuilder recordNo = typeBuilder.Def ineField("_reco rdNo", typeof(int),
FieldAttributes .Private);

// field for GetPropertyValu e
FieldBuilder getPropertyValu e =
typeBuilder.Def ineField("GetPr opertyValue",
typeof(MyAssemb ly.PropertyValu eEventHandler), FieldAttributes .Public);

// define the constructor

// el constructor que recibe el numero de registro
ConstructorBuil der cb =
typeBuilder.Def ineConstructor( MethodAttribute s.Public |
MethodAttribute s.HideBySig |
MethodAttribute s.SpecialName | MethodAttribute s.RTSpecialName ,
CallingConventi ons.Standard,
new Type[]{typeof(int)});

ILGenerator ilgen = cb.GetILGenerat or();

// call the base constructor
// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// call base constructor
ilgen.Emit(OpCo des.Call, typeof(object). GetConstructor( new Type[0]));

// now save the record number in field "_recordNo"

// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// load first logical argument
ilgen.Emit(OpCo des.Ldarg_1);
// store in field _recordNo
ilgen.Emit(OpCo des.Stfld, recordNo);

// emit return code
ilgen.Emit(OpCo des.Ret);
// define the private method GetPropertyValu e
MethodBuilder mb = typeBuilder.Def ineMethod("_get PropertyValue",
MethodAttribute s.Private | MethodAttribute s.HideBySig,
CallingConventi ons.Standard,
typeof(object), new Type[]{typeof(string) , typeof(System.T ype)});

ilgen = mb.GetILGenerat or();

// declare local variables
ilgen.DeclareLo cal(typeof(MyAs sembly.Property ValueEventArgs) );
ilgen.DeclareLo cal(typeof(obje ct));

// define "notnull" label
Label notnull_label = ilgen.DefineLab el();
Label exit_label = ilgen.DefineLab el();

// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// load "_getPropertyVa lue" field
ilgen.Emit(OpCo des.Ldfld, getPropertyValu e);
// branch if != null
ilgen.Emit(OpCo des.Brtrue_S, notnull_label);
// load null
ilgen.Emit(OpCo des.Ldnull);
// set local variable 1 (result) to null
ilgen.Emit(OpCo des.Stloc_1);
// jump to exit_label
ilgen.Emit(OpCo des.Br_S, exit_label);

// mark "notnull_la bel
ilgen.MarkLabel (notnull_label) ;

// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// load _recordNo private type variable
ilgen.Emit(OpCo des.Ldfld, recordNo);
// load first argument - "propertyNa me"
ilgen.Emit(OpCo des.Ldarg_1);
// load second argument - "propertyTy pe"
ilgen.Emit(OpCo des.Ldarg_2);
// load "null" as the default value of the property
ilgen.Emit(OpCo des.Ldnull);
// create the MyAssembly.Prop ertyValueEventA rgs argument
ilgen.Emit(OpCo des.Newobj,
typeof(MyAssemb ly.PropertyValu eEventArgs).Get Constructor(new
Type[]{typeof(int),
typeof(string), typeof(System.T ype), typeof(object)} ));
// store in local variable 0
ilgen.Emit(OpCo des.Stloc_0);

// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// load field getPropertyValu e
ilgen.Emit(OpCo des.Ldfld, getPropertyValu e);
// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// load local variable 0 - MyAssembly.Prop ertyValueEventA rgs
ilgen.Emit(OpCo des.Ldloc_0);
// invoke the event handler in property
//***** I don't know how to code this line that corresponds to the below
line ==>> ilgen.Emit(OpCo des.Callvirt, how to ?
IL_0023: callvirt instance void
[MyAssembly]MyAssembly.Prop ertyValueEventH andler::Invoke( object, class
[MyAssembly]MyAssembly.Prop ertyValueEventA rgs)

// load event args
ilgen.Emit(OpCo des.Ldloc_0);
ilgen.Emit(OpCo des.Callvirt,
typeof(MyAssemb ly.PropertyValu eEventArgs).Get Method("get_Val ue"));
ilgen.Emit(OpCo des.Stloc_1);
ilgen.Emit(OpCo des.Br_S, exit_label);
// mark label "exit"
ilgen.MarkLabel (exit_label);
ilgen.Emit(OpCo des.Ldloc_1);
// return
ilgen.Emit(OpCo des.Ret);


// now populate the list of properties from a database list of fields
for(int i = 0; i < DbTable.Fields. Count; i++)
{
Type t = null;
// map to our field types
switch(DbTable. Fields[i].FieldType)
{
case FieldType.Strin g:
t = Type.GetType("S ystem.String");
break;
case FieldType.Byte:
t=Type.GetType( "System.Byt e");
break;
case FieldType.Char:
t=Type.GetType( "System.Cha r");
break;
case FieldType.Int16 :
t=Type.GetType( "System.Int16") ;
break;
case FieldType.Int32 :
t=Type.GetType( "System.Int32") ;
break;
case FieldType.Int64 :
t=Type.GetType( "System.Int64") ;
break;
case FieldType.Singl e:
t=Type.GetType( "System.Single" );
break;
case FieldType.Doubl e:
t=Type.GetType( "System.Double" );
break;
case FieldType.Decim al:
t=Type.GetType( "System.Decimal ");
break;
case FieldType.Boole an:
t=Type.GetType( "System.Boolean ");
break;
case FieldType.DateT ime:
t=Type.GetType( "System.DateTim e");
break;
case FieldType.Memo:
case FieldType.ByteA rray:
t=Type.GetType( "System.Byt e[]");
break;
default:
t=Type.GetType( "System.String" );
break;
}
if(t == null)continue;

PropertyBuilder pb
=typeBuilder.De fineProperty(Db Table.Fields[i].FieldName,
System.Reflecti on.PropertyAttr ibutes.None, t, new Type[]{});

MethodBuilder getMethod = typeBuilder.Def ineMethod("get_ " +
DbTable.Fields[i].FieldName,
MethodAttribute s.Public, t, new Type[]{});

ilgen = getMethod.GetIL Generator();
ilgen.DeclareLo cal(t);

// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
ilgen.Emit(OpCo des.Ldloc_0);
// **** don't know how to code the following lines coming from
ildasm.exe
IL_000b: call class [mscorlib]System.Type
[mscorlib]System.Type::Ge tTypeFromHandle (valuetype
[mscorlib]System.RuntimeT ypeHandle)
IL_0010: call instance object
MapSolver.Dynam icClasses.Layer Fields::_getPro pertyValue(stri ng,

class [mscorlib]System.Type)
}

There is also a SetPropertyValu e method but with the GetProperty will be
enough help to me :-)

Basically, I don't know how to code the following lines obtained from
ildasm.exe:

this used to invoke the event handler :

IL_0023: callvirt instance void
[MyAssembly]MyAssembly.Prop ertyValueEventH andler::Invoke( object, class
[MyAssembly]MyAssembly.Prop ertyValueEventA rgs)

and these lines, used to obtain the "typeof(string) " or similar and the
other to call the _getPropertyVal ue method of this type.

IL_0006: ldtoken [mscorlib]System.String
IL_000b: call class [mscorlib]System.Type
[mscorlib]System.Type::Ge tTypeFromHandle (valuetype
[mscorlib]System.RuntimeT ypeHandle)
IL_0010: call instance object
MapSolver.Dynam icClasses.Layer Fields::_getPro pertyValue(stri ng,

class [mscorlib]System.Type)

Someone could help or point me in the right direction ?

Thanks in Advance
Luis Arvayo

P.S. Sorry the long post :-)


Mar 22 '06 #1
2 3626
Why use Reflection.Emit ?
It is easy with System.CodeDom.

"Luis Arvayo" <am**********@p rodigy.net.mx> wrote in message
news:eX******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

In c#, I need to dynamically create types at runtime that will consist of
the following:
- inherits from a given interface

- will have a constructor with an int argument

- Will have an event handler (derived from the interface)

- Will have a method that will return the value of the property or
properties based on firing the event handler and the hooked to the event
will return the value.
The prototype is something like the following:
// Event args for the event handler
public class PropertyValueEv entArgs : EventArgs
{
int _recordNo;
public int RecordNo
{
get {return _recordNo;}
}

string _propertyName;
public string PropertyName
{
get {return _propertyName;}
}

Type _propertyType;
public Type PropertyType
{
get {return _propertyType;}
}

object _value;
public object Value
{
get {return _value;}
set {_value = value;}
}

public PropertyValueEv entArgs(int recordNo, string propertyName, Type
propertyType, object value)
{
_recordNo = recordNo;
_propertyName = propertyName;
_propertyType = propertyType;
_value = value;
}
}

// delegate for the event handler

public delegate void PropertyValueEv entHandler(obje ct sender,
PropertyValueEv entArgs e);

// The interfase that must be implemented by the dynamic type
public interface IDynamicDataFie lds
{
event PropertyValueEv entHandler GetPropertyValu e;
}

// note: the above two are already defined in other assembly
The prototye class that must be created at runtime :
public class LayerFields : IDynamicDataFie lds
{
// a private field
int _recordNo;

// the event
public event PropertyValueEv entHandler GetPropertyValu e;

// the constructor
public LayerFields(int recordNo)
{
_recordNo = recordNo;
}
// return value for the property based on the event
object _getPropertyVal ue(string propertyName, Type propertyType)
{
if(GetPropertyV alue == null) return null;

PropertyValueEv entArgs e = new PropertyValueEv entArgs(_record No,
propertyName, propertyType, null);
GetPropertyValu e(this, e);
return e.Value;
}
// here the list of variable number of properties of type string, int,
bool, and so on.

public string StringProp
{
get {return (string)_getPro pertyValue("Str ingProp", typeof(string)) ;}
}

public int IntProp
{
get (return (int)_getProper tyValue("IntPro p", typeof(int));}
}

... and so on
}
This is the opcode generated by ildasm.exe

********* constructor

.method public hidebysig specialname rtspecialname
instance void .ctor(int32 recordNo) cil managed
{
// Code size 14 (0xe)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object:: .ctor()
IL_0006: ldarg.0
IL_0007: ldarg.1
IL_0008: stfld int32
MapSolver.Dynam icClasses.Layer Fields::_record No
IL_000d: ret
} // end of method LayerFields::.c tor
********** _getPropertyVal ue method
.method private hidebysig instance object
_getPropertyVal ue(string propertyName,
class [mscorlib]System.Type propertyType) cil
managed
{
// Code size 51 (0x33)
.maxstack 5
.locals ([0] class [MyAssembly]MyAssembly.Prop ertyValueEventA rgs e,
[1] object CS$00000003$000 00000)
IL_0000: ldarg.0
IL_0001: ldfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0006: brtrue.s IL_000c
IL_0008: ldnull
IL_0009: stloc.1
IL_000a: br.s IL_0031
IL_000c: ldarg.0
IL_000d: ldfld int32
MapSolver.Dynam icClasses.Layer Fields::_record No
IL_0012: ldarg.1
IL_0013: ldarg.2
IL_0014: ldnull
IL_0015: newobj instance void
[MyAssembly]MyAssembly.Prop ertyValueEventA rgs::.ctor(int3 2,

string,

class [mscorlib]System.Type,

object)
IL_001a: stloc.0
IL_001b: ldarg.0
IL_001c: ldfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0021: ldarg.0
IL_0022: ldloc.0
IL_0023: callvirt instance void
[MyAssembly]MyAssembly.Prop ertyValueEventH andler::Invoke( object,

class [MyAssembly]MyAssembly.Prop ertyValueEventA rgs)
IL_0028: ldloc.0
IL_0029: callvirt instance object
[MyAssembly]MyAssembly.Prop ertyValueEventA rgs::get_Value( )
IL_002e: stloc.1
IL_002f: br.s IL_0031
IL_0031: ldloc.1
IL_0032: ret
} // end of method LayerFields::_g etPropertyValue
*************** * return the property value
.method public hidebysig specialname instance string
get_StringProp( ) cil managed
{
// Code size 31 (0x1f)
.maxstack 3
.locals ([0] string CS$00000003$000 00000)
IL_0000: ldarg.0
IL_0001: ldstr "StringProp "
IL_0006: ldtoken [mscorlib]System.String
IL_000b: call class [mscorlib]System.Type
[mscorlib]System.Type::Ge tTypeFromHandle (valuetype
[mscorlib]System.RuntimeT ypeHandle)
IL_0010: call instance object
MapSolver.Dynam icClasses.Layer Fields::_getPro pertyValue(stri ng,

class [mscorlib]System.Type)
IL_0015: castclass [mscorlib]System.String
IL_001a: stloc.0
IL_001b: br.s IL_001d
IL_001d: ldloc.0
IL_001e: ret
} // end of method LayerFields::ge t_COLONIA

*************** Is this needed to code this and how ?

.method public hidebysig newslot specialname virtual final
instance void add_GetProperty Value(class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler 'value') cil managed
synchronized
{
// Code size 24 (0x18)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.0
IL_0002: ldfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0007: ldarg.1
IL_0008: call class [mscorlib]System.Delegate
[mscorlib]System.Delegate ::Combine(class [mscorlib]System.Delegate ,

class [mscorlib]System.Delegate )
IL_000d: castclass [MyAssembly]MyAssembly.Prop ertyValueEventH andler
IL_0012: stfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0017: ret
} // end of method LayerFields::ad d_GetPropertyVa lue
*************** * this is the counterpart to add_GetProperty Value

.method public hidebysig newslot specialname virtual final
instance void remove_GetPrope rtyValue(class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler 'value') cil managed
synchronized
{
// Code size 24 (0x18)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.0
IL_0002: ldfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0007: ldarg.1
IL_0008: call class [mscorlib]System.Delegate
[mscorlib]System.Delegate ::Remove(class [mscorlib]System.Delegate ,

class [mscorlib]System.Delegate )
IL_000d: castclass [MyAssembly]MyAssembly.Prop ertyValueEventH andler
IL_0012: stfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0017: ret
} // end of method LayerFields::re move_GetPropert yValue

Up to now, I have created the following code, but I am stuck in the marked
code:
// create a dynamic assembly and module
AssemblyName an = new AssemblyName();
an.Name = "DynamicDat a";
AssemblyBuilder ab =
System.AppDomai n.CurrentDomain .DefineDynamicA ssembly(an,
AssemblyBuilder Access.Run);
ModuleBuilder module = ab.DefineDynami cModule(ab.GetN ame().Name, false);

// create a new type to hold our Main method
TypeBuilder typeBuilder = module.DefineTy pe("TableFields ",
TypeAttributes. Public | TypeAttributes. Class |
TypeAttributes. AutoClass | TypeAttributes. AnsiClass |
TypeAttributes. BeforeFieldInit | TypeAttributes. AutoLayout,
typeof(object), new Type[]{typeof(MyAssem bly.IDynamicDat aFields)});

// Define three fields for this type

// field for the record number
FieldBuilder recordNo = typeBuilder.Def ineField("_reco rdNo",
typeof(int), FieldAttributes .Private);

// field for GetPropertyValu e
FieldBuilder getPropertyValu e =
typeBuilder.Def ineField("GetPr opertyValue",
typeof(MyAssemb ly.PropertyValu eEventHandler), FieldAttributes .Public);

// define the constructor

// el constructor que recibe el numero de registro
ConstructorBuil der cb =
typeBuilder.Def ineConstructor( MethodAttribute s.Public |
MethodAttribute s.HideBySig |
MethodAttribute s.SpecialName | MethodAttribute s.RTSpecialName ,
CallingConventi ons.Standard,
new Type[]{typeof(int)});

ILGenerator ilgen = cb.GetILGenerat or();

// call the base constructor
// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// call base constructor
ilgen.Emit(OpCo des.Call, typeof(object). GetConstructor( new Type[0]));

// now save the record number in field "_recordNo"

// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// load first logical argument
ilgen.Emit(OpCo des.Ldarg_1);
// store in field _recordNo
ilgen.Emit(OpCo des.Stfld, recordNo);

// emit return code
ilgen.Emit(OpCo des.Ret);
// define the private method GetPropertyValu e
MethodBuilder mb = typeBuilder.Def ineMethod("_get PropertyValue",
MethodAttribute s.Private | MethodAttribute s.HideBySig,
CallingConventi ons.Standard,
typeof(object), new Type[]{typeof(string) , typeof(System.T ype)});

ilgen = mb.GetILGenerat or();

// declare local variables
ilgen.DeclareLo cal(typeof(MyAs sembly.Property ValueEventArgs) );
ilgen.DeclareLo cal(typeof(obje ct));

// define "notnull" label
Label notnull_label = ilgen.DefineLab el();
Label exit_label = ilgen.DefineLab el();

// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// load "_getPropertyVa lue" field
ilgen.Emit(OpCo des.Ldfld, getPropertyValu e);
// branch if != null
ilgen.Emit(OpCo des.Brtrue_S, notnull_label);
// load null
ilgen.Emit(OpCo des.Ldnull);
// set local variable 1 (result) to null
ilgen.Emit(OpCo des.Stloc_1);
// jump to exit_label
ilgen.Emit(OpCo des.Br_S, exit_label);

// mark "notnull_la bel
ilgen.MarkLabel (notnull_label) ;

// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// load _recordNo private type variable
ilgen.Emit(OpCo des.Ldfld, recordNo);
// load first argument - "propertyNa me"
ilgen.Emit(OpCo des.Ldarg_1);
// load second argument - "propertyTy pe"
ilgen.Emit(OpCo des.Ldarg_2);
// load "null" as the default value of the property
ilgen.Emit(OpCo des.Ldnull);
// create the MyAssembly.Prop ertyValueEventA rgs argument
ilgen.Emit(OpCo des.Newobj,
typeof(MyAssemb ly.PropertyValu eEventArgs).Get Constructor(new
Type[]{typeof(int),
typeof(string), typeof(System.T ype), typeof(object)} ));
// store in local variable 0
ilgen.Emit(OpCo des.Stloc_0);

// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// load field getPropertyValu e
ilgen.Emit(OpCo des.Ldfld, getPropertyValu e);
// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// load local variable 0 - MyAssembly.Prop ertyValueEventA rgs
ilgen.Emit(OpCo des.Ldloc_0);
// invoke the event handler in property
//***** I don't know how to code this line that corresponds to the below
line ==>> ilgen.Emit(OpCo des.Callvirt, how to ?
IL_0023: callvirt instance void
[MyAssembly]MyAssembly.Prop ertyValueEventH andler::Invoke( object, class
[MyAssembly]MyAssembly.Prop ertyValueEventA rgs)

// load event args
ilgen.Emit(OpCo des.Ldloc_0);
ilgen.Emit(OpCo des.Callvirt,
typeof(MyAssemb ly.PropertyValu eEventArgs).Get Method("get_Val ue"));
ilgen.Emit(OpCo des.Stloc_1);
ilgen.Emit(OpCo des.Br_S, exit_label);
// mark label "exit"
ilgen.MarkLabel (exit_label);
ilgen.Emit(OpCo des.Ldloc_1);
// return
ilgen.Emit(OpCo des.Ret);


// now populate the list of properties from a database list of fields
for(int i = 0; i < DbTable.Fields. Count; i++)
{
Type t = null;
// map to our field types
switch(DbTable. Fields[i].FieldType)
{
case FieldType.Strin g:
t = Type.GetType("S ystem.String");
break;
case FieldType.Byte:
t=Type.GetType( "System.Byt e");
break;
case FieldType.Char:
t=Type.GetType( "System.Cha r");
break;
case FieldType.Int16 :
t=Type.GetType( "System.Int16") ;
break;
case FieldType.Int32 :
t=Type.GetType( "System.Int32") ;
break;
case FieldType.Int64 :
t=Type.GetType( "System.Int64") ;
break;
case FieldType.Singl e:
t=Type.GetType( "System.Single" );
break;
case FieldType.Doubl e:
t=Type.GetType( "System.Double" );
break;
case FieldType.Decim al:
t=Type.GetType( "System.Decimal ");
break;
case FieldType.Boole an:
t=Type.GetType( "System.Boolean ");
break;
case FieldType.DateT ime:
t=Type.GetType( "System.DateTim e");
break;
case FieldType.Memo:
case FieldType.ByteA rray:
t=Type.GetType( "System.Byt e[]");
break;
default:
t=Type.GetType( "System.String" );
break;
}
if(t == null)continue;

PropertyBuilder pb
=typeBuilder.De fineProperty(Db Table.Fields[i].FieldName,
System.Reflecti on.PropertyAttr ibutes.None, t, new Type[]{});

MethodBuilder getMethod = typeBuilder.Def ineMethod("get_ " +
DbTable.Fields[i].FieldName,
MethodAttribute s.Public, t, new Type[]{});

ilgen = getMethod.GetIL Generator();
ilgen.DeclareLo cal(t);

// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
ilgen.Emit(OpCo des.Ldloc_0);
// **** don't know how to code the following lines coming from
ildasm.exe
IL_000b: call class [mscorlib]System.Type
[mscorlib]System.Type::Ge tTypeFromHandle (valuetype
[mscorlib]System.RuntimeT ypeHandle)
IL_0010: call instance object
MapSolver.Dynam icClasses.Layer Fields::_getPro pertyValue(stri ng,

class [mscorlib]System.Type)
}

There is also a SetPropertyValu e method but with the GetProperty will be
enough help to me :-)

Basically, I don't know how to code the following lines obtained from
ildasm.exe:

this used to invoke the event handler :

IL_0023: callvirt instance void
[MyAssembly]MyAssembly.Prop ertyValueEventH andler::Invoke( object, class
[MyAssembly]MyAssembly.Prop ertyValueEventA rgs)

and these lines, used to obtain the "typeof(string) " or similar and the
other to call the _getPropertyVal ue method of this type.

IL_0006: ldtoken [mscorlib]System.String
IL_000b: call class [mscorlib]System.Type
[mscorlib]System.Type::Ge tTypeFromHandle (valuetype
[mscorlib]System.RuntimeT ypeHandle)
IL_0010: call instance object
MapSolver.Dynam icClasses.Layer Fields::_getPro pertyValue(stri ng,

class [mscorlib]System.Type)

Someone could help or point me in the right direction ?

Thanks in Advance
Luis Arvayo

P.S. Sorry the long post :-)


Mar 22 '06 #2
> Why use Reflection.Emit ?
It is easy with System.CodeDom.

Why ? Because it is said that the result is more optimized than using
CodeDom. Is not that true ?

Also, it is said that by using CodeDom a new assembly must be created the
next time I need another dynamic type which is not true for Reflection.Emit
where the assembly could be reused. In this app, I need to create a lot of
dynamic types and at different moments in lifetime of app.
"Nick Hounsome" <nh***@nickhoun some.me.uk> escribió en el mensaje
news:Y%******** ***********@fe3 .news.blueyonde r.co.uk... Why use Reflection.Emit ?
It is easy with System.CodeDom.

"Luis Arvayo" <am**********@p rodigy.net.mx> wrote in message
news:eX******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

In c#, I need to dynamically create types at runtime that will consist of
the following:
- inherits from a given interface

- will have a constructor with an int argument

- Will have an event handler (derived from the interface)

- Will have a method that will return the value of the property or
properties based on firing the event handler and the hooked to the event
will return the value.
The prototype is something like the following:
// Event args for the event handler
public class PropertyValueEv entArgs : EventArgs
{
int _recordNo;
public int RecordNo
{
get {return _recordNo;}
}

string _propertyName;
public string PropertyName
{
get {return _propertyName;}
}

Type _propertyType;
public Type PropertyType
{
get {return _propertyType;}
}

object _value;
public object Value
{
get {return _value;}
set {_value = value;}
}

public PropertyValueEv entArgs(int recordNo, string propertyName, Type
propertyType, object value)
{
_recordNo = recordNo;
_propertyName = propertyName;
_propertyType = propertyType;
_value = value;
}
}

// delegate for the event handler

public delegate void PropertyValueEv entHandler(obje ct sender,
PropertyValueEv entArgs e);

// The interfase that must be implemented by the dynamic type
public interface IDynamicDataFie lds
{
event PropertyValueEv entHandler GetPropertyValu e;
}

// note: the above two are already defined in other assembly
The prototye class that must be created at runtime :
public class LayerFields : IDynamicDataFie lds
{
// a private field
int _recordNo;

// the event
public event PropertyValueEv entHandler GetPropertyValu e;

// the constructor
public LayerFields(int recordNo)
{
_recordNo = recordNo;
}
// return value for the property based on the event
object _getPropertyVal ue(string propertyName, Type propertyType)
{
if(GetPropertyV alue == null) return null;

PropertyValueEv entArgs e = new PropertyValueEv entArgs(_record No,
propertyName, propertyType, null);
GetPropertyValu e(this, e);
return e.Value;
}
// here the list of variable number of properties of type string, int,
bool, and so on.

public string StringProp
{
get {return (string)_getPro pertyValue("Str ingProp", typeof(string)) ;}
}

public int IntProp
{
get (return (int)_getProper tyValue("IntPro p", typeof(int));}
}

... and so on
}
This is the opcode generated by ildasm.exe

********* constructor

.method public hidebysig specialname rtspecialname
instance void .ctor(int32 recordNo) cil managed
{
// Code size 14 (0xe)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object:: .ctor()
IL_0006: ldarg.0
IL_0007: ldarg.1
IL_0008: stfld int32
MapSolver.Dynam icClasses.Layer Fields::_record No
IL_000d: ret
} // end of method LayerFields::.c tor
********** _getPropertyVal ue method
.method private hidebysig instance object
_getPropertyVal ue(string propertyName,
class [mscorlib]System.Type propertyType) cil
managed
{
// Code size 51 (0x33)
.maxstack 5
.locals ([0] class [MyAssembly]MyAssembly.Prop ertyValueEventA rgs e,
[1] object CS$00000003$000 00000)
IL_0000: ldarg.0
IL_0001: ldfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0006: brtrue.s IL_000c
IL_0008: ldnull
IL_0009: stloc.1
IL_000a: br.s IL_0031
IL_000c: ldarg.0
IL_000d: ldfld int32
MapSolver.Dynam icClasses.Layer Fields::_record No
IL_0012: ldarg.1
IL_0013: ldarg.2
IL_0014: ldnull
IL_0015: newobj instance void
[MyAssembly]MyAssembly.Prop ertyValueEventA rgs::.ctor(int3 2,

string,

class [mscorlib]System.Type,

object)
IL_001a: stloc.0
IL_001b: ldarg.0
IL_001c: ldfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0021: ldarg.0
IL_0022: ldloc.0
IL_0023: callvirt instance void
[MyAssembly]MyAssembly.Prop ertyValueEventH andler::Invoke( object,

class [MyAssembly]MyAssembly.Prop ertyValueEventA rgs)
IL_0028: ldloc.0
IL_0029: callvirt instance object
[MyAssembly]MyAssembly.Prop ertyValueEventA rgs::get_Value( )
IL_002e: stloc.1
IL_002f: br.s IL_0031
IL_0031: ldloc.1
IL_0032: ret
} // end of method LayerFields::_g etPropertyValue
*************** * return the property value
.method public hidebysig specialname instance string
get_StringProp( ) cil managed
{
// Code size 31 (0x1f)
.maxstack 3
.locals ([0] string CS$00000003$000 00000)
IL_0000: ldarg.0
IL_0001: ldstr "StringProp "
IL_0006: ldtoken [mscorlib]System.String
IL_000b: call class [mscorlib]System.Type
[mscorlib]System.Type::Ge tTypeFromHandle (valuetype
[mscorlib]System.RuntimeT ypeHandle)
IL_0010: call instance object
MapSolver.Dynam icClasses.Layer Fields::_getPro pertyValue(stri ng,

class [mscorlib]System.Type)
IL_0015: castclass [mscorlib]System.String
IL_001a: stloc.0
IL_001b: br.s IL_001d
IL_001d: ldloc.0
IL_001e: ret
} // end of method LayerFields::ge t_COLONIA

*************** Is this needed to code this and how ?

.method public hidebysig newslot specialname virtual final
instance void add_GetProperty Value(class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler 'value') cil managed
synchronized
{
// Code size 24 (0x18)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.0
IL_0002: ldfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0007: ldarg.1
IL_0008: call class [mscorlib]System.Delegate
[mscorlib]System.Delegate ::Combine(class [mscorlib]System.Delegate ,

class [mscorlib]System.Delegate )
IL_000d: castclass [MyAssembly]MyAssembly.Prop ertyValueEventH andler
IL_0012: stfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0017: ret
} // end of method LayerFields::ad d_GetPropertyVa lue
*************** * this is the counterpart to add_GetProperty Value

.method public hidebysig newslot specialname virtual final
instance void remove_GetPrope rtyValue(class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler 'value') cil managed
synchronized
{
// Code size 24 (0x18)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.0
IL_0002: ldfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0007: ldarg.1
IL_0008: call class [mscorlib]System.Delegate
[mscorlib]System.Delegate ::Remove(class [mscorlib]System.Delegate ,

class [mscorlib]System.Delegate )
IL_000d: castclass [MyAssembly]MyAssembly.Prop ertyValueEventH andler
IL_0012: stfld class
[MyAssembly]MyAssembly.Prop ertyValueEventH andler
MapSolver.Dynam icClasses.Layer Fields::GetProp ertyValue
IL_0017: ret
} // end of method LayerFields::re move_GetPropert yValue

Up to now, I have created the following code, but I am stuck in the
marked code:
// create a dynamic assembly and module
AssemblyName an = new AssemblyName();
an.Name = "DynamicDat a";
AssemblyBuilder ab =
System.AppDomai n.CurrentDomain .DefineDynamicA ssembly(an,
AssemblyBuilder Access.Run);
ModuleBuilder module = ab.DefineDynami cModule(ab.GetN ame().Name,
false);

// create a new type to hold our Main method
TypeBuilder typeBuilder = module.DefineTy pe("TableFields ",
TypeAttributes. Public | TypeAttributes. Class |
TypeAttributes. AutoClass | TypeAttributes. AnsiClass |
TypeAttributes. BeforeFieldInit | TypeAttributes. AutoLayout,
typeof(object), new Type[]{typeof(MyAssem bly.IDynamicDat aFields)});

// Define three fields for this type

// field for the record number
FieldBuilder recordNo = typeBuilder.Def ineField("_reco rdNo",
typeof(int), FieldAttributes .Private);

// field for GetPropertyValu e
FieldBuilder getPropertyValu e =
typeBuilder.Def ineField("GetPr opertyValue",
typeof(MyAssemb ly.PropertyValu eEventHandler), FieldAttributes .Public);

// define the constructor

// el constructor que recibe el numero de registro
ConstructorBuil der cb =
typeBuilder.Def ineConstructor( MethodAttribute s.Public |
MethodAttribute s.HideBySig |
MethodAttribute s.SpecialName | MethodAttribute s.RTSpecialName ,
CallingConventi ons.Standard,
new Type[]{typeof(int)});

ILGenerator ilgen = cb.GetILGenerat or();

// call the base constructor
// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// call base constructor
ilgen.Emit(OpCo des.Call, typeof(object). GetConstructor( new Type[0]));

// now save the record number in field "_recordNo"

// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// load first logical argument
ilgen.Emit(OpCo des.Ldarg_1);
// store in field _recordNo
ilgen.Emit(OpCo des.Stfld, recordNo);

// emit return code
ilgen.Emit(OpCo des.Ret);
// define the private method GetPropertyValu e
MethodBuilder mb = typeBuilder.Def ineMethod("_get PropertyValue",
MethodAttribute s.Private | MethodAttribute s.HideBySig,
CallingConventi ons.Standard,
typeof(object), new Type[]{typeof(string) , typeof(System.T ype)});

ilgen = mb.GetILGenerat or();

// declare local variables
ilgen.DeclareLo cal(typeof(MyAs sembly.Property ValueEventArgs) );
ilgen.DeclareLo cal(typeof(obje ct));

// define "notnull" label
Label notnull_label = ilgen.DefineLab el();
Label exit_label = ilgen.DefineLab el();

// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// load "_getPropertyVa lue" field
ilgen.Emit(OpCo des.Ldfld, getPropertyValu e);
// branch if != null
ilgen.Emit(OpCo des.Brtrue_S, notnull_label);
// load null
ilgen.Emit(OpCo des.Ldnull);
// set local variable 1 (result) to null
ilgen.Emit(OpCo des.Stloc_1);
// jump to exit_label
ilgen.Emit(OpCo des.Br_S, exit_label);

// mark "notnull_la bel
ilgen.MarkLabel (notnull_label) ;

// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// load _recordNo private type variable
ilgen.Emit(OpCo des.Ldfld, recordNo);
// load first argument - "propertyNa me"
ilgen.Emit(OpCo des.Ldarg_1);
// load second argument - "propertyTy pe"
ilgen.Emit(OpCo des.Ldarg_2);
// load "null" as the default value of the property
ilgen.Emit(OpCo des.Ldnull);
// create the MyAssembly.Prop ertyValueEventA rgs argument
ilgen.Emit(OpCo des.Newobj,
typeof(MyAssemb ly.PropertyValu eEventArgs).Get Constructor(new
Type[]{typeof(int),
typeof(string), typeof(System.T ype), typeof(object)} ));
// store in local variable 0
ilgen.Emit(OpCo des.Stloc_0);

// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// load field getPropertyValu e
ilgen.Emit(OpCo des.Ldfld, getPropertyValu e);
// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
// load local variable 0 - MyAssembly.Prop ertyValueEventA rgs
ilgen.Emit(OpCo des.Ldloc_0);
// invoke the event handler in property
//***** I don't know how to code this line that corresponds to the
below line ==>> ilgen.Emit(OpCo des.Callvirt, how to ?
IL_0023: callvirt instance void
[MyAssembly]MyAssembly.Prop ertyValueEventH andler::Invoke( object, class
[MyAssembly]MyAssembly.Prop ertyValueEventA rgs)

// load event args
ilgen.Emit(OpCo des.Ldloc_0);
ilgen.Emit(OpCo des.Callvirt,
typeof(MyAssemb ly.PropertyValu eEventArgs).Get Method("get_Val ue"));
ilgen.Emit(OpCo des.Stloc_1);
ilgen.Emit(OpCo des.Br_S, exit_label);
// mark label "exit"
ilgen.MarkLabel (exit_label);
ilgen.Emit(OpCo des.Ldloc_1);
// return
ilgen.Emit(OpCo des.Ret);


// now populate the list of properties from a database list of fields
for(int i = 0; i < DbTable.Fields. Count; i++)
{
Type t = null;
// map to our field types
switch(DbTable. Fields[i].FieldType)
{
case FieldType.Strin g:
t = Type.GetType("S ystem.String");
break;
case FieldType.Byte:
t=Type.GetType( "System.Byt e");
break;
case FieldType.Char:
t=Type.GetType( "System.Cha r");
break;
case FieldType.Int16 :
t=Type.GetType( "System.Int16") ;
break;
case FieldType.Int32 :
t=Type.GetType( "System.Int32") ;
break;
case FieldType.Int64 :
t=Type.GetType( "System.Int64") ;
break;
case FieldType.Singl e:
t=Type.GetType( "System.Single" );
break;
case FieldType.Doubl e:
t=Type.GetType( "System.Double" );
break;
case FieldType.Decim al:
t=Type.GetType( "System.Decimal ");
break;
case FieldType.Boole an:
t=Type.GetType( "System.Boolean ");
break;
case FieldType.DateT ime:
t=Type.GetType( "System.DateTim e");
break;
case FieldType.Memo:
case FieldType.ByteA rray:
t=Type.GetType( "System.Byt e[]");
break;
default:
t=Type.GetType( "System.String" );
break;
}
if(t == null)continue;

PropertyBuilder pb
=typeBuilder.De fineProperty(Db Table.Fields[i].FieldName,
System.Reflecti on.PropertyAttr ibutes.None, t, new Type[]{});

MethodBuilder getMethod = typeBuilder.Def ineMethod("get_ " +
DbTable.Fields[i].FieldName,
MethodAttribute s.Public, t, new Type[]{});

ilgen = getMethod.GetIL Generator();
ilgen.DeclareLo cal(t);

// load "this"
ilgen.Emit(OpCo des.Ldarg_0);
ilgen.Emit(OpCo des.Ldloc_0);
// **** don't know how to code the following lines coming from
ildasm.exe
IL_000b: call class [mscorlib]System.Type
[mscorlib]System.Type::Ge tTypeFromHandle (valuetype
[mscorlib]System.RuntimeT ypeHandle)
IL_0010: call instance object
MapSolver.Dynam icClasses.Layer Fields::_getPro pertyValue(stri ng,

class [mscorlib]System.Type)
}

There is also a SetPropertyValu e method but with the GetProperty will be
enough help to me :-)

Basically, I don't know how to code the following lines obtained from
ildasm.exe:

this used to invoke the event handler :

IL_0023: callvirt instance void
[MyAssembly]MyAssembly.Prop ertyValueEventH andler::Invoke( object, class
[MyAssembly]MyAssembly.Prop ertyValueEventA rgs)

and these lines, used to obtain the "typeof(string) " or similar and the
other to call the _getPropertyVal ue method of this type.

IL_0006: ldtoken [mscorlib]System.String
IL_000b: call class [mscorlib]System.Type
[mscorlib]System.Type::Ge tTypeFromHandle (valuetype
[mscorlib]System.RuntimeT ypeHandle)
IL_0010: call instance object
MapSolver.Dynam icClasses.Layer Fields::_getPro pertyValue(stri ng,

class [mscorlib]System.Type)

Someone could help or point me in the right direction ?

Thanks in Advance
Luis Arvayo

P.S. Sorry the long post :-)



Mar 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1566
by: SlipperyCat | last post by:
I want to use the property grid to display instances of dynamic types. A problem I'm having is that the property grid is not respecting attributes that are set with SetCustomAttribute. I've taken an example from MSDN and added a few lines to set the CateoryAttribute of my property. Unfortunetly it still comes up as misc. What's wrong with...
1
2131
by: andrew queisser | last post by:
I've been trying to dynamically create a class DevT that's derived from a generic base GenBase<T>. It doesn't seem to work. I'm attaching a code sample below that illustrates the problem. CreateType() fails when the base class is a parametrized class, as in DevT : GenBase<int>. CreateType() works if the base class is not parametrized, even...
5
2859
by: Stephen Lamb | last post by:
How would one do the following at runtime? I'm really interested in steps 2 and 3. 1. Read data from somewhere that describes types and instances. 2. Construct new types from data. 3. Construct instances of types from data. 3. Use new types and instances in a type safe manner. e.g.
8
2533
by: Eyeawanda Pondicherry | last post by:
I have put some code together that creates an enum dynamically from some database values. The enum can be read perfectly by an application that references the dynamically generated dll. If I /emit/ a new version of the assembly, and start the application again, the new values will appear for the enum. However, suppose I want the...
11
2063
by: Sean M. DonCarlos | last post by:
I have an unmanaged Win32 app that looks up the name of a DLL (unknown at compile time) from an external location, loads it with LoadLibrary, and then uses GetProcAddress on three exported functions (whose names and signatures are known at compile time). The app then calls these functions as needed throughout its execution. Depending on...
3
3309
by: cwertman | last post by:
I have a question regarding dynamic properties. I have an Object say Account --Id --Prefix --Fname --Lname --Suffix
5
2046
by: cwertman | last post by:
I have a question regarding dynamic properties. I have an Object say Account --Id --Prefix --Fname --Lname --Suffix
0
2698
by: pjr | last post by:
Using VS2005, I dynamically create an event delegate. Code follows question. My method gets the event's parameters and passes them onto a common event handler. My delegate gets called when expected and in turn, calls my common event handler. However, once in the common event handler code, the "this pointer" is wrong and hence I can't access other...
15
10845
by: EDBrian | last post by:
My problem is this. Our clients create different fields they want to collect and we allow them build dynamic filters, reports etc... We run some TSQL to actually create the column and all works very well. We are now adding a lot more functionality to our filters and could really benefit from using the LINQ to SQL. I have experimented with...
0
7912
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.