Thanks Peter. The example you have is pretty much what I have figured out. I spend a lot of trial and terror to figure out some of this stuff. Part of the problem was that I was not seeing any reflected code in Reflector. I figured out that I needed to set the emitSymbolInfo parameter on the AssemblyBuilder.DefineDynamicModule method. Once I figured that out I was able to use a combination of Reflection and ILDASM to figure out what opcodes I needed to implement to make properties on the class.
Now I've been able to make a dynamic class with dynamic properties. Thanks, Mike On Thu, Jul 17, 2008 at 1:05 PM, Peter Ritchie < [EMAIL PROTECTED]> wrote: > If you provide example code we can probably point out what's missing/needs- > correction. I don't know off hand how you can write a valid assembly to > disk without being able to read types with Reflector/ILDASM. > > Here's the most simple case of dynamnically creating an assembly: > AssemblyName assemblyName = new AssemblyName("dynamicAssembly"); > AssemblyBuilder assemblyBuilder = > AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, > AssemblyBuilderAccess.Save); > ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule > (assemblyName.Name, assemblyName.Name + ".dll"); > TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", > TypeAttributes.Public); > > typeBuilder.CreateType(); > assemblyBuilder.Save(Path.GetFileName > (moduleBuilder.FullyQualifiedName)); > > ...which loads fine in Reflector. > > For a property with a getter and a setter, the following between the call > to DefineType and CreateType has worked for me: > // define a field for the property > FieldBuilder fieldBuilder = typeBuilder.DefineField("value", > typeof (int), FieldAttributes.Private); > > // define the property > PropertyBuilder propertyBuilder = typeBuilder.DefineProperty > ("Value", PropertyAttributes.HasDefault, typeof (int), null); > // define the property getter > MethodBuilder methodBuilder = typeBuilder.DefineMethod("get_Value", > > MethodAttributes.Public | MethodAttributes.SpecialName | > > MethodAttributes.HideBySig, > typeof > (int), > > Type.EmptyTypes); > // emit the body of the getter > ILGenerator ilGenerator = methodBuilder.GetILGenerator(); > ilGenerator.Emit(OpCodes.Ldarg_0); > ilGenerator.Emit(OpCodes.Ldfld, fieldBuilder); > ilGenerator.Emit(OpCodes.Ret); > > // attach the getter to the property > propertyBuilder.SetGetMethod(methodBuilder); > > // define the property setter > methodBuilder = typeBuilder.DefineMethod("set_Value", > MethodAttributes.Public | > MethodAttributes.SpecialName | > > MethodAttributes.HideBySig, > null, > new Type[] {typeof > (int)}); > > // emit the body of the setter > ilGenerator = methodBuilder.GetILGenerator(); > ilGenerator.Emit(OpCodes.Ldarg_0); > ilGenerator.Emit(OpCodes.Ldarg_1); > ilGenerator.Emit(OpCodes.Stfld, fieldBuilder); > ilGenerator.Emit(OpCodes.Ret); > > // attach the setter to the property > propertyBuilder.SetSetMethod(methodBuilder); > > Documentation in-and-around http://msdn.microsoft.com/en- > us/library/4xtysk39.aspx<http://msdn.microsoft.com/en-us/library/4xtysk39.aspx>usually > has enough info for what I'm usually > doing. > For example: http://msdn.microsoft.com/en- > us/library/system.reflection.emit.assemblybuilder.aspx<http://msdn.microsoft.com/en-us/library/system.reflection.emit.assemblybuilder.aspx> > > Cheers -- Peter > > On Thu, 17 Jul 2008 09:21:03 -0500, Mike Andrews > <[EMAIL PROTECTED]> wrote: > > >I'm needing to do some dynamic code generation. > >I've been reading up on the Reflection.Emit namespace but I'm having some > >issues. > >When I compile a dynamic assembly and then save to disk, I can't read the > >types from it in Reflector or ILDASM. > > > >Do any of you have any good tutorials or sample code I might examine? > > > >I mainly want to create a dynamic class with read/write properties. > > =================================== > This list is hosted by DevelopMentor(R) http://www.develop.com > > View archives and manage your subscription(s) at > http://discuss.develop.com > =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com