/// <summary> /// Gets type delegate to create instance in an efficient way /// </summary> /// <param name="propertyInfo"></param> /// <returns></returns> public static PropertyCallerInfo <TObjectType, TPropertyType> GetDelegateInstance <TObjectType, TPropertyType>(PropertyInfo propertyInfo) { Type type = typeof(TObjectType); ParameterExpression arg = Expression.Parameter(type.MakeByRefType(), "x"); Expression expr = Expression.Property(arg, propertyInfo.Name); GetPropertyValue <TObjectType, TPropertyType> propertyResolver = Expression.Lambda <GetPropertyValue <TObjectType, TPropertyType> >(expr, arg).Compile(); //var openGetterType = typeof(GetPropertyValue<,>); //var concreteGetterType = openGetterType // .MakeGenericType(type, propertyInfo.PropertyType); Type openSetterType = typeof(Action <,>); Type concreteSetterType = openSetterType .MakeGenericType(type, propertyInfo.PropertyType); //Delegate getterInvocation = Delegate.CreateDelegate(concreteGetterType, null, propertyInfo.GetGetMethod()); Delegate setterInvocation = Delegate.CreateDelegate(concreteSetterType, null, propertyInfo.GetSetMethod()); Type callerType = typeof(PropertyCallerInfo <,>); Type callerGenericType = callerType .MakeGenericType(type, propertyInfo.PropertyType); return((PropertyCallerInfo <TObjectType, TPropertyType>)Activator.CreateInstance(callerGenericType, propertyResolver, setterInvocation)); }
public static GetPropertyValue <T> CreateGetPropertyValueDelegate <TSource, T>(this PropertyInfo propertyInfo, TSource obj) { string propertyName = propertyInfo.Name; string key = string.Format("Delegate-GetProperty-{0}-{1}", typeof(TSource).FullName, propertyName); GetPropertyValue <T> result = (GetPropertyValue <T>)myDelegateCache.GetOrAdd( key, newkey => { return(Delegate.CreateDelegate(typeof(GetPropertyValue <T>), obj, propertyInfo.GetGetMethod())); }); return(result); }
static void Main(string[] args) { string[] FieldsConst = new string[] { FIELD1, FIELD2, FIELD3, FIELD4, FIELD5, FIELD6, FIELD7, FIELD8, FIELD9 }; RateInfo[] newRates = new RateInfo[] { new RateInfo { Value = 1 }, new RateInfo { Value = 2 }, new RateInfo { Value = 3 }, new RateInfo { Value = 4 }, new RateInfo { Value = 5 }, new RateInfo { Value = 6 }, new RateInfo { Value = 7 }, new RateInfo { Value = 8 }, new RateInfo { Value = 9 } }; Swap us = new Swap(); SetPropertyValue setSwapValue = new SetPropertyValue(SetFieldValue); GetPropertyValue <Swap> getSwapValue = new GetPropertyValue <Swap>(GetFieldValue); using (us.GetContext(State.Default)) { SetToDefault(us); } List <RateInfo> rateInfos = GetOriginalRateInfos(us); List <Dictionary <FieldName, RateInfo> > productsCombination = new List <Dictionary <FieldName, RateInfo> >(); GetCombination(us, rateInfos, ref productsCombination); foreach (var combination in productsCombination) { foreach (var item in combination) { Console.Write($"{item.Key} " + $"{item.Value.Value}, "); } Console.WriteLine(); } // Myślę że trzeba tu tylko jeszcze wrzucić wartości do jakichś słowników i będzie to co trzeba. // Tylko GetAllCombinations(us, FieldsConst, newRates); }
/// <summary> /// Property value caller /// </summary> public PropertyCallerInfo(GetPropertyValue <TType, TPropertyType> funcGetValue, Action <TType, TPropertyType> funcSetValue) { GetValueAction = funcGetValue; SetValueAction = funcSetValue; }