-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProtobufAdapter.cs
More file actions
82 lines (71 loc) · 2.42 KB
/
ProtobufAdapter.cs
File metadata and controls
82 lines (71 loc) · 2.42 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
using System;
using UnityEngine;
using ILRuntime.Runtime.Enviorment;
using System.IO;
using ILRuntime.CLR.TypeSystem;
using ILRuntime.CLR.Method;
using ILRuntime.Runtime.Generated;
using System.Collections.Generic;
using System.Reflection;
using ProtoBuf;
namespace MegoEngine
{
class ProtobufAdapter : MonoBehaviour,IProtobufTypeAdapter
{
private static ILRuntime.Runtime.Enviorment.AppDomain appdomain;
void Awake()
{
//此处调用初始化
InitProtobufTypes();
}
#region ProtobufTypeAdapter
public object CreateInstance(Type type)
{
var _type = appdomain.GetType(type);
return appdomain.Instantiate(_type.FullName);
}
public Type TYPE_TIMESPAN { get; private set; }
public Type TYPE_GUID { get; private set; }
public Type TYPE_URI { get; private set; }
public Type TYPE_BYTEARRAY { get; private set; }
public Type TYPE_SYSTYPE { get; private set; }
public object ConvertCLRInstance(object value)
{
ILRuntime.Runtime.Intepreter.ILTypeInstance instance = value as ILRuntime.Runtime.Intepreter.ILTypeInstance;
if (instance != null)
{
return instance.CLRInstance;
}
return value;
}
void InitProtobufTypes()
{
TYPE_TIMESPAN = GetType("System.TimeSpan");
TYPE_GUID = GetType("System.Guid");
TYPE_URI = typeof(Uri);
TYPE_BYTEARRAY = GetType("System.Byte[]");
TYPE_SYSTYPE = GetType("System.Type");
ProtobufTypeHelper.typeAdapter = this;
}
public Type GetListItemType(Type type)
{
var wt = type as ILRuntime.Reflection.ILRuntimeWrapperType;
if (wt != null)
{
var clrType = wt.CLRType;
if (clrType != null && clrType.GenericArguments != null && clrType.GenericArguments.Length > 0)
{
type = clrType.GenericArguments[0].Value.ReflectionType;
return type;
}
}
return null;
}
public Type GetType(string classFullName)
{
var type = appdomain.GetType(classFullName);
return type == null ? null : type.ReflectionType;
}
#endregion
}
}