0

我目前正在尝试实现一个使用TypeBuilder动态创建一个新类的类,该类派生自一个本身派生自DbContext的类(iow 动态类间接继承自DbContext)。

要覆盖(使用DefineMethod + DefineMethodOverrideDispose类,我需要先使用GetMethod检索它。我能够SaveChanges()使用以下方法检索该方法:

typeof(TBaseDbContext).GetMethod("SaveChanges")

然而Dispose(bool disposing)完全不同,因为它是受保护的并且有参数。

缩小问题范围,我得出以下代码:

        MethodInfo disposeOfBase =
            typeof(DbContext).GetMethod("Dispose",
                BindingFlags.NonPublic,
                null,
                CallingConventions.Any,
                new Type[] { typeof(bool) },
                null);

disposeOfBase中的结果为空。我尝试了不同的组合,但我认为以上应该接近正确。我不知道为什么找不到Dispose

我在这里做错了什么?如何修改代码以便 disposeOfBase 保存 DbContext 的受保护Dispose方法的信息?

4

1 回答 1

1

I tested and it worked, so I'm posting the above comment as an answer:

Try BindingFlags.NonPublic | BindingFlags.Instance. You must specify either Instance or Static to get a method back.

于 2016-07-04T14:15:09.663 回答