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
Yeah.
That's the unfortunate consequence of a old design decision: metadata is allowed to not be connected to a module until they are attached to one. As such the API makes no immediate check.
It would be cool to solve eventually by providing factory methods to make sure all pieces of metadata are always connected to a module.
Jb
if (m.Name.Equals("add", StringComparison.CurrentCultureIgnoreCase))
{
methodILProcessor = m.Body.GetILProcessor();
Instruction instrMsg = methodILProcessor.Create(OpCodes.Ldstr, "method - add()");
m.Body.Instructions.Insert(0, instrMsg);
Type classObj = getType("Logger");//get type using reflection
TypeReference typeRef = type.Module.Import(classObj);
MethodReference logMethodRef = assembly.MainModule.Import(classObj.GetMethod("LogText", new Type[] { typeof(string) }));
m.Body.Instructions.Insert(1, Instruction.Create(OpCodes.Call, logMethodRef));
}
--
--
--
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 mono-cecil+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to a topic in the Google Groups "mono-cecil" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mono-cecil/E_dlFgLDPjc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mono-cecil+unsubscribe@googlegroups.com.