forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComposer.cs
More file actions
78 lines (71 loc) · 2.29 KB
/
Composer.cs
File metadata and controls
78 lines (71 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security;
using System.Text;
namespace Simple.Data
{
public abstract class Composer
{
protected static readonly Assembly ThisAssembly = typeof (Composer).Assembly;
private static Composer _composer = new MefHelper();
public static Composer Default
{
get { return _composer; }
}
public abstract T Compose<T>();
public abstract T Compose<T>(string contractName);
internal static void SetDefault(Composer composer)
{
_composer = composer;
}
public static string GetSimpleDataAssemblyPath()
{
var path = ThisAssembly.CodeBase.Replace("file:///", "").Replace("file://", "//");
path = Path.GetDirectoryName(path);
if (path == null) throw new ArgumentException("Unrecognised assemblyFile.");
if (!Path.IsPathRooted(path))
{
path = Path.DirectorySeparatorChar + path;
}
return path;
}
public static bool TryLoadAssembly(string assemblyFile, out Assembly assembly)
{
if (assemblyFile == null) throw new ArgumentNullException("assemblyFile");
if (assemblyFile.Length == 0) throw new ArgumentException("Assembly file name is empty.", "assemblyFile");
try
{
assembly = Assembly.ReflectionOnlyLoadFrom(assemblyFile);
return true;
}
catch (FileNotFoundException)
{
assembly = null;
return false;
}
catch(FileLoadException)
{
assembly = null;
return false;
}
catch (BadImageFormatException)
{
assembly = null;
return false;
}
catch(SecurityException)
{
assembly = null;
return false;
}
catch (PathTooLongException)
{
assembly = null;
return false;
}
}
}
}