Entitas  0.40.0
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
TypeSerializationExtension.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text.RegularExpressions;
5 
6 namespace Entitas {
7 
8  public static class TypeSerializationExtension {
9 
10  /// Generates a simplified type string for the specified type that
11  /// can be compiled. This is useful for code generation that will
12  /// produce compilable source code.
13  /// e.g. int instead of System.Int32
14  /// e.g. System.Collections.Generic.Dictionary<int, string> instead of
15  /// System.Collections.Generic.Dictionary`2[System.Int32,System.String]
16  public static string ToCompilableString(this Type type) {
17  if(_builtInTypesToString.ContainsKey(type.FullName)) {
18  return _builtInTypesToString[type.FullName];
19  }
20  if(type.IsGenericType) {
21  var genericMainType = type.FullName.Split('`')[0];
22  var genericArguments = type.GetGenericArguments().Select(
23  argType => argType.ToCompilableString()
24  ).ToArray();
25  return genericMainType +
26  "<" + string.Join(", ", genericArguments) + ">";
27  }
28  if(type.IsArray) {
29  return type.GetElementType().ToCompilableString() +
30  "[" + new string(',', type.GetArrayRank() - 1) + "]";
31  }
32  if(type.IsNested) {
33  return type.FullName.Replace('+', '.');
34  }
35 
36  return type.FullName;
37  }
38 
39  /// Tries to find and create a type based on the specified type string.
40  public static Type ToType(this string typeString) {
41  var fullTypeName = generateTypeString(typeString);
42  var type = Type.GetType(fullTypeName);
43  if(type != null) {
44  return type;
45  }
46 
47  foreach(var assembly in AppDomain.CurrentDomain.GetAssemblies()) {
48  type = assembly.GetType(fullTypeName);
49  if(type != null) {
50  return type;
51  }
52  }
53 
54  return null;
55  }
56 
57  public static string ShortTypeName(this string fullTypeName) {
58  var split = fullTypeName.Split('.');
59  return split[split.Length - 1];
60  }
61 
62  public static string RemoveDots(this string fullTypeName) {
63  return fullTypeName.Replace(".", string.Empty);
64  }
65 
66  static string generateTypeString(string typeString) {
67  if(_builtInTypeStrings.ContainsKey(typeString)) {
68  typeString = _builtInTypeStrings[typeString];
69  } else {
70  typeString = generateGenericArguments(typeString);
71  typeString = generateArray(typeString);
72  }
73 
74  return typeString;
75  }
76 
77  static string generateGenericArguments(string typeString) {
78  const string genericArgsPattern = @"<(?<arg>.*)>";
79  var separator = new[] { ", " };
80  typeString = Regex.Replace(typeString, genericArgsPattern,
81  m => {
82  var ts = generateTypeString(m.Groups["arg"].Value);
83  var argsCount = ts.Split(separator, StringSplitOptions.None).Length;
84 
85  return "`" + argsCount + "[" + ts + "]";
86  });
87 
88  return typeString;
89  }
90 
91  static string generateArray(string typeString) {
92  const string arrayPattern = @"(?<type>[^\[]*)(?<rank>\[,*\])";
93  typeString = Regex.Replace(typeString, arrayPattern,
94  m => {
95  var type = generateTypeString(m.Groups["type"].Value);
96  var rank = m.Groups["rank"].Value;
97  return type + rank;
98  });
99 
100  return typeString;
101  }
102 
103  static readonly Dictionary<string, string> _builtInTypesToString = new Dictionary<string, string>() {
104  { "System.Boolean", "bool" },
105  { "System.Byte", "byte" },
106  { "System.SByte", "sbyte" },
107  { "System.Char", "char" },
108  { "System.Decimal", "decimal" },
109  { "System.Double", "double" },
110  { "System.Single", "float" },
111  { "System.Int32", "int" },
112  { "System.UInt32", "uint" },
113  { "System.Int64", "long" },
114  { "System.UInt64", "ulong" },
115  { "System.Object", "object" },
116  { "System.Int16", "short" },
117  { "System.UInt16", "ushort" },
118  { "System.String", "string" },
119  { "System.Void", "void" }
120  };
121 
122  static readonly Dictionary<string, string> _builtInTypeStrings = new Dictionary<string, string>() {
123  { "bool", "System.Boolean" },
124  { "byte", "System.Byte" },
125  { "sbyte", "System.SByte" },
126  { "char", "System.Char" },
127  { "decimal", "System.Decimal" },
128  { "double", "System.Double" },
129  { "float", "System.Single" },
130  { "int", "System.Int32" },
131  { "uint", "System.UInt32" },
132  { "long", "System.Int64" },
133  { "ulong", "System.UInt64" },
134  { "object", "System.Object" },
135  { "short", "System.Int16" },
136  { "ushort", "System.UInt16" },
137  { "string", "System.String" },
138  { "void", "System.Void" }
139  };
140  }
141 }
static Type ToType(this string typeString)
Tries to find and create a type based on the specified type string.
static string ToCompilableString(this Type type)