【发布时间】:2016-04-15 17:47:57
【问题描述】:
实体
public class CompareParameter<T>
{
public string FieldName { get; set; }
public T FieldValue { get; set; }
public CompareType CompareType { get; set; }
}
界面
public interface IMongoConnectionFactory
{
int GetCountWithFilter<T>(params object[] param) where T:EntityBase;
}
接口实现
public class MongoConnectionFactory : IMongoConnectionFactory
{
public int GetCountWithFilter<T>(params object[] param) where T : RedBusPlusEntities.EntityBase
{
throw new NotImplementationException();
}
}
业务代码
public int GetCustomerCount(int operatorId, int day)
{
Customer c = new Customer();
CompareParameter<int> OpId = new CompareParameter<int>();
OpId.FieldName=GetMemberName.GetName(() => c.OpId);
OpId.FieldValue=operatorId;
OpId.CompareType=CompareType.EQ;
CompareParameter<long> Day = new CompareParameter<long>();
Day.FieldName=GetMemberName.GetName(() => c.Doil);
Day.FieldValue=GetStartDateOfIssue(day);
Day.CompareType = CompareType.GTE;
return _fac.GetCountWithFilter<Customer>(OpId, Day);
}
所以我在这里将两个不同类型的两个参数从 Business 传递给 DAL。所以我从object 传递它。现在我如何构造与接口实现中传递的相同类型。用Convert.ChangeType 尝试了一些方法,但失败了。有人可以帮助我吗?
【问题讨论】:
-
了解您在 int GetCountWithFilter
(params object[] param) 中所做的工作会很有帮助,因为它可能会传入 object[] 以外的其他内容 -
我正在尝试通过传递参数对象来创建一个 mongo 过滤器查询。 mongo 查询看起来像
var filter = builder.Eq("cuisine", "Italian") & builder.Eq("zip", 10075);传递的对象包含创建过滤器所需的信息和类型 -
所以你能不能把你的界面改成: public interface IMongoConnectionFactory { int GetCountWithFilter
(params CompareParameter [] param) where T:EntityBase; } -
@ScottMorken In
params CompareParameter<T>[] paramT 与 GetCountWithFilter 的 T 不同。从CompareParameter实体定义中也可以看出,该实体的 T 会因对象而异。
标签: c# generics casting type-conversion