Generics ReflectionTypeLoadException、DllImport和泛型类

Generics ReflectionTypeLoadException、DllImport和泛型类,generics,reflection,pinvoke,mef,dllimport,Generics,Reflection,Pinvoke,Mef,Dllimport,我尝试在我的项目中使用MEF,但在某些情况下会出现以下错误: System.Reflection.ReflectionTypeLoadException 无法加载一个或多个请求的类型。有关详细信息,请检索LoaderExceptions属性 泛型方法或泛型类中的方法是内部调用、PInvoke,或在COM导入类中定义 在日志异常中,它显示一个使用DllImport属性的泛型类 不能在泛型类中调用pinvoke方法吗 我仍然感兴趣的是为什么这会导致一个问题 源代码: void Compose()

我尝试在我的项目中使用MEF,但在某些情况下会出现以下错误:

System.Reflection.ReflectionTypeLoadException
无法加载一个或多个请求的类型。有关详细信息,请检索LoaderExceptions属性

泛型方法或泛型类中的方法是内部调用、PInvoke,或在COM导入类中定义

在日志异常中,它显示一个使用DllImport属性的泛型类

不能在泛型类中调用pinvoke方法吗

我仍然感兴趣的是为什么这会导致一个问题

源代码:

void Compose()
        {
            try
            {
                DirectoryCatalog catalog =
                    new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory);

                AggregateCatalog agcatalogue = new AggregateCatalog(new ComposablePartCatalog[] {catalog, 
                    new AssemblyCatalog(Assembly.GetExecutingAssembly())});

                CompositionContainer container = new CompositionContainer(agcatalogue);

                CompositionBatch batch = new CompositionBatch();

                batch.AddPart(this);

                container.Compose(batch);
            }
            catch (FileNotFoundException fnfex)
            {
                Console.WriteLine(fnfex.Message);
            }
            catch (CompositionException cex)
            {
                Console.WriteLine(cex.Message);
            }
            catch (System.Reflection.ReflectionTypeLoadException rte)
            {
                Console.WriteLine(rte.Message);
                foreach (Exception ex in rte.LoaderExceptions)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }


//Generic Class
 [Serializable]
    public class ProcessCounters<T> : IDisposable where T : ICounterData
    {

        /// <summary>
        /// Extern
        /// </summary>
        /// <param name="ticks"></param>
        [DllImport("Kernel32.dll")]
        public static extern void QueryPerformanceCounter(ref long ticks);

        private static T _counterData;

...}
void Compose()
{
尝试
{
目录目录=
新目录目录(AppDomain.CurrentDomain.BaseDirectory);
AggregateCatalog agcatalogue=新的AggregateCatalog(新的组件PartCatalog[]{catalog,
新的AssemblyCatalog(Assembly.getExecutionGassembly())});
CompositionContainer=新的CompositionContainer(agcatalogue);
CompositionBatch=新的CompositionBatch();
batch.AddPart(本);
容器。组成(批);
}
捕获(FileNotFoundException fnfex)
{
Console.WriteLine(fnfex.Message);
}
捕获(组合异常cex)
{
控制台写入线(cex消息);
}
捕获(System.Reflection.ReflectionTypeLoadException rte)
{
Console.WriteLine(rte.Message);
foreach(rte.LoaderExceptions中的异常除外)
{
Console.WriteLine(例如ToString());
}
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
}
//泛型类
[可序列化]
公共类ProcessCounters:IDisposable,其中T:ICounterData
{
/// 
///外行
/// 
/// 
[DllImport(“Kernel32.dll”)]
公共静态外部无效QueryPerformanceCounter(参考长刻度);
私有静态T_计数器数据;
...}
更新:
限制:在泛型类中不能有DllImports。将您的DllImports放入 单独、静态、分级(根据建议)。 解决方案:通过将NativeMethods类移到泛型类之外来解决此问题


Compose属于什么类型?你在写东西,但我们不知道。。。此外,不导出泛型类型
ProcessCounters
。限制:泛型类中不能有DllImports。撰写
所属的类型是什么?你在写东西,但我们不知道。。。此外,不导出泛型类型
ProcessCounters
。限制:泛型类中不能有DllImports。