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

Reply via email to