I removed the AOP reference to my AssemblyWeaver and started creating
static TypeDefinitions, MethodDefinitions and FieldDefinitions that
I'll use for code injection.
But these Definitions are reporting me errors when importing them.
Here is an example:
TypeReference attributeType =
typeDefinition.Module.Import(new
TypeDefinition("Indiefreaks.AOP.Profiler", "ProfilerAttribute",
TypeAttributes.Class | TypeAttributes.Public |
TypeAttributes.AutoClass | TypeAttributes.AnsiClass |
TypeAttributes.BeforeFieldInit,
new
TypeDefinition("System", "Attribute",
TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Abstract
| TypeAttributes.AutoClass | TypeAttributes.AnsiClass |
TypeAttributes.Serializable |
TypeAttributes.BeforeFieldInit,
new TypeDefinition("System", "Object",
TypeAttributes.Class | TypeAttributes.Public |
TypeAttributes.AutoClass | TypeAttributes.AnsiClass |
TypeAttributes.Serializable |
TypeAttributes.BeforeFieldInit))));
foreach (CustomAttribute customAttribute in
typeDefinition.Module.Assembly.CustomAttributes)
{
if (customAttribute.AttributeType.FullName ==
attributeType.FullName)
return true;
}
I'm recreating the entire tree call since the targeted assembly may
not be using the current .Net framework version.
With that I'm getting a NullReferenceException on the
MetaDataImporter.ImportScope(IMetadataScope scope) method as the
TypeDefinition.Scope isn't set.
What should I do to get proper Cecil TypeDefinition, method and field
correctly created?
Thanks
On Jun 24, 8:28 am, Jb Evain <[email protected]> wrote:
> Hi,
>
> On Fri, Jun 24, 2011 at 3:14 AM, Indiefreaks.com
>
> <[email protected]> wrote:
> > My 1st question would then be: Is it possible to have one single
> > program that would late bind assemblies so they get used for code
> > injection?
>
> I'm not sure I understand your question, but Cecil is platform
> independent. So as long as you don't add references to the running
> runtime, you'll be fine.
>
> > And secondly: If it is possible, I imagine that I would need to import
> > these types, methods, fields differently than using the usual
> > Model.Import(typeof(whatever)); What would be that second option?
>
> Use only the Cecil type system, and create references with
> ModuleDefinition.Import
> (TypeReference/MethodReference/FieldReference).
>
> Jb
--
--
mono-cecil