private static object GetValueFromExtraDataProvider <T>(object key, IExtraDataContainer dataProvider) { object value = null; if (key != null) { value = dataProvider.GetExtraData(key); } return(value ?? dataProvider.Values.FirstOrDefault(o => { if (o is T) { return true; } var delegateInstance = o as Delegate; if (delegateInstance != null) { return delegateInstance.GetMethodInfo().ReturnType == typeof(T); } return false; })); }
/// <summary> /// Get a value from extra data or return default. will convert /// </summary> /// <typeparam name="T"></typeparam> /// <param name="container"></param> /// <param name="key"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static T GetExtraDataOrDefaultValue <T>(this IExtraDataContainer container, object key, T defaultValue = default(T)) { var value = container.GetExtraData(key); if (value == null) { return(defaultValue); } if (value is T) { return((T)value); } return((T)Convert.ChangeType(value, typeof(T))); }
/// <summary> /// Get value from extra data provider /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="dataProvider"></param> /// <param name="tValue"></param> /// <returns></returns> protected virtual bool GetValueFromExtraDataProvider <T>(object key, IExtraDataContainer dataProvider, out object tValue) { object value = null; if (key != null) { value = dataProvider.GetExtraData(key); } if (value != null) { tValue = value; return(true); } foreach (var o in dataProvider.KeyValuePairs) { if (o.Key is string stringKey && stringKey.StartsWith(UniqueStringId.Prefix)) { continue; } if (o.Value is T) { tValue = o.Value; return(true); } if (o.Value is Delegate delegateInstance && delegateInstance.GetMethodInfo().ReturnType == typeof(T)) { tValue = o.Value; return(true); } } tValue = null; return(false); }
protected virtual bool GetValueFromExtraDataProvider(Type type, object key, IExtraDataContainer dataProvider, out object tValue) { object value = null; if (key != null) { value = dataProvider.GetExtraData(key); } if (value != null) { tValue = value; return(true); } foreach (var o in dataProvider.Values) { if (type.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo())) { tValue = o; return(true); } var delegateInstance = o as Delegate; if (delegateInstance != null && delegateInstance.GetMethodInfo().ReturnType == type) { tValue = o; return(true); } } tValue = null; return(false); }
public static ITenant GetTenantContext(this IExtraDataContainer scopeData) => (ITenant)scopeData.GetExtraData(TENANT_KEY);
public static T GetExtraData <T>(this IExtraDataContainer container, object key) { return((T)container.GetExtraData(key)); }