private static ViewDataInfo GetIndexedPropertyValue(object indexableObject, string key) { IDictionary <string, object> dictionary = indexableObject as IDictionary <string, object>; object obj = (object)null; bool flag = false; if (dictionary != null) { flag = dictionary.TryGetValue(key, out obj); } else { TryGetValueDelegate getValueDelegate = TypeHelpers.CreateTryGetValueDelegate(indexableObject.GetType()); if (getValueDelegate != null) { flag = getValueDelegate(indexableObject, key, out obj); } } if (!flag) { return((ViewDataInfo)null); } return(new ViewDataInfo() { Container = indexableObject, Value = obj }); }
private static ViewDataInfo GetIndexedPropertyValue(object indexableObject, string key) { IDictionary <string, object> dict = indexableObject as IDictionary <string, object>; object value = null; bool success = false; if (dict != null) { success = dict.TryGetValue(key, out value); } else { TryGetValueDelegate tgvDel = TypeHelpers.CreateTryGetValueDelegate(indexableObject.GetType()); if (tgvDel != null) { success = tgvDel(indexableObject, key, out value); } } if (success) { return(new ViewDataInfo() { Container = indexableObject, Value = value }); } return(null); }
public static IntComparison ParseString(string input, TryGetValueDelegate parser) { Comparison comparison; string numSubstring; if (input.Length > 1) { switch (input.ElementAt(0)) { case '=': comparison = Comparison.Equal; numSubstring = input.Substring(1); break; case '>': if (input.Length > 2 && input.ElementAt(1) == '=') { comparison = Comparison.GreaterEqual; numSubstring = input.Substring(2); } else { comparison = Comparison.Greater; numSubstring = input.Substring(1); } break; case '<': if (input.Length > 2 && input.ElementAt(1) == '=') { comparison = Comparison.LesserEqual; numSubstring = input.Substring(2); } else { comparison = Comparison.Lesser; numSubstring = input.Substring(1); } break; default: comparison = Comparison.Equal; numSubstring = input; break; } } else { comparison = Comparison.Equal; numSubstring = input; } int num; if (parser(numSubstring, out num)) { return(new IntComparison(num, comparison)); } return(null); }
private static bool IsTestProject(TryGetValueDelegate <ProjectConfigReader> tryGetValue, Compilation compilation, AnalyzerOptions options) { var projectType = ProjectConfiguration(tryGetValue, options).ProjectType; return(projectType == ProjectType.Unknown ? compilation.IsTest() // SonarLint, NuGet or Scanner <= 5.0 : projectType == ProjectType.Test); // Scanner >= 5.1 does authoritative decision that we follow }
private static ProjectConfigReader ProjectConfiguration(TryGetValueDelegate <ProjectConfigReader> tryGetValue, AnalyzerOptions options) { if (options.AdditionalFiles.FirstOrDefault(IsSonarProjectConfig) is { } sonarProjectConfigXml) { return(sonarProjectConfigXml.GetText() is { } sourceText // TryGetValue catches all exceptions from SourceTextValueProvider and returns false when thrown && tryGetValue(sourceText, ProjectConfigProvider, out var cachedProjectConfigReader) ? cachedProjectConfigReader : throw new InvalidOperationException($"File {Path.GetFileName(sonarProjectConfigXml.Path)} has been added as an AdditionalFile but could not be read and parsed.")); }
public void CreateTryGetValueDelegateReturnsNullForNonDictionaries() { // Arrange object notDictionary = "Hello, world"; // Act TryGetValueDelegate d = TypeHelpers.CreateTryGetValueDelegate(notDictionary.GetType()); // Assert Assert.IsNull(d); }
public static bool TryGetValue(this IDictionary dictionary, string key, out object value) { Precondition.Require(dictionary, () => Error.ArgumentNull("dictionary")); TryGetValueDelegate dlgt = CreateTryGetValueDelegate(dictionary.GetType()); if (dlgt == null) { throw Error.CouldNotCreateTryGetValueDelegateForType(dictionary.GetType()); } return(dlgt(dictionary, key, out value)); }
public static TryGetValueDelegate CreateTryGetValueDelegate(Type targetType) { _tryGetValueDelegateCacheLock.EnterReadLock(); TryGetValueDelegate tryGetValueDelegate; try { if (_tryGetValueDelegateCache.TryGetValue(targetType, out tryGetValueDelegate)) { return(tryGetValueDelegate); } } finally { _tryGetValueDelegateCacheLock.ExitReadLock(); } Type type = ExtractGenericInterface(targetType, typeof(IDictionary <,>)); if (type != null) { Type[] genericArguments = type.GetGenericArguments(); Type type2 = genericArguments[0]; Type type3 = genericArguments[1]; if (type2.IsAssignableFrom(typeof(string))) { MethodInfo method = _strongTryGetValueImplInfo.MakeGenericMethod(new Type[] { type2, type3 }); tryGetValueDelegate = (TryGetValueDelegate)Delegate.CreateDelegate(typeof(TryGetValueDelegate), method); } } if (tryGetValueDelegate == null && typeof(IDictionary).IsAssignableFrom(targetType)) { tryGetValueDelegate = new TryGetValueDelegate(TryGetValueFromNonGenericDictionary); } _tryGetValueDelegateCacheLock.EnterWriteLock(); try { _tryGetValueDelegateCache[targetType] = tryGetValueDelegate; } finally { _tryGetValueDelegateCacheLock.ExitWriteLock(); } return(tryGetValueDelegate); }
public void CreateTryGetValueDelegateWrapsGenericStringDictionaries() { // Arrange object dictionary = new Dictionary <string, int>() { { "theKey", 42 } }; // Act TryGetValueDelegate d = TypeHelpers.CreateTryGetValueDelegate(dictionary.GetType()); object value; bool found = d(dictionary, "theKey", out value); // Assert Assert.IsTrue(found); Assert.AreEqual(42, value); }
public void CreateTryGetValueDelegateWrapsNonGenericDictionaries() { // Arrange object dictionary = new Hashtable() { { "foo", 42 } }; // Act TryGetValueDelegate d = TypeHelpers.CreateTryGetValueDelegate(dictionary.GetType()); object fooValue; bool fooFound = d(dictionary, "foo", out fooValue); object barValue; bool barFound = d(dictionary, "bar", out barValue); // Assert Assert.IsTrue(fooFound); Assert.AreEqual(42, fooValue); Assert.IsFalse(barFound); Assert.IsNull(barValue); }
public static TryGetValueDelegate CreateTryGetValueDelegate(Type targetType) { TypeHelpers._tryGetValueDelegateCacheLock.EnterReadLock(); TryGetValueDelegate getValueDelegate; try { if (TypeHelpers._tryGetValueDelegateCache.TryGetValue(targetType, out getValueDelegate)) { return(getValueDelegate); } } finally { TypeHelpers._tryGetValueDelegateCacheLock.ExitReadLock(); } Type genericInterface = TypeHelpers.ExtractGenericInterface(targetType, typeof(IDictionary <,>)); if (genericInterface != (Type)null) { Type[] genericArguments = genericInterface.GetGenericArguments(); Type type1 = genericArguments[0]; Type type2 = genericArguments[1]; if (type1.IsAssignableFrom(typeof(string))) { getValueDelegate = (TryGetValueDelegate)Delegate.CreateDelegate(typeof(TryGetValueDelegate), TypeHelpers._strongTryGetValueImplInfo.MakeGenericMethod(type1, type2)); } } if (getValueDelegate == null && typeof(IDictionary).IsAssignableFrom(targetType)) { getValueDelegate = new TryGetValueDelegate(TypeHelpers.TryGetValueFromNonGenericDictionary); } TypeHelpers._tryGetValueDelegateCacheLock.EnterWriteLock(); try { TypeHelpers._tryGetValueDelegateCache[targetType] = getValueDelegate; } finally { TypeHelpers._tryGetValueDelegateCacheLock.ExitWriteLock(); } return(getValueDelegate); }
internal static bool GetBoolean(string key, TryGetValueDelegate tryGetValue, bool defaultValue = default) { return(tryGetValue(key, out var value) ? Convert.ToBoolean(value, CultureInfo.InvariantCulture) : defaultValue); }
public InstanceDataWeakTable() { _dict = Activator.CreateInstance(_TableType); _tryGetValue = (TryGetValueDelegate)Delegate.CreateDelegate(typeof(TryGetValueDelegate), _dict, _TryGetValueMethod); _add = (AddDelegate)Delegate.CreateDelegate(typeof(AddDelegate), _dict, _AddMethod); }
public SimpleReactiveDictionary(IObservable <IEnumerable <ReactiveDictionaryChange <TKey, TValue> > > changes, TryGetValueDelegate <TKey, TValue> trygetvalue) { this.changes = changes; this.trygetvalue = trygetvalue; }
internal static IReactiveDictionary <TKey, TValue> ToDictionary <TKey, TValue>( this IObservable <IEnumerable <ReactiveDictionaryChange <TKey, TValue> > > sequence, TryGetValueDelegate <TKey, TValue> trygetvalue) { return(new SimpleReactiveDictionary <TKey, TValue>(sequence, trygetvalue)); }
/// <summary>Initializes a new instance of the <see cref="GenericVirtualReadOnlyDictionaryy{TKey, TValue}"/> class.</summary> /// <param name="keys">The enumerated list of keys.</param> /// <param name="getValue">The function used to get a value given a key. Called directly by <c>TryGetValue</c>.</param> /// <param name="hasKey"> /// An optional function to directly determine if a key exists. If not supplied, the default implementation checks for equality on /// every value in <paramref name="keys"/>. /// </param> public GenericVirtualReadOnlyDictionaryy(IEnumerable <TKey> keys, TryGetValueDelegate getValue, Func <TKey, bool> hasKey = null) { Keys = keys ?? throw new ArgumentNullException(nameof(keys)); getValFunc = getValue ?? throw new ArgumentNullException(nameof(getValue)); hasKeyFunc = hasKey ?? DefHasKey; }
public DelegateFieldMapping(Expression <Func <TField, TProperty> > expression, TryGetValueDelegate @delegate) : base(expression) { Delegate = @delegate; }
internal static int GetInt32(string key, TryGetValueDelegate tryGetValue, int defaultValue = default) { return(tryGetValue(key, out var value) ? Convert.ToInt32(value, CultureInfo.InvariantCulture) : defaultValue); }
internal static byte[] GetBytes(string key, TryGetValueDelegate tryGetValue, byte[] defaultValue = default) { return(tryGetValue(key, out var value) ? (byte[])value : defaultValue); }
internal static string GetString(string key, TryGetValueDelegate tryGetValue, string defaultValue = default) { return(tryGetValue(key, out var value) ? Convert.ToString(value, CultureInfo.InvariantCulture) : defaultValue); }
internal static byte GetByte(string key, TryGetValueDelegate tryGetValue, byte defaultValue = default) { return(tryGetValue(key, out var value) ? Convert.ToByte(value, CultureInfo.CurrentCulture) : defaultValue); }
private static bool IsTestProject(TryGetValueDelegate <ProjectConfigReader> tryGetValue, Compilation c, AnalyzerOptions options) => IsTest(ProjectConfiguration(tryGetValue, options).ProjectType, c);
public ExecutionContext(IParser parser, bool caseSensitive, bool cacheExternalValue, bool failWhenMissingVariable, TryGetValueDelegate <T> tryGetValueDelegate) { this.Parser = parser; this.CaseSensitive = caseSensitive; this.CacheExternalValue = cacheExternalValue; this.TryGetExternalData = tryGetValueDelegate; this.FailWhenMissingVariable = failWhenMissingVariable; Store = caseSensitive ? new Dictionary <string, ContextValue>() : new Dictionary <string, ContextValue>(StringComparer.InvariantCultureIgnoreCase); }
internal static FbWireCrypt GetWireCrypt(string key, TryGetValueDelegate tryGetValue, FbWireCrypt defaultValue = default) { return(tryGetValue(key, out var value) ? (FbWireCrypt)value : defaultValue); }
internal static IsolationLevel GetIsolationLevel(string key, TryGetValueDelegate tryGetValue, IsolationLevel defaultValue = default) { return(tryGetValue(key, out var value) ? (IsolationLevel)value : defaultValue); }
internal static FbServerType GetServerType(string key, TryGetValueDelegate tryGetValue, FbServerType defaultValue = default) { return(tryGetValue(key, out var value) ? (FbServerType)value : defaultValue); }