Generic中Value和Reference类型的接口

时间:2012-08-25 04:08:40

标签: c# generics

我正在创建一个将由类实现的接口,这些接口将用作创建查询字符串的过滤器。每个过滤器都将负责在查询中构造一个参数。用户可以定义是否包括运算符以及运算符的值以及值。如何表达值取决于数据类型。

布尔值:“= 0”

整数:“> = 2”

日期时间:“<>'2012-01-10'”

字符串:“='某些字符串'”

public interface IFilter<T> 
{
    Nullable<T> Value { get; set; }
    String Operator { get; set; }
    Boolean IncludeOperator { get; set; }
}

此接口将由一个抽象类实现,该类定义基本字段,属性并覆盖ToString方法;以及一个抽象的GetValueAsString()方法,该方法可以覆盖以执行从过滤器构造适当字符串所需的任何逻辑。

public abstract class Filter<T> : IFilter<T> 
{
    Nullable<T> _value;
    String _operator = "=";
    Boolean _includeOperator = false;

    public Nullable<T> Value { get { return this._value; } set { this._value = value; } }

    public String Operator { get { return this._operator; } set { this._operator = value; } }

    public Boolean IncludeOperator { get { return this._includeOperator; } set { this._includeOperator = value; } }


    public override string ToString()
    {
        String param = GetValueAsString();
        if (param != null && this.IncludeOperator)
            return this.Operator + " " + param;
        else if (param != null && !this.IncludeOperator)
            return param.Trim();
        else
            return null;
    }

    protected abstract String GetValueAsString();
}

我想创建从接口和抽象类派生的类型化过滤器。每个过滤器都知道如何将特定类型转换为字符串以包含在查询中,但每个过滤器都需要遵循IFilter接口,它可以通过它继承Filter基类来实现。

public class IntFilter: Filter<Int64>
{
    protected override string GetValueAsString()
    {
        // Convert the integer to a string
        return this.Value.Value.ToString();
    }
}

public class BooleanFilter : Filter<Boolean>
{
    protected override string GetValueAsString()
    {
        // convert the Boolean to a BIT value 1=True / 0=False
        return (this.Value.HasValue && this.Value.Value) ? "1" : "0";
    }
}

public class DateTimeFilter : Filter<DateTime>
{
    protected override string GetValueAsString()
    {
        // Convert the DateTime to a dateTime string in the following format - 2/27/2009 12:12:22 PM
        return (this.Value.HasValue) ? this.Value.Value.ToString("G") : null;
    }
}

这里变得棘手......我需要为字符串类型创建一个过滤器,但使用相同的接口和抽象类。

public class StringFilter : Filter<String>
{
    protected override string GetValueAsString()
    {
        // Convert the integer to a string
        return this.Value.Value.ToString();
    }
}

我需要能够将这些过滤器中的任何一个传递给以下方法...

ExecuteQuery( IFilter filter );

您可以看到使用参考和值类型的泛型类型时出现问题。有没有人知道如何为所有类型的Filter类使用相同的接口? 这可能吗?

1 个答案:

答案 0 :(得分:0)

你可以拉Nullable&lt;&gt;超出定义,然后将它们传递给实现。这是一个例子:

public interface IFilter<T>
{
    T Value { get; set; }
    String Operator { get; set; }
    Boolean IncludeOperator { get; set; }
}

public abstract class Filter<T> : IFilter<T>
{
    T _value;
    String _operator = "=";
    Boolean _includeOperator = false;

    public T Value { get { return this._value; } set { this._value = value; } }

    public String Operator { get { return this._operator; } set { this._operator = value; } }

    public Boolean IncludeOperator { get { return this._includeOperator; } set { this._includeOperator = value; } }


    public override string ToString()
    {
        String param = GetValueAsString();
        if (param != null && this.IncludeOperator)
            return this.Operator + " " + param;
        else if (param != null && !this.IncludeOperator)
            return param.Trim();
        else
            return null;
    }

    protected abstract String GetValueAsString();
}

public class IntFilter : Filter<Nullable<Int64>>
{
    protected override string GetValueAsString()
    {
        // Convert the integer to a string
        return this.Value.HasValue ? this.Value.ToString() : "0";
    }
}

public class BooleanFilter : Filter<Nullable<Boolean>>
{
    protected override string GetValueAsString()
    {
        // convert the Boolean to a BIT value 1=True / 0=False
        return (this.Value.HasValue && this.Value.Value) ? "1" : "0";
    }
}

public class DateTimeFilter : Filter<Nullable<DateTime>>
{
    protected override string GetValueAsString()
    {
        // Convert the DateTime to a dateTime string in the following format - 2/27/2009 12:12:22 PM
        return (this.Value.HasValue) ? this.Value.Value.ToString("G") : null;
    }
}

public class StringFilter : Filter<String>
{
    protected override string GetValueAsString()
    {
        return string.IsNullOrWhiteSpace(Value) ? "" : Value;
    }
}