我有以下代码作为搜索的对象模型,用户在客户端进行,这非常有效:
public class DemographicAttribute
{
public string DemographicAttributeValue { get; set; }
}
public class AudienceOperators<T>: IAudienceOperators
{
public string DemographicAttributeType { get; set; }
public List<T> Operand { get; set; }
public string Operator { get; set; }
//List<U> Entity2 { get; set; }
public AudienceOperators(List<T> operand, string _operator = null, string demographicAttributeType = "")
{
Operand = operand;
Operator = _operator;
DemographicAttributeType = demographicAttributeType;
}
}
public interface IAudienceOperators {}
用法如下:
DemographicAttribute atr1 = new DemographicAttribute();
atr1.DemographicAttributeType = "Occupation";
atr1.DemographicAttributeValue = "White collar";
DemographicAttribute atr2 = new DemographicAttribute();
atr2.DemographicAttributeType = "Occupation";
atr2.DemographicAttributeValue = "Blue";
DemographicAttribute atr3 = new DemographicAttribute();
atr3.DemographicAttributeType = "Age";
atr3.DemographicAttributeValue = "10 - 14";
DemographicAttribute atr4 = new DemographicAttribute();
atr4.DemographicAttributeType = "Age";
atr4.DemographicAttributeValue = "15 - 25";
AudienceOperators<String> op1 = new AudienceOperators<String>(new List<String> { atr1.DemographicAttributeValue, atr2.DemographicAttributeValue }, "OR", atr1.DemographicAttributeType);
AudienceOperators<String> op2 = new AudienceOperators<String>(new List<String> { atr3.DemographicAttributeValue, atr4.DemographicAttributeValue }, "OR", atr3.DemographicAttributeType);
AudienceOperators<IAudienceOperators> op3 = new AudienceOperators<IAudienceOperators>(new List<IAudienceOperators> { op1, op2 }, "AND");
为了能够创建通用类的实例,我需要创建空白接口。但我认为,这不是最好的代码实践。还有其他办法吗?
答案 0 :(得分:0)
你不需要做任何特别的事情来允许&#34;递归&#34;通用实例。
public class Recursively<T> {
public T Instance { get; set; }
}
...
var recursively = new Recursively<Recursively<Recursively<string>>>();
Console.WriteLine(recursively.Instance.GetType().ToString());
Console.Writeline(recursively.Instance.Instance.GetType().ToString());
Console.Writeline(recursively.Instance.Instance.Instance.GetType().ToString());
答案 1 :(得分:0)
也许,使用对象?
AudienceOperators<object> op3 = new AudienceOperators<object>(new List<object> { op1, op2 }, "AND");
你不必宣布IAudienceOperators。