From f17c72e03843c9ae6d26c5638dcc14d00dba9341 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sun, 24 Apr 2005 18:38:55 +0000 Subject: [PATCH] Replaced LoadWithPartialName through Assembly.Load, it seems to do the same job in .NET 2.0. Optimized the StringParser a bit. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@110 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Src/Nodes/EnvironmentNode.cs | 2 +- .../ClassBrowser/Nodes/ReferenceFolderNode.cs | 2 +- .../Src/Project/Items/ReferenceProjectItem.cs | 2 +- .../ParserService/ProjectContentRegistry.cs | 2 +- .../Src/Services/StringParser/StringParser.cs | 148 +++++++++--------- 5 files changed, 77 insertions(+), 79 deletions(-) diff --git a/src/AddIns/Misc/HighlightingEditor/Project/Src/Nodes/EnvironmentNode.cs b/src/AddIns/Misc/HighlightingEditor/Project/Src/Nodes/EnvironmentNode.cs index 7fb65fc387..989e4437fb 100644 --- a/src/AddIns/Misc/HighlightingEditor/Project/Src/Nodes/EnvironmentNode.cs +++ b/src/AddIns/Misc/HighlightingEditor/Project/Src/Nodes/EnvironmentNode.cs @@ -43,7 +43,7 @@ namespace ICSharpCode.SharpDevelop.AddIns.HighlightingEditor.Nodes EnvironmentNode.ColorNames = (string[])envColorNames.ToArray(typeof(string)); this.ColorDescs = (string[])envColorDescriptions.ToArray(typeof(string)); this.Colors = (EditorHighlightColor[])envColors.ToArray(typeof(EditorHighlightColor)); - StringParser.Parse(ref ColorDescs); + StringParser.Parse(ColorDescs); Text = ResNodeName("EnvironmentColors"); diff --git a/src/Main/Base/Project/Src/Gui/Pads/ClassBrowser/Nodes/ReferenceFolderNode.cs b/src/Main/Base/Project/Src/Gui/Pads/ClassBrowser/Nodes/ReferenceFolderNode.cs index 27e5733f13..9e41d794d1 100644 --- a/src/Main/Base/Project/Src/Gui/Pads/ClassBrowser/Nodes/ReferenceFolderNode.cs +++ b/src/Main/Base/Project/Src/Gui/Pads/ClassBrowser/Nodes/ReferenceFolderNode.cs @@ -88,7 +88,7 @@ namespace ICSharpCode.SharpDevelop.Gui assembly = Assembly.ReflectionOnlyLoadFrom(item.FileName); } catch (Exception) { try { - assembly = Assembly.LoadWithPartialName(item.Include); + assembly = Assembly.ReflectionOnlyLoad(item.Include); } catch (Exception e) { Console.WriteLine("Can't load assembly '{0}' : " + e.Message, item.Include); } diff --git a/src/Main/Base/Project/Src/Project/Items/ReferenceProjectItem.cs b/src/Main/Base/Project/Src/Project/Items/ReferenceProjectItem.cs index 9e30476584..98532729c5 100644 --- a/src/Main/Base/Project/Src/Project/Items/ReferenceProjectItem.cs +++ b/src/Main/Base/Project/Src/Project/Items/ReferenceProjectItem.cs @@ -99,7 +99,7 @@ namespace ICSharpCode.SharpDevelop.Project if (info.Length < 4) { try { - Assembly refAssembly = Assembly.LoadWithPartialName(referenceName); + Assembly refAssembly = Assembly.Load(referenceName); // if it failed, then return just the short name if (refAssembly == null) { diff --git a/src/Main/Base/Project/Src/Services/ParserService/ProjectContentRegistry.cs b/src/Main/Base/Project/Src/Services/ParserService/ProjectContentRegistry.cs index 227133d7b8..880b014896 100644 --- a/src/Main/Base/Project/Src/Services/ParserService/ProjectContentRegistry.cs +++ b/src/Main/Base/Project/Src/Services/ParserService/ProjectContentRegistry.cs @@ -57,7 +57,7 @@ namespace ICSharpCode.Core } } catch (Exception) { try { - assembly = Assembly.LoadWithPartialName(item.Include); + assembly = Assembly.ReflectionOnlyLoad(item.Include); if (assembly != null) { contents[item.Include] = CaseSensitiveProjectContent.Create(assembly); return contents[item.Include]; diff --git a/src/Main/Core/Project/Src/Services/StringParser/StringParser.cs b/src/Main/Core/Project/Src/Services/StringParser/StringParser.cs index f335006ceb..4248757be1 100644 --- a/src/Main/Core/Project/Src/Services/StringParser/StringParser.cs +++ b/src/Main/Core/Project/Src/Services/StringParser/StringParser.cs @@ -24,9 +24,9 @@ namespace ICSharpCode.Core /// public static class StringParser { - static Dictionary properties = new Dictionary(); - static Dictionary stringTagProviders = new Dictionary(); - static Dictionary propertyObjects = new Dictionary(); + readonly static Dictionary properties; + readonly static Dictionary stringTagProviders; + readonly static Dictionary propertyObjects; public static Dictionary Properties { get { @@ -42,13 +42,16 @@ namespace ICSharpCode.Core static StringParser() { - Assembly entryAssembly = System.Reflection.Assembly.GetEntryAssembly(); - + Assembly entryAssembly = Assembly.GetEntryAssembly(); + properties = new Dictionary(StringComparer.InvariantCultureIgnoreCase); + stringTagProviders = new Dictionary(StringComparer.InvariantCultureIgnoreCase); + propertyObjects = new Dictionary(); // entryAssembly == null might happen in unit test mode if (entryAssembly != null) { string exeName = entryAssembly.Location; propertyObjects["exe"] = FileVersionInfo.GetVersionInfo(exeName); } + properties["USER"] = Environment.UserName; } public static string Parse(string input) @@ -57,11 +60,11 @@ namespace ICSharpCode.Core } /// - /// Parses an array and replaces the elements + /// Parses an array and replaces the elements in the existing array. /// - public static void Parse(ref string[] inputs) + public static void Parse(string[] inputs) { - for (int i = inputs.GetLowerBound(0); i <= inputs.GetUpperBound(0); ++i) { + for (int i = 0; i < inputs.Length; ++i) { inputs[i] = Parse(inputs[i], null); } } @@ -73,7 +76,7 @@ namespace ICSharpCode.Core } } - readonly static Regex pattern = new Regex(@"\$\{([^\}]*)\}"); + readonly static Regex pattern = new Regex(@"\$\{([^\}]*)\}", RegexOptions.Compiled | RegexOptions.CultureInvariant); /// /// Expands ${xyz} style property values. @@ -87,71 +90,13 @@ namespace ICSharpCode.Core string token = m.ToString(); string propertyName = m.Groups[1].Captures[0].Value; - string propertyNameUpper= propertyName.ToUpper(); - string propertyValue = null; - switch (propertyName.ToUpper()) { - case "USER": // current user - propertyValue = Environment.UserName; - break; - case "DATE": // current date - propertyValue = DateTime.Today.ToShortDateString(); - break; - case "TIME": // current time - propertyValue = DateTime.Now.ToShortTimeString(); - break; - default: - propertyValue = null; - if (customTags != null) { - for (int j = 0; j < customTags.GetLength(0); ++j) { - if (propertyName.ToUpper() == customTags[j, 0].ToUpper()) { - propertyValue = customTags[j, 1]; - break; - } - } - } - - if (propertyValue == null && properties.ContainsKey(propertyName)) { - propertyValue = properties[propertyName]; - } - - if (propertyValue == null && properties.ContainsKey(propertyNameUpper)) { - propertyValue = properties[propertyNameUpper]; - } - if (propertyValue == null && stringTagProviders.ContainsKey(propertyName)) { - propertyValue = stringTagProviders[propertyName].Convert(propertyName); - } - - if (propertyValue == null) { - int k = propertyName.IndexOf(':'); - if (k > 0) { - switch (propertyName.Substring(0, k).ToUpper()) { - - case "ENV": - propertyValue = Environment.GetEnvironmentVariable(propertyName.Substring(k + 1)); - break; - - case "RES": - try { - propertyValue = Parse(ResourceService.GetString(propertyName.Substring(k + 1)), customTags); - } catch (Exception) { - propertyValue = null; - } - break; - - case "PROPERTY": - propertyValue = PropertyService.Get(propertyName.Substring(k + 1)); - break; - - default: - object obj = propertyObjects[propertyName.Substring(0, k)]; - propertyValue = Get(obj, propertyName.Substring(k + 1)); - break; - } - } - } - break; - } + string propertyValue = GetValue(propertyName, customTags); + if (propertyValue != null) { + if (m.Length == input.Length) { + // safe a replace operation when input is a property on its own. + return propertyValue; + } output = output.Replace(token, propertyValue); } } @@ -160,11 +105,64 @@ namespace ICSharpCode.Core return output; } - static string Get(object obj, string name) + static string GetValue(string propertyName, string[,] customTags) { - if (obj == null) { + if (propertyName.StartsWith("res:")) { + // most properties start with res: in lowercase, + // so we can safe 2 string allocations here + try { + return Parse(ResourceService.GetString(propertyName.Substring(4)), customTags); + } catch (ResourceNotFoundException) { + return null; + } + } + if (propertyName.Equals("DATE", StringComparison.InvariantCultureIgnoreCase)) + return DateTime.Today.ToShortDateString(); + if (propertyName.Equals("TIME", StringComparison.InvariantCultureIgnoreCase)) + return DateTime.Now.ToShortTimeString(); + + if (customTags != null) { + for (int j = 0; j < customTags.GetLength(0); ++j) { + if (propertyName.Equals(customTags[j, 0], StringComparison.InvariantCultureIgnoreCase)) { + return customTags[j, 1]; + } + } + } + + if (properties.ContainsKey(propertyName)) { + return properties[propertyName]; + } + + if (stringTagProviders.ContainsKey(propertyName)) { + return stringTagProviders[propertyName].Convert(propertyName); + } + + int k = propertyName.IndexOf(':'); + if (k <= 0) return null; + string prefix = propertyName.Substring(0, k); + switch (prefix.ToUpper()) { + case "ENV": + return Environment.GetEnvironmentVariable(propertyName.Substring(k + 1)); + case "RES": + try { + return Parse(ResourceService.GetString(propertyName.Substring(k + 1)), customTags); + } catch (ResourceNotFoundException) { + return null; + } + case "PROPERTY": + return PropertyService.Get(propertyName.Substring(k + 1)); + default: + if (propertyObjects.ContainsKey(prefix)) { + return Get(propertyObjects[prefix], propertyName.Substring(k + 1)); + } else { + return null; + } } + } + + static string Get(object obj, string name) + { Type type = obj.GetType(); PropertyInfo prop = type.GetProperty(name); if (prop != null) {