diff options
| author | Victoria Plitt <Victoria.Plitt@twine-s.com> | 2020-04-26 08:34:14 +0200 |
|---|---|---|
| committer | Victoria Plitt <Victoria.Plitt@twine-s.com> | 2020-04-26 08:34:14 +0200 |
| commit | 9038c50a3a350312c9895240be28b4fb90fbe3b1 (patch) | |
| tree | cbc8824844340d8eff76f57455fcea8e7a9d6cd6 /Software/Visual_Studio/Scripting/Tango.Scripting.Basic/ReferenceAssembly.cs | |
| parent | ccaf9afcde7a630a7d765fa9874322b5f44e2df7 (diff) | |
| parent | 8438e5447b6bed6ed97f2e3fda6977ce4bf74d0c (diff) | |
| download | Tango-9038c50a3a350312c9895240be28b4fb90fbe3b1.tar.gz Tango-9038c50a3a350312c9895240be28b4fb90fbe3b1.zip | |
Merge branch 'master' of https://twinetfs.visualstudio.com/Tango/_git/Tango
Diffstat (limited to 'Software/Visual_Studio/Scripting/Tango.Scripting.Basic/ReferenceAssembly.cs')
| -rw-r--r-- | Software/Visual_Studio/Scripting/Tango.Scripting.Basic/ReferenceAssembly.cs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/ReferenceAssembly.cs b/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/ReferenceAssembly.cs new file mode 100644 index 000000000..665bd7ab1 --- /dev/null +++ b/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/ReferenceAssembly.cs @@ -0,0 +1,88 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using Tango.Core.Helpers; + +namespace Tango.Scripting.Basic +{ + public class ReferenceAssembly + { + private static Dictionary<String, Assembly> _assembliesCache; + + static ReferenceAssembly() + { + _assembliesCache = new Dictionary<string, Assembly>(); + } + + public String File { get; set; } + + public Type HintType { get; set; } + + [JsonIgnore] + public String Name + { + get { return Path.GetFileNameWithoutExtension(File); } + } + + public static ReferenceAssembly FromType(Type type) + { + ReferenceAssembly reference = new ReferenceAssembly(); + reference.HintType = type; + var assembly = type.Assembly; + reference.File = assembly.Location; + return reference; + } + + public static ReferenceAssembly FromFile(String file) + { + return new ReferenceAssembly() { File = file }; + } + + public Assembly Load() + { + Assembly loaded = null; + if (!_assembliesCache.TryGetValue(Name, out loaded)) + { + try + { + if (HintType != null) + { + loaded = HintType.Assembly; + } + else + { + loaded = Assembly.LoadFrom(File); + } + + _assembliesCache.Add(Name, loaded); + } + catch + { + try + { + String dotNetPath = AssemblyHelper.GetAssemblyTargetFrameworkFolder(Assembly.GetExecutingAssembly()); + String dotNetAsm = Path.Combine(dotNetPath, Name + ".dll"); + loaded = Assembly.LoadFrom(dotNetAsm); + _assembliesCache.Add(Name, loaded); + } + catch (Exception ex) + { + throw new FileNotFoundException($"Could not load assembly '{Name}'. File not found.", ex); + } + } + } + + return loaded; + } + + public override string ToString() + { + return Name; + } + } +} |
