forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatform.cs
More file actions
116 lines (100 loc) · 3.52 KB
/
Copy pathPlatform.cs
File metadata and controls
116 lines (100 loc) · 3.52 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Reflection;
using ServiceStack.Configuration;
using ServiceStack.Platforms;
using ServiceStack.Web;
namespace ServiceStack
{
public class Platform
{
public static Platform Instance =
#if NETSTANDARD2_0
new PlatformNetCore();
#else
new PlatformNet();
#endif
public virtual HashSet<string> GetRazorNamespaces()
{
return new HashSet<string>();
}
public virtual void InitHostConfig(HostConfig config) {}
public virtual string GetNullableAppSetting(string key)
{
return null;
}
public virtual string GetAppSetting(string key)
{
return null;
}
public virtual string GetAppSetting(string key, string defaultValue)
{
return defaultValue;
}
public virtual T GetAppSetting<T>(string key, T defaultValue)
{
return defaultValue;
}
public virtual string GetConnectionString(string key)
{
return null;
}
public virtual string GetAppConfigPath()
{
return null;
}
public virtual Dictionary<string, string> GetCookiesAsDictionary(IRequest httpReq)
{
return new Dictionary<string, string>();
}
public virtual Dictionary<string, string> GetCookiesAsDictionary(IResponse httpRes)
{
return new Dictionary<string, string>();
}
/// <summary>
/// Get the static Parse(string) method on the type supplied
/// </summary>
private static MethodInfo GetParseMethod(Type type)
{
const string parseMethod = "Parse";
if (type == typeof(string))
return typeof(ConfigUtils).GetMethod(parseMethod, BindingFlags.Public | BindingFlags.Static);
var parseMethodInfo = type.GetStaticMethod(parseMethod, new[] { typeof(string) });
return parseMethodInfo;
}
/// <summary>
/// Gets the constructor info for T(string) if exists.
/// </summary>
private static ConstructorInfo GetConstructorInfo(Type type)
{
foreach (var ci in type.GetConstructors())
{
var ciTypes = ci.GetGenericArguments();
var matchFound = (ciTypes.Length == 1 && ciTypes[0] == typeof(string)); //e.g. T(string)
if (matchFound)
return ci;
}
return null;
}
/// <summary>
/// Returns the value returned by the 'T.Parse(string)' method if exists otherwise 'new T(string)'.
/// e.g. if T was a TimeSpan it will return TimeSpan.Parse(textValue).
/// If there is no Parse Method it will attempt to create a new instance of the destined type
/// </summary>
public static T ParseTextValue<T>(string textValue)
{
var parseMethod = GetParseMethod(typeof(T));
if (parseMethod == null)
{
var ci = GetConstructorInfo(typeof(T));
if (ci == null)
throw new TypeLoadException(
$"Error creating type {typeof(T).GetOperationName()} from text '{textValue}");
var newT = ci.Invoke(null, new object[] { textValue });
return (T)newT;
}
var value = parseMethod.Invoke(null, new object[] { textValue });
return (T)value;
}
}
}