After resolving one issue with IMetaDataImport, I'm dealing with another for quite a long time. It's with EnumGenericParams method.
The method throws AccessViolationExpcetion, but this happens only sometimes. In the other cases the method returns from calling without any problems, but its parameters are the same as the parameters when it throws expcetion. I can't find out why only sometimes.
Also, the AccessViolationExpcetion can't be catch by catch statement. When debugging in VS 2010, the exception TargetInvocationException is first caught with InnerException set to AccessViolationExpcetion.
The definition of IMetaDataImport2 and EnumGenericParams:
[ComImport]
[Guid("<valid GUID>")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[CLSCompliant(false)]
public interface IMetaDataImport2 : IMetaDataImport {
....
void EnumGenericParams(
[ComAliasName("HCORENUM*"), In, Out] ref IntPtr phEnum,
[ComAliasName("mdToken")] mdToken tk,
[ComAliasName("mdGenericParam*"), Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex=3)] mdToken[] rGenericParams,
[ComAliasName("ULONG")] uint cGenericParams,
[ComAliasName("ULONG*"), Out] out uint pcGenericParams
);
....
The method calling:
metadataImport2.EnumGenericParams(ref pEnum, memberDef, null, 0, out genParamCount);
(Even calling with third parameter not null and fourth > 0 leads to exception.)
The problem occured when I moved app from .NET 3.5 to .NET 4.
Thank you for any help!
Uff (for the second time :-) ), it seems that setting SecurityPermissions high enough can get rid of the problem:
Also, assembly with the IMetaDataInterface2 should be under .NET 3.5 (or possibly lower).