public Type InternalLocate(string typeName) { if (string.IsNullOrEmpty(typeName)) { return(null); } string mappedTypeName = FluorineConfiguration.Instance.GetMappedTypeName(typeName); //Lookup first in our cache. Type type; if (!_typeCache.TryGetValue(mappedTypeName, out type)) { type = TypeHelper.Locate(mappedTypeName); if (type != null) { _typeCache[mappedTypeName] = type; } else { type = InternalLocateInLac(mappedTypeName); // Locate in the LAC } } return(type); }
public object InternalCreateInstance(Type type, object[] args) { if (type != null) { if (type.IsAbstract && type.IsSealed) { return(type); } #if !SILVERLIGHT if (_reflectionEmitPermission) { ConstructorInvoker invoker; _typeConstructorCache.TryGetValue(type, out invoker); if (invoker == null) { invoker = ConstructorExtensions.DelegateForCreateInstance(type, args); _typeConstructorCache[type] = invoker; } return(invoker(args)); } return(Activator.CreateInstance(type, BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, args, null)); #else return(type.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance | BindingFlags.Static, null, null, args)); #endif } return(null); }
/// <summary> /// Sets an attribute on this object. /// </summary> /// <param name="name">The attribute name.</param> /// <param name="value">The attribute value.</param> /// <returns>true if the attribute value changed otherwise false.</returns> public virtual bool SetAttribute(string name, object value) { if (name == null) return false; // Update with new value object previous = null; _attributes.TryGetValue(name, out previous); if( previous == null || !value.Equals(previous) ) _attributes[name] = value; return (previous == null || !value.Equals(previous)); }
/// <summary> /// Returns the value of a named parameter, or null if there is no such parameter. /// </summary> /// <param name="parameterName">The name of the parameter to retrieve.</param> /// <returns>The parameter value, or null if it does not exist.</returns> internal string GetParameter(string parameterName) { Tuple <string, ElementLocation> parameterValue = null; if (_parameters.TryGetValue(parameterName, out parameterValue)) { return(parameterValue.Item1); } return(null); }
public void TryGetValue_ReferenceNotFound() { var dictionary = new CopyOnWriteDictionary <object>(); object v; bool result = dictionary.TryGetValue(string.Empty, out v); Assert.False(result); Assert.Null(v); Assert.False(dictionary.ContainsKey(string.Empty)); }
public void TryGetValue_ReferenceNotFound() { var dictionary = new CopyOnWriteDictionary <object, object>(); object v; bool result = dictionary.TryGetValue(new Object(), out v); Assert.AreEqual(false, result); Assert.AreEqual(null, v); Assert.AreEqual(false, dictionary.ContainsKey(new Object())); }
public void TryGetValue_ReferenceFound() { object k1 = new Object(); object v1 = new Object(); var dictionary = new CopyOnWriteDictionary<object, object>(); dictionary[k1] = v1; // Now look for the same key we inserted object v2; bool result = dictionary.TryGetValue(k1, out v2); Assert.AreEqual(true, result); Assert.AreEqual(true, Object.ReferenceEquals(v1, v2)); }
/// <summary> /// Gets the value of the parameter with the specified name, /// or empty string if it is not present. /// </summary> public string GetParameter(string name) { lock (_locker) { ErrorUtilities.VerifyThrowArgumentLength(name, nameof(name)); EnsureParametersInitialized(); if (_parameters.TryGetValue(name, out Tuple <string, ElementLocation> parameter)) { return(parameter.Item1); } return(String.Empty); } }
public void TryGetValue_ReferenceFound() { object k1 = new Object(); object v1 = new Object(); var dictionary = new CopyOnWriteDictionary <object, object>(); dictionary[k1] = v1; // Now look for the same key we inserted object v2; bool result = dictionary.TryGetValue(k1, out v2); Assert.AreEqual(true, result); Assert.AreEqual(true, Object.ReferenceEquals(v1, v2)); }
public void TryGetValue_ReferenceFound() { string k1 = new string(nameof(TryGetValue_ReferenceFound).ToCharArray()); object v1 = new Object(); var dictionary = new CopyOnWriteDictionary <object>(); dictionary[k1] = v1; // Now look for the same key we inserted object v2; bool result = dictionary.TryGetValue(k1, out v2); Assert.True(result); Assert.True(Object.ReferenceEquals(v1, v2)); }
public void ReadOperation_DelegatesToSourceDictionary_IfNoMutationsArePerformed() { // Arrange var values = new List <object>(); var enumerator = Mock.Of <IEnumerator <KeyValuePair <string, object> > >(); var sourceDictionary = new Mock <IDictionary <string, object> >(MockBehavior.Strict); sourceDictionary .SetupGet(d => d.Count) .Returns(100) .Verifiable(); sourceDictionary .SetupGet(d => d.Values) .Returns(values) .Verifiable(); sourceDictionary .Setup(d => d.ContainsKey("test-key")) .Returns(value: true) .Verifiable(); sourceDictionary .Setup(d => d.GetEnumerator()) .Returns(enumerator) .Verifiable(); sourceDictionary .Setup(d => d["key2"]) .Returns("key2-value") .Verifiable(); object value; sourceDictionary.Setup(d => d.TryGetValue("different-key", out value)) .Returns(false) .Verifiable(); var copyOnWriteDictionary = new CopyOnWriteDictionary <string, object>(sourceDictionary.Object, StringComparer.OrdinalIgnoreCase); // Act and Assert Assert.Equal("key2-value", copyOnWriteDictionary["key2"]); Assert.Equal(100, copyOnWriteDictionary.Count); Assert.Same(values, copyOnWriteDictionary.Values); Assert.True(copyOnWriteDictionary.ContainsKey("test-key")); Assert.Same(enumerator, copyOnWriteDictionary.GetEnumerator()); Assert.False(copyOnWriteDictionary.TryGetValue("different-key", out value)); sourceDictionary.Verify(); }
/// <summary> /// Returns the escaped value of the metadata with the specified key. /// </summary> string ITaskItem2.GetMetadataValueEscaped(string metadataName) { ErrorUtilities.VerifyThrowArgumentNull(metadataName, "metadataName"); string metadataValue = null; if (FileUtilities.ItemSpecModifiers.IsDerivableItemSpecModifier(metadataName)) { // FileUtilities.GetItemSpecModifier is expecting escaped data, which we assume we already are. // Passing in a null for currentDirectory indicates we are already in the correct current directory metadataValue = FileUtilities.ItemSpecModifiers.GetItemSpecModifier(null, _itemSpec, _definingProject, metadataName, ref _fullPath); } else if (_metadata != null) { _metadata.TryGetValue(metadataName, out metadataValue); } return((metadataValue == null) ? String.Empty : metadataValue); }
public void ReadOperation_DelegatesToSourceDictionary_IfNoMutationsArePerformed() { // Arrange var values = new List<object>(); var enumerator = Mock.Of<IEnumerator<KeyValuePair<string, object>>>(); var sourceDictionary = new Mock<IDictionary<string, object>>(MockBehavior.Strict); sourceDictionary .SetupGet(d => d.Count) .Returns(100) .Verifiable(); sourceDictionary .SetupGet(d => d.Values) .Returns(values) .Verifiable(); sourceDictionary .Setup(d => d.ContainsKey("test-key")) .Returns(value: true) .Verifiable(); sourceDictionary .Setup(d => d.GetEnumerator()) .Returns(enumerator) .Verifiable(); sourceDictionary .Setup(d => d["key2"]) .Returns("key2-value") .Verifiable(); object value; sourceDictionary.Setup(d => d.TryGetValue("different-key", out value)) .Returns(false) .Verifiable(); var copyOnWriteDictionary = new CopyOnWriteDictionary<string, object>(sourceDictionary.Object, StringComparer.OrdinalIgnoreCase); // Act and Assert Assert.Equal("key2-value", copyOnWriteDictionary["key2"]); Assert.Equal(100, copyOnWriteDictionary.Count); Assert.Same(values, copyOnWriteDictionary.Values); Assert.True(copyOnWriteDictionary.ContainsKey("test-key")); Assert.Same(enumerator, copyOnWriteDictionary.GetEnumerator()); Assert.False(copyOnWriteDictionary.TryGetValue("different-key", out value)); sourceDictionary.Verify(); }
/// <inheritdoc /> public TMetricType GetNodeMetric <TMetricType>(Host host, NodeMetric nodeMetric) where TMetricType : class, IDriverMetric { if (!_nodeMetricsCollection.TryGetValue(host, out var nodeMetrics)) { throw new ArgumentException("Could not retrieve metrics for this host: " + host.Address); } var metric = nodeMetrics.MetricsRegistry.GetMetric(nodeMetric); if (metric == null) { throw new ArgumentException("Could not find the provided metric: ", nodeMetric.Name); } if (!(metric is TMetricType typedMetric)) { throw new ArgumentException( $"Node Metric {nodeMetric.Name} is not of type {typeof(TMetricType).Name}. Its type is {metric.GetType().Name}."); } return(typedMetric); }
public bool TryGet(IPEndPoint endpoint, out Host host) { return(_hosts.TryGetValue(endpoint, out host)); }
internal static string GetItemSpecModifier(string currentDirectory, string itemSpec, string modifier, ref CopyOnWriteDictionary<string, string> cachedModifiers) { ErrorUtilities.VerifyThrow(itemSpec != null, "Need item-spec to modify."); ErrorUtilities.VerifyThrow(modifier != null, "Need modifier to apply to item-spec."); string fullPath = null; if (cachedModifiers != null) { cachedModifiers.TryGetValue(modifier, out fullPath); } if (fullPath == null) { bool flag = true; try { if (string.Compare(modifier, "FullPath", StringComparison.OrdinalIgnoreCase) == 0) { if (currentDirectory == null) { currentDirectory = string.Empty; } fullPath = FileUtilities.GetFullPath(itemSpec, currentDirectory); ThrowForUrl(fullPath, itemSpec, currentDirectory); } else if (string.Compare(modifier, "RootDir", StringComparison.OrdinalIgnoreCase) == 0) { string str2; if (currentDirectory == null) { currentDirectory = string.Empty; } if ((cachedModifiers == null) || !cachedModifiers.TryGetValue("FullPath", out str2)) { str2 = FileUtilities.GetFullPath(itemSpec, currentDirectory); ThrowForUrl(str2, itemSpec, currentDirectory); } fullPath = Path.GetPathRoot(str2); if (!FileUtilities.EndsWithSlash(fullPath)) { ErrorUtilities.VerifyThrow(FileUtilitiesRegex.UNCPattern.IsMatch(fullPath), "Only UNC shares should be missing trailing slashes."); fullPath = fullPath + Path.DirectorySeparatorChar; } } else if (string.Compare(modifier, "Filename", StringComparison.OrdinalIgnoreCase) == 0) { if (Path.GetDirectoryName(itemSpec) == null) { fullPath = string.Empty; } else { fullPath = Path.GetFileNameWithoutExtension(itemSpec); } } else if (string.Compare(modifier, "Extension", StringComparison.OrdinalIgnoreCase) == 0) { if (Path.GetDirectoryName(itemSpec) == null) { fullPath = string.Empty; } else { fullPath = Path.GetExtension(itemSpec); } } else if (string.Compare(modifier, "RelativeDir", StringComparison.OrdinalIgnoreCase) == 0) { fullPath = FileUtilities.GetDirectory(itemSpec); } else if (string.Compare(modifier, "Directory", StringComparison.OrdinalIgnoreCase) == 0) { string str3; if (currentDirectory == null) { currentDirectory = string.Empty; } if ((cachedModifiers == null) || !cachedModifiers.TryGetValue("FullPath", out str3)) { str3 = FileUtilities.GetFullPath(itemSpec, currentDirectory); ThrowForUrl(str3, itemSpec, currentDirectory); } fullPath = FileUtilities.GetDirectory(str3); Match match = FileUtilitiesRegex.DrivePattern.Match(fullPath); if (!match.Success) { match = FileUtilitiesRegex.UNCPattern.Match(fullPath); } if (match.Success) { ErrorUtilities.VerifyThrow((fullPath.Length > match.Length) && FileUtilities.IsSlash(fullPath[match.Length]), "Root directory must have a trailing slash."); fullPath = fullPath.Substring(match.Length + 1); } } else if (string.Compare(modifier, "RecursiveDir", StringComparison.OrdinalIgnoreCase) == 0) { fullPath = string.Empty; } else if (string.Compare(modifier, "Identity", StringComparison.OrdinalIgnoreCase) == 0) { flag = cachedModifiers != null; fullPath = itemSpec; } else if (string.Compare(modifier, "ModifiedTime", StringComparison.OrdinalIgnoreCase) == 0) { flag = false; FileInfo fileInfoNoThrow = FileUtilities.GetFileInfoNoThrow(EscapingUtilities.UnescapeAll(itemSpec)); if (fileInfoNoThrow != null) { fullPath = fileInfoNoThrow.LastWriteTime.ToString("yyyy'-'MM'-'dd HH':'mm':'ss'.'fffffff", null); } else { fullPath = string.Empty; } } else if (string.Compare(modifier, "CreatedTime", StringComparison.OrdinalIgnoreCase) == 0) { flag = false; string path = EscapingUtilities.UnescapeAll(itemSpec); if (File.Exists(path)) { fullPath = File.GetCreationTime(path).ToString("yyyy'-'MM'-'dd HH':'mm':'ss'.'fffffff", null); } else { fullPath = string.Empty; } } else if (string.Compare(modifier, "AccessedTime", StringComparison.OrdinalIgnoreCase) == 0) { flag = false; string str6 = EscapingUtilities.UnescapeAll(itemSpec); if (File.Exists(str6)) { fullPath = File.GetLastAccessTime(str6).ToString("yyyy'-'MM'-'dd HH':'mm':'ss'.'fffffff", null); } else { fullPath = string.Empty; } } else { ErrorUtilities.VerifyThrow(false, "\"{0}\" is not a valid item-spec modifier.", modifier); } } catch (Exception exception) { if (ExceptionHandling.NotExpectedException(exception)) { throw; } ErrorUtilities.VerifyThrowInvalidOperation(false, "Shared.InvalidFilespecForTransform", modifier, itemSpec, exception.Message); } ErrorUtilities.VerifyThrow(fullPath != null, "The item-spec modifier \"{0}\" was not evaluated.", modifier); if (flag) { if (cachedModifiers == null) { cachedModifiers = new CopyOnWriteDictionary<string, string>(StringComparer.OrdinalIgnoreCase); } cachedModifiers[modifier] = fullPath; } } return fullPath; }
internal static string GetItemSpecModifier(string currentDirectory, string itemSpec, string modifier, ref CopyOnWriteDictionary <string, string> cachedModifiers) { ErrorUtilities.VerifyThrow(itemSpec != null, "Need item-spec to modify."); ErrorUtilities.VerifyThrow(modifier != null, "Need modifier to apply to item-spec."); string fullPath = null; if (cachedModifiers != null) { cachedModifiers.TryGetValue(modifier, out fullPath); } if (fullPath == null) { bool flag = true; try { if (string.Compare(modifier, "FullPath", StringComparison.OrdinalIgnoreCase) == 0) { if (currentDirectory == null) { currentDirectory = string.Empty; } fullPath = FileUtilities.GetFullPath(itemSpec, currentDirectory); ThrowForUrl(fullPath, itemSpec, currentDirectory); } else if (string.Compare(modifier, "RootDir", StringComparison.OrdinalIgnoreCase) == 0) { string str2; if (currentDirectory == null) { currentDirectory = string.Empty; } if ((cachedModifiers == null) || !cachedModifiers.TryGetValue("FullPath", out str2)) { str2 = FileUtilities.GetFullPath(itemSpec, currentDirectory); ThrowForUrl(str2, itemSpec, currentDirectory); } fullPath = Path.GetPathRoot(str2); if (!FileUtilities.EndsWithSlash(fullPath)) { ErrorUtilities.VerifyThrow(FileUtilitiesRegex.UNCPattern.IsMatch(fullPath), "Only UNC shares should be missing trailing slashes."); fullPath = fullPath + Path.DirectorySeparatorChar; } } else if (string.Compare(modifier, "Filename", StringComparison.OrdinalIgnoreCase) == 0) { if (Path.GetDirectoryName(itemSpec) == null) { fullPath = string.Empty; } else { fullPath = Path.GetFileNameWithoutExtension(itemSpec); } } else if (string.Compare(modifier, "Extension", StringComparison.OrdinalIgnoreCase) == 0) { if (Path.GetDirectoryName(itemSpec) == null) { fullPath = string.Empty; } else { fullPath = Path.GetExtension(itemSpec); } } else if (string.Compare(modifier, "RelativeDir", StringComparison.OrdinalIgnoreCase) == 0) { fullPath = FileUtilities.GetDirectory(itemSpec); } else if (string.Compare(modifier, "Directory", StringComparison.OrdinalIgnoreCase) == 0) { string str3; if (currentDirectory == null) { currentDirectory = string.Empty; } if ((cachedModifiers == null) || !cachedModifiers.TryGetValue("FullPath", out str3)) { str3 = FileUtilities.GetFullPath(itemSpec, currentDirectory); ThrowForUrl(str3, itemSpec, currentDirectory); } fullPath = FileUtilities.GetDirectory(str3); Match match = FileUtilitiesRegex.DrivePattern.Match(fullPath); if (!match.Success) { match = FileUtilitiesRegex.UNCPattern.Match(fullPath); } if (match.Success) { ErrorUtilities.VerifyThrow((fullPath.Length > match.Length) && FileUtilities.IsSlash(fullPath[match.Length]), "Root directory must have a trailing slash."); fullPath = fullPath.Substring(match.Length + 1); } } else if (string.Compare(modifier, "RecursiveDir", StringComparison.OrdinalIgnoreCase) == 0) { fullPath = string.Empty; } else if (string.Compare(modifier, "Identity", StringComparison.OrdinalIgnoreCase) == 0) { flag = cachedModifiers != null; fullPath = itemSpec; } else if (string.Compare(modifier, "ModifiedTime", StringComparison.OrdinalIgnoreCase) == 0) { flag = false; FileInfo fileInfoNoThrow = FileUtilities.GetFileInfoNoThrow(EscapingUtilities.UnescapeAll(itemSpec)); if (fileInfoNoThrow != null) { fullPath = fileInfoNoThrow.LastWriteTime.ToString("yyyy'-'MM'-'dd HH':'mm':'ss'.'fffffff", null); } else { fullPath = string.Empty; } } else if (string.Compare(modifier, "CreatedTime", StringComparison.OrdinalIgnoreCase) == 0) { flag = false; string path = EscapingUtilities.UnescapeAll(itemSpec); if (File.Exists(path)) { fullPath = File.GetCreationTime(path).ToString("yyyy'-'MM'-'dd HH':'mm':'ss'.'fffffff", null); } else { fullPath = string.Empty; } } else if (string.Compare(modifier, "AccessedTime", StringComparison.OrdinalIgnoreCase) == 0) { flag = false; string str6 = EscapingUtilities.UnescapeAll(itemSpec); if (File.Exists(str6)) { fullPath = File.GetLastAccessTime(str6).ToString("yyyy'-'MM'-'dd HH':'mm':'ss'.'fffffff", null); } else { fullPath = string.Empty; } } else { ErrorUtilities.VerifyThrow(false, "\"{0}\" is not a valid item-spec modifier.", modifier); } } catch (Exception exception) { if (ExceptionHandling.NotExpectedException(exception)) { throw; } ErrorUtilities.VerifyThrowInvalidOperation(false, "Shared.InvalidFilespecForTransform", modifier, itemSpec, exception.Message); } ErrorUtilities.VerifyThrow(fullPath != null, "The item-spec modifier \"{0}\" was not evaluated.", modifier); if (flag) { if (cachedModifiers == null) { cachedModifiers = new CopyOnWriteDictionary <string, string>(StringComparer.OrdinalIgnoreCase); } cachedModifiers[modifier] = fullPath; } } return(fullPath); }
public void TryGetValue_ReferenceNotFound() { var dictionary = new CopyOnWriteDictionary<object, object>(); object v; bool result = dictionary.TryGetValue(new Object(), out v); Assert.AreEqual(false, result); Assert.AreEqual(null, v); Assert.AreEqual(false, dictionary.ContainsKey(new Object())); }