Tanoda
SerializableType.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Reflection;
11using UnityEngine;
12
13namespace Leap.Unity {
14
15 [Serializable]
16 public struct SerializableType : ISerializationCallbackReceiver {
17
18 [SerializeField, HideInInspector]
19 private Type _type;
20
21 [SerializeField, HideInInspector]
22 private string _fullName;
23
24 private static Assembly[] _cachedAssemblies = null;
25 private static Assembly[] _assemblies {
26 get {
27 if (_cachedAssemblies == null) {
28 _cachedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
29 }
30 return _cachedAssemblies;
31 }
32 }
33
34 public void OnAfterDeserialize() {
35 if (!string.IsNullOrEmpty(_fullName)) {
36 foreach (var assembly in _assemblies) {
37 _type = assembly.GetType(_fullName, throwOnError: false);
38 if (_type != null) {
39 break;
40 }
41 }
42 } else {
43 _type = null;
44 }
45 }
46
47 public void OnBeforeSerialize() {
48 if (_type != null) {
49 _fullName = _type.FullName;
50 }
51 }
52
53 public static implicit operator Type(SerializableType serializableType) {
54 return serializableType._type;
55 }
56
57 public static implicit operator SerializableType(Type type) {
58 return new SerializableType() {
59 _type = type
60 };
61 }
62 }
63}