diff options
| author | Avi Levkovich <avi@twine-s.com> | 2020-04-23 18:24:10 +0300 |
|---|---|---|
| committer | Avi Levkovich <avi@twine-s.com> | 2020-04-23 18:24:10 +0300 |
| commit | 8438e5447b6bed6ed97f2e3fda6977ce4bf74d0c (patch) | |
| tree | 4efca7fbcdbb241ac3760e372dda75f99638dde0 /Software/Visual_Studio/Scripting/Tango.Scripting.Basic/ReferenceAssembly.cs | |
| parent | f48d6b6714c3115c3b2031e84b85a9fa7442c40f (diff) | |
| parent | 5627d88d6dec269a0b2dcc4538773fe6bbfc767d (diff) | |
| download | Tango-8438e5447b6bed6ed97f2e3fda6977ce4bf74d0c.tar.gz Tango-8438e5447b6bed6ed97f2e3fda6977ce4bf74d0c.zip | |
merge conflict
Diffstat (limited to 'Software/Visual_Studio/Scripting/Tango.Scripting.Basic/ReferenceAssembly.cs')
| -rw-r--r-- | Software/Visual_Studio/Scripting/Tango.Scripting.Basic/ReferenceAssembly.cs | 76 |
1 files changed, 73 insertions, 3 deletions
diff --git a/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/ReferenceAssembly.cs b/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/ReferenceAssembly.cs index be66e026b..665bd7ab1 100644 --- a/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/ReferenceAssembly.cs +++ b/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/ReferenceAssembly.cs @@ -1,18 +1,88 @@ -using System; +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 { - public Type FromType { get; set; } + 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 FromType.Assembly.GetName().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; } } } |
