如何将事件添加到属性get accessor?

时间:2014-08-12 13:22:53

标签: c#

详细说明:我正在通过过滤器功能从实体编写基本搜索,所以我可以这样做:

public Entity GetEntityBy(Entity filter)
{ }
public IList<Entity> GetEntitiesBy(Entity filter)
{ }

问题在于非可空类型(int,float等),我不想强​​迫&#34;强迫&#34;所有属性都写成nullables。我想避免任何类型的规则(例如应用属性或实现我自己的get / set),这样我就可以像往常一样编写实体并简单地使用这个过滤函数。

代码如下所示:

public class Entity
{
    public int EntityID { get; set; }
    public string Name { get; set; }
    public DateTime RegisterDate { get; set; }
    //Other properties
}

public IList<Entity> GetEntitiesBy(Entity filter)
{
    if (filter != null)
    {
        if (filter.EntityID > 0)
        {
            //Add criteria to filter by ID
            //In this case it works because there shouldn't have any IDs with 0
        }

        //this won't work because DateTime can't be null
        //I can't check the default value as well because there are some searchs using the default value and I don't want to ignore that
        if (RegisterDate != null) 
        {
        }
    }
}

它应该是一个简单的相等过滤器,具体取决于filter参数中的值,但现在我不知道何时应该忽略默认值。 我已经有一个SCRUD管理器类,所以我想向它所属的类添加一个函数调用,这样我就可以检查何时读取/写入一个属性。

简短描述:如何在调用属性的getset附加者之前添加函数调用动态课?这甚至可能吗?

0 个答案:

没有答案
相关问题