来自 ObjectContext 的 EntityFramework EntityType?

发布于 2024-11-10 18:41:04 字数 574 浏览 9 评论 0原文

我有一个具有以下签名的方法:

public voidGenerateLog(TEntityEntity) where TEntity : EntityObject

如何循环遍历我的 ObjectContext 并为我的 ObjectContext 中的每个实体调用此方法?
我知道我可以这样做:

foreach (ObjectStateEntry entry in
                context.ObjectStateManager.GetObjectStateEntries(
                EntityState.Added | EntityState.Modified))
{
    string entityName = entry.Entity.GetType().Name;
}

但我不知道如何从名称的字符串表示形式转到 GenerateLog 而不是 GenerateLog

I have a method with this signature:

public void GenerateLog<TEntity>(TEntity entity) where TEntity : EntityObject

How can I loop through my ObjectContext and call this for each Entity in my ObjectContext?
I know that I can do this:

foreach (ObjectStateEntry entry in
                context.ObjectStateManager.GetObjectStateEntries(
                EntityState.Added | EntityState.Modified))
{
    string entityName = entry.Entity.GetType().Name;
}

But I don't know how to go from a String representation of the name to GenerateLog<MYSTRING> instead of GenerateLog<TEntity>.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌伤浅笑 2024-11-17 18:41:04

您需要从 GenerateLog 创建一个通用方法,然后调用它。在让这样的东西工作之前,我通常需要先搞乱一下,但这应该很接近

MethodInfo generateLog = typeof(YourClass)
    .GetMethod("GenerateLog", BindingFlags.Public | BindingFlags.Instance );

MethodInfo genericGenerateLog = generateLog.MakeGenericMethod(entry.Entity.GetType());

genericGenerateLog.Invoke(this, new object[] { entry.Entity });

YourClass 只是GenerateLog所在的类。

You need to make a generic method from your GenerateLog and then call that. I normally need to mess around a bit before I get something like this to work, but this should be close

MethodInfo generateLog = typeof(YourClass)
    .GetMethod("GenerateLog", BindingFlags.Public | BindingFlags.Instance );

MethodInfo genericGenerateLog = generateLog.MakeGenericMethod(entry.Entity.GetType());

genericGenerateLog.Invoke(this, new object[] { entry.Entity });

YourClass is simply the class the GenerateLog is in.

丘比特射中我 2024-11-17 18:41:04

正如 Drew Marsh 所说,无法仅使用泛型 Type 参数的名称来调用泛型方法。因此,我只能建议您可能认为使用运行时方法解析有点垃圾的解决方案 - 尽管它会起作用...

首先,在 foreach 内分配一个动态变量,并调用名为(例如)CallGenerateLog() 的私有方法:

foreach (ObjectStateEntry entry in
                context.ObjectStateManager.GetObjectStateEntries(
                EntityState.Added | EntityState.Modified))
{
    dynamic dynamicEntity = entry.Entity;

    CallGenerateLog(dynamicEntity);
}

...为您想要记录的每种实体类型提供一个 CallGenerateLog() 重载,并具有每个都调用您的 GenerateLog() 方法,例如:

private static void CallGenerateLog(User user)
{
    GenerateLog(user);
}

private static void CallGenerateLog(Customer customer)
{
    GenerateLog(customer);
}

...等等...并提供一个包罗万象的重载,该重载将满足编译器的要求,并在您没有​​实体类型时被调用发现显式重载 for 。

private static void CallGenerateLog(object entity)
{
}

这样做的问题包括:

  1. 您需要为每个实体类型重载CallGenerateLog(),因此,如果您添加要记录的新实体类型,您必须记住添加一个它的重载(尽管您可以使用 T4 模板解决此问题)

  2. 运行时方法解析会产生一些开销,因此你可能必须分析该方法的执行情况并决定它是否继续给您带来任何问题。

As Drew Marsh said, there's no way to call a generic method with only the name of the generic Type argument. Because of that, I can only suggest what you may well judge to be a bit of a rubbish solution using runtime method resolution - although it would work...

Firstly, assign a dynamic variable inside the foreach, and call a private method named (e.g.) CallGenerateLog():

foreach (ObjectStateEntry entry in
                context.ObjectStateManager.GetObjectStateEntries(
                EntityState.Added | EntityState.Modified))
{
    dynamic dynamicEntity = entry.Entity;

    CallGenerateLog(dynamicEntity);
}

...provide one overload of CallGenerateLog() for each of the Entity types you want to log, and have each one call your GenerateLog() method, e.g.:

private static void CallGenerateLog(User user)
{
    GenerateLog(user);
}

private static void CallGenerateLog(Customer customer)
{
    GenerateLog(customer);
}

...etc... and provide a catch-all overload which will satisfy the compiler and be called if an Entity type you don't have an explicit overload for is found.

private static void CallGenerateLog(object entity)
{
}

Problems with this include:

  1. You need an overload of CallGenerateLog() for each Entity type, so if you add a new Entity type which you want to log you'll have to remember to add an overload for it (although you could address this with T4 templates)

  2. There is some overhead to runtime method resolution, so you may have to profile how well the method performs and decide if it's going to cause you any problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文