forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompilerServices.cs
More file actions
85 lines (75 loc) · 3.01 KB
/
CompilerServices.cs
File metadata and controls
85 lines (75 loc) · 3.01 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
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
namespace ServiceStack.Razor.Compilation
{
/// <summary>
/// Provides service methods for compilation.
/// </summary>
public static class CompilerServices
{
private static readonly Type DynamicType = typeof(DynamicObject);
private static readonly Type ExpandoType = typeof(ExpandoObject);
/// <summary>
/// Determines if the specified type is an anonymous type.
/// </summary>
/// <param name="type">The type to check.</param>
/// <returns>True if the type is an anonymous type, otherwise false.</returns>
public static bool IsAnonymousType(Type type)
{
if (type == null)
throw new ArgumentNullException("type");
return (type.IsClass
&& type.IsSealed
&& type.BaseType == typeof(object)
&& type.Name.StartsWith("<>")
&& type.IsDefined(typeof(CompilerGeneratedAttribute), true));
}
/// <summary>
/// Determines if the specified type is a dynamic type.
/// </summary>
/// <param name="type">The type to check.</param>
/// <returns>True if the type is an anonymous type, otherwise false.</returns>
public static bool IsDynamicType(Type type)
{
if (type == null)
throw new ArgumentNullException("type");
return (DynamicType.IsAssignableFrom(type)
|| ExpandoType.IsAssignableFrom(type)
|| IsAnonymousType(type));
}
/// <summary>
/// Generates a random class name.
/// </summary>
/// <returns>A new random class name.</returns>
public static string GenerateClassName()
{
return Regex.Replace(Guid.NewGuid().ToString("N"), @"[^A-Za-z]*", "");
}
/// <summary>
/// Gets the public or protected constructors of the specified type.
/// </summary>
/// <param name="type">The target type.</param>
/// <returns>An enumerable of constructors.</returns>
public static IEnumerable<ConstructorInfo> GetConstructors(Type type)
{
if (type == null)
throw new ArgumentNullException("type");
var constructors = type
.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
return constructors;
}
/// <summary>
/// Gets an enumerable of all assemblies loaded in the current domain.
/// </summary>
/// <returns>An enumerable of loaded assemblies.</returns>
public static IEnumerable<Assembly> GetLoadedAssemblies()
{
var domain = AppDomain.CurrentDomain;
return domain.GetAssemblies();
}
}
}