1

是否有一种简单的方法可以将方法存储在 EF 表中,类似于继承?

例如,我有一个代表门户页面上的应用程序的表格(有点像您的基本 iphone 或 android 主屏幕)。我想通过每个应用显示一个数字,显示该应用有多少通知。但是,获取通知数量的方法因应用程序而异。

我目前的解决方案是只拥有一个包含我需要的所有方法的类,然后根据应用名称进行切换。有没有更好的办法?

4

1 回答 1

1

假设您获取通知的方法是无参数的,您可以将方法名称存储在该表的列中并通过反射调用它。对于此示例,我们将调用列/属性NotificationMethodName

//this would go in your entity class 
public int GetNotificationCount()
{    
   MethodInfo mi = typeof(HelperClass).GetMethod(this.NotificationMethodName);    
   return (int)mi.Invoke(this, null); 
}

public class HelperClass
{
  //your class that currently has all the methods to get notification count
}
于 2012-08-22T19:54:51.520 回答