private Dictionary <string, object> GetKeyValueDictSync <TDict, TKey>(List <TDict> keyValueList, Expression <Func <TDict, TKey> > keyProperty) { var keyPropertyName = PropertyLambda.For(keyProperty); var keyPropertyInfo = typeof(TDict).GetProperty(keyPropertyName); var cacheKey = string.Format("{0}.{1}", typeof(TDict).FullName, keyPropertyName); if (!this.KeyValueCacheDict.ContainsKey(cacheKey)) { lock (syncObj) { if (!this.KeyValueCacheDict.ContainsKey(cacheKey)) { Dictionary <string, object> keyValueDict = new Dictionary <string, object>(); foreach (var item in keyValueList) { var keyItem = keyPropertyInfo.GetValue(item, null); var keyString = keyItem == null ? string.Empty : keyItem.ToString().TrimOrEmpty(); if (!keyValueDict.ContainsKey(keyString)) { keyValueDict.Add(keyString, item); } } this.KeyValueCacheDict.Add(cacheKey, keyValueDict); } } } return(this.KeyValueCacheDict[cacheKey]); }
public void PropertyLambdaCanReadAPropertyNameFromALambda() { IPropertyInterface obj = null; var lambda = PropertyLambda <IPropertyInterface> .Create(() => obj.GetSet); lambda.Getter.Name.ShouldBe("get_GetSet"); }
/// <summary> /// Execute `onGet` when the property specified in `propertyLookupExpression` is called. For example, /// call `<code>fake.StubGetter(() => fake.Object.MyProperty, () => 5)</code>` to always return five. /// </summary> /// <param name="propertyLookupExpression">Pass a lambda of the following form: <code>() => fake.Object.MyProperty</code>.</param> /// <param name="onGet">Pass a lambda of the following form: <code>() => "hello"</code>.</param> public FuncCallHistory <TProp> StubGetter <TProp>(Expression <Func <TProp> > propertyLookupExpression, Func <TProp> onGet) { var lambda = PropertyLambda <TObj> .Create(propertyLookupExpression); var stubbedOperation = new FuncCallHistory <TProp>(); interceptor.RegisterOperation(lambda.Getter, onGet, stubbedOperation); return(stubbedOperation); }
public static string PropertyName <TModel, TProperty>(this HtmlHelper <TModel> helper, Expression <Func <TModel, TProperty> > expression) { return(PropertyLambda.For(expression)); }