Don't seem to be having much luck with this. var corlib = this.AssemblyResolver.Resolve((AssemblyNameReference)this.ModuleDefinition.TypeSystem.Corlib);
That gets me an AssemblyDefinition, but its main module has no types...and consequently calling GetType(string) returns null. Why would this be? On Monday, January 6, 2014 6:06:24 PM UTC+10:30, Jb Evain wrote: > > Yeah that works, but it's a bit convoluted. See: > > var corlib = > this.AssemblyResolver.Resolve((AssemblyNameReference)this.ModuleDefinition.TypeSystem.Corlib); > > > var attributeDefinition = > corlib.MainModule.GetType(typeof(CompilerGeneratedAttribute).FullName); > var constructorDefinition = > attributeDefinition.GetConstructors().First(x => x.Parameters.Count == > 0); > > var constructorReference = > this.ModuleDefinition.Import(constructorDefinition); > > writeMethod.CustomAttributes.Add(constructorReference); > > If you have the definition of your type in the corlib, you simply get > the definition of the constructor and import a reference for it. > > Jb > > > On Mon, Jan 6, 2014 at 7:56 AM, John Galt <[email protected]<javascript:>> > wrote: > > Thanks for the reply, Jb. This is what I ended up doing: > > > > var corlib = > > > this.AssemblyResolver.Resolve((AssemblyNameReference)this.ModuleDefinition.TypeSystem.Corlib); > > > > var compilerGeneratedAttributeType = > > > this.ModuleDefinition.Import(corlib.MainModule.GetType(typeof(CompilerGeneratedAttribute).FullName)); > > > > var compilerGeneratedAttributeCtor = > > > this.ModuleDefinition.Import(compilerGeneratedAttributeType.Resolve().GetConstructors().First(x > > > > => x.Parameters.Count == 0)); > > writeMethod.CustomAttributes.Add(new > > CustomAttribute(compilerGeneratedAttributeCtor)); > > > > > > Can you please confirm that this is the right approach? I'm trying to > get my > > head around general patterns and best practices before attempting to > > abstract into helper methods. > > > > Thanks > > > > On Monday, January 6, 2014 1:26:49 AM UTC+10:30, Jb Evain wrote: > >> > >> Hey John, > >> > >> There is :) > >> > >> You can simply write: > >> > >> var constructor = > >> typeof(CompilerGeneratedAttribute).GetConstructors().First(x => > >> x.Parameters.Count == 0) > >> var compilerGeneratedAttributeCtor = > ModuleDefinition.Import(constructor); > >> > >> Note that this will create a reference to the executing version of the > >> assembly containing the type CompilerGeneratedAttribute. So if you're > >> modifying a .net 4.0 assembly on .net 4.0 that's not a problem, but if > >> you're modifying a .net 2.0 assembly when running a .net 4.0 program, > >> you will create a reference to the .net 4.0 mscorlib. > >> > >> To avoid this, it's usually best to work only with the Cecil type > >> system. You just need to resolve the reference to the corlib > >> (module.TypeSystem.Corlib), get the type CompilerGeneratedAttribute, > >> and import a reference into your current module. > >> > >> Jb > >> > >> On Sun, Jan 5, 2014 at 2:20 AM, John Galt <[email protected]> > wrote: > >> > Hi, > >> > > >> > I'm new to Cecil (great job!) so am still floundering around a bit. > >> > Apologies for such a noob question. > >> > > >> > I just caught myself doing this: > >> > > >> > var compilerGeneratedAttributeCtor = > >> > > >> > > ModuleDefinition.Import(ModuleDefinition.Import(typeof(CompilerGeneratedAttribute)).Resolve().GetConstructors().First(x > > > >> > => x.Parameters.Count == 0)); > >> > > >> > And I thought: surely there's a cleaner, easier way. Any thoughts? > >> > > >> > Thanks > >> > > >> > -- > >> > -- > >> > -- > >> > mono-cecil > >> > --- > >> > You received this message because you are subscribed to the Google > >> > Groups > >> > "mono-cecil" group. > >> > To unsubscribe from this group and stop receiving emails from it, > send > >> > an > >> > email to [email protected]. > >> > For more options, visit https://groups.google.com/groups/opt_out. > > > > -- > > -- > > -- > > mono-cecil > > --- > > You received this message because you are subscribed to the Google > Groups > > "mono-cecil" group. > > To unsubscribe from this group and stop receiving emails from it, send > an > > email to [email protected] <javascript:>. > > For more options, visit https://groups.google.com/groups/opt_out. > -- -- -- mono-cecil --- You received this message because you are subscribed to the Google Groups "mono-cecil" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
