帮助,我似乎有点想道。
昨天问了this问题后,我决定看一下表达树。我发现了一个不错的小地方,这是我到目前为止所做的:
// Gets the property type
ParameterExpression paramProperty = Expression.Parameter(property.PropertyType);
// Gets the value from row[0] (SqlDataReader)
ParameterExpression paramValue = Expression.Parameter(row[0].GetType());
// really no clue, makes a property so to speak?
MemberExpression prop = Expression.Property(paramProperty, property);
// assigns the property the value from the SqlDataReader
BinaryExpression assign = Expression.Assign(prop, paramValue);
// adds to an expression list ready for compilation
exps.Add(assign);
// allows things to be executed sequentially?
BlockExpression blockExpression = exps.Count > 0 ? Expression.Block(exps) : Expression.Block(Expression.Empty());
// create the parameter array
List<ParameterExpression> paramArr = new List<ParameterExpression>();
paramArr.Add(paramProperty);
paramArr.Add(paramValue);
// get a lambda so I can compile this for re-use
Expression<Action<T>> lamExp = Expression.Lambda<Action<T>>(blockExpression, paramArr);
首先,我的评论是对的吗?我正在以一种教程和msdn doc的旧时尚方式拼凑这些信息。
从我一直在阅读的内容中,我认为我应该能够编译它,将它存储在一个类型为密钥的字典中,并在需要时调用它。例如
ConcurrentDictionary<Type, ??> ExpressionCache;
if(ExpressionCache.ContainsKey(typeof(T))
{
// property is the variable of a foreach loop of type PropertyInfo
ExpressionCache[typeof(T)](property);
} // else do the first piece of code...
总而言之,
只要解释得很好,欢迎任何改进或建议。我试图理解它是如何工作的,而不仅仅是为了使它工作:)
答案 0 :(得分:3)
听起来你比其他任何东西都在寻找评论......对吧?
首先,如何在编译时使用SqlReader?
// Gets the value from row[0] (SqlDataReader)
ParameterExpression paramValue = Expression.Parameter(Type.GetTypeFromHandle(row[0].GetType()));
你的lambda虽然接受了两个参数......所以有些错误(Action需要一个参数)。
我认为你真正想要的是Func<SqlDataReader, T>
。这样,你给它你的SqlDataReader,它产生一个T.所以:
var list = new List<T>();
// get a SqlDataReader
while (reader.Read())
{
Func<SqlDataReader, T> readRow = GetReader<T>();
list.Add(readRow(reader);
}
我会将您的缓存存储为
ConcurrentDictionary<Type, Delegate> ExpressionCache; // can't use T here since each Func will have a different T
然后在检索时将其转换为调用者的相应委托类型(让您的方法采用通用参数:
public Func<SqlDataReader, T> GetReader<T>()
{
Delegate d;
if(!ExpressionCache.TryGetValue(typeof(T), out d)
{
ExpressionCache[typeof(T)] = d = // build and compile lambda
}
// cast to strong typed delegate...we don't want to have to DynamicInvoke...that's slow
return (Func<SqlDataReader, T>)d;
}
所以...至于你的lambda构建器/编译器:
// hang on to row[string] property
var indexerProperty = typeof(SqlDataReader).GetProperty("Item", new[] { typeof(string) });
// list of statements in our dynamic method
var statements = new List<Expression>();
// store instance for setting of properties
ParameterExpression instanceParameter = Expression.Variable(typeof(T));
ParameterExpression sqlDataReaderParameter = Expression.Parameter(typeof(SqlDataReader));
// create and assign new T to variable: var instance = new T();
BinaryExpression createInstance = Expression.Assign(instanceParameter, Expression.New(typeof(T)));
statements.Add(createInstance);
foreach (var property in typeof(T).GetProperties())
{
// instance.MyProperty
MemberExpression getProperty = Expression.Property(instanceParameter, property);
// row[property] -- NOTE: this assumes column names are the same as PropertyInfo names on T
IndexExpression readValue = Expression.MakeIndex(sqlDataReaderParameter, indexerProperty, new[] { Expression.Constant(property.Name) });
// instance.MyProperty = row[property]
BinaryExpression assignProperty = Expression.Assign(getProperty, Expression.Convert(readValue, property.PropertyType));
statements.Add(assignProperty);
}
var returnStatement = instanceParameter;
statements.Add(returnStatement);
var body = Expression.Block(instanceParameter.Type, new[] { instanceParameter }, statements.ToArray());
/* so we end up with
* T Read(SqlDataReader row)
* {
* var x = new T();
* x.Prop1 = (cast)row["Prop1"]
* x.Prop2 = (cast)row["Prop2"]
* x.Prop3 = (cast)row["Prop3"]
* x.Prop4 = (cast)row["Prop4"]
* etc.
* return x
* }
*/
var lambda = Expression.Lambda<Func<SqlDataReader, T>>(body, sqlDataReaderParameter);
// cache me!
return lambda.Compile();
没有测试过,所以请小心使用,亲自试一试。
我不确定我的预期用法是否正确......是吗?