Thanks for your help! Works like a charm. I was too focused on importing to see that it actually importing makes it worse. Also because TypeDefinition inherits from TypeReference, so there are no compiler errors when using the resolved Definition.
The exception message is also not much of an help, since it points you in the wrong direction. I'm wondering if the "new ParameterDefinition (viewModelType)" should not have thrown an exception explaining you need a TypeReference in stead of a TypeDefinition, in case of the type living in another Assembly. Just a thought... Regards, Remy On Feb 27, 4:55 pm, Jb Evain <[email protected]> wrote: > Hi, > > On Feb 27, 2012, at 4:21 PM, Remy Blok wrote: > > > It is probably someting simple but I can't seem to find it. > > Indeed it is ;) > > > > > //get Arguments from the attribute > > var viewType = > > module.Import().Resolve(); // > > Resolves Class1 > > var viewModelType = > > module.Import((TypeReference)customAttributeArguments[1].Value).Resolve(); > > // > > Resolves OtherAssembly.IViewModel > > Your issue is just here: a bit of cargo cult programming. > > Let's decompose: > > > (TypeReference)customAttributeArguments[1].Value > > You have a perfectly valid TypeReference about “OtherAssembly.IViewModel” > (which is defined in OtherAssembly.dll) scoped for Views.dll > > > module.Import((TypeReference)customAttributeArguments[1].Value) > > This does exactly nothing. You already have a TypeReference scoped for module. > > > module.Import((TypeReference)customAttributeArguments[1].Value).Resolve() > > This returns the TypeDefinition of IViewModel from OtherAssembly.dll, > obviously scoped for OtherAssembly.dll > > As you try to use it as is, obviously Cecil complains. You have to create a > reference for it that makes sense. > > Let's recompose using steps that make sense. > > Let start with > > > var viewType = > > module.Import((TypeReference)customAttributeArguments[0].Value).Resolve(); > > // Resolves Class1 > > And turn it into: > > > var viewType = (TypeDefinition) customAttributes.Arguments[0].Value; > > Because Class1 is defined in the same module, you can use it as a > TypeDefinition directly. You could also write: > > > var viewType = ((TypeReference) > > customAttributes.Arguments[0].Value).Resolve(); > > But it's less explicit. Now for the second argument which is defined in > another assembly: > > Let's turn > > > var viewModelType = > > module.Import((TypeReference)customAttributeArguments[1].Value).Resolve(); > > // Resolves OtherAssembly.IViewModel > > Into: > > > var viewModelType = (TypeReference) customAttributes.Arguments[1].Value; > > You already have a perfectly valid TypeReference, you don't need to Import or > Resolve it. > > var ctor = …; > var ctor.Parameters.Add (new ParameterDefinition (viewModelType)); > > Bang. Fixed. > > Jb -- -- mono-cecil
