I can't figure out why, but every time I try to modify an existing
type in the assembly with the following code:
...
_hostField = new FieldDefinition("__host", _aopHostType,
FieldAttributes.Private);
typeDef.Fields.Add(_hostField);
// Implement IModifiedType
if (typeDef.Interfaces.Contains(_modifiedInterfaceType))
return;
typeDef.Interfaces.Add(_modifiedInterfaceType);
MethodAttributes attributes = MethodAttributes.Public |
MethodAttributes.HideBySig |
MethodAttributes.SpecialName
| MethodAttributes.NewSlot |
MethodAttributes.Virtual;
// Implement the getter property
_getHost = new MethodDefinition("get_Host", attributes,
_aopHostType);
_getHost.ImplAttributes = MethodImplAttributes.Managed |
MethodImplAttributes.IL;
CilWorker IL = _getHost.Body.CilWorker;
IL.Emit(OpCodes.Ldarg_0);
IL.Emit(OpCodes.Ldfld, _hostField);
IL.Emit(OpCodes.Ret);
// Implement the setter
_setHost = new MethodDefinition("set_Host", attributes,
_voidType);
_setHost.ImplAttributes = MethodImplAttributes.Managed |
MethodImplAttributes.IL;
_setHost.Parameters.Add(new
ParameterDefinition(_aopHostType));
IL = _setHost.Body.CilWorker;
IL.Emit(OpCodes.Ldarg_0);
IL.Emit(OpCodes.Ldarg_1);
IL.Emit(OpCodes.Stfld, _hostField);
IL.Emit(OpCodes.Ret);
PropertyDefinition hostProperty =
new PropertyDefinition("Host", _aopHostType,
PropertyAttributes.Unused);
_getHost.IsPublic = true;
_setHost.IsPublic = true;
hostProperty.GetMethod = _getHost;
hostProperty.SetMethod = _setHost;
typeDef.Methods.Add(_getHost);
typeDef.Methods.Add(_setHost);
typeDef.Properties.Add(hostProperty);
...
After saving the modified assembly, I keep getting this error from
PEVerify:
---------------------------------------Error
Message---------------------------------------
Microsoft (R) .NET Framework PE Verifier. Version 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
[token 0x02000002] Type load failed.
1 Error Verifying testlibrary.dll
---------------------------------------Error
Message---------------------------------------
Is this a bug, or am I doing something wrong here? I thought this was
the most straightforward way to add an interface implementation to an
existing class, and I'm not sure whether this is a bug or not. Anyone
have any ideas?
Thanks in advance.
Regards,
Philip
--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---