Tanoda
MethodCacheEntry.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3using System.Reflection;
4
6{
8 {
9 public MethodCacheEntry(object ins, MethodInfo m) : base(GetMethodName(ins, m))
10 {
11 if (m == null)
12 throw new ArgumentNullException();
13
14 _instance = ins;
15 MethodInfo = m;
16 }
17
18 private static string GetMethodName(object ins, MethodBase methodInfo)
19 {
20 if (methodInfo != null)
21 {
22 var name = FieldCacheEntry.GetMemberName(ins, methodInfo);
23
24 var genericArguments = methodInfo.GetGenericArguments();
25 if (genericArguments.Any())
26 {
27 name += "<" + string.Join(", ", genericArguments.Select(x => x.Name).ToArray()) + ">";
28 }
29
30 return name;
31 }
32 return "INVALID";
33 }
34
35 public MethodInfo MethodInfo { get; private set; }
36
37 private readonly object _instance;
38 private object _valueCache;
39
40 public override object GetValueToCache()
41 {
42 return (_instance == null ? "Static " : "") + "Method call - enter to evaluate";
43 }
44
45 public override object GetValue()
46 {
47 return _valueCache ?? base.GetValue();
48 }
49
50 public override object EnterValue()
51 {
52 try
53 {
54 // If this is the first user clicked, eval the method and display the result. second time enter as normal
55 if (_valueCache == null)
56 {
57 var result = MethodInfo.Invoke(_instance, null);
58
59 _valueCache = result;
60 return null;
61 }
62
63 return _valueCache;
64 }
65 catch (Exception ex)
66 {
67 RuntimeUnityEditorCore.Logger.Log(LogLevel.Warning, "Failed to evaluate the method" + Name() + " - " +ex.Message);
68 _valueCache = ex;
69 return null;
70 }
71 }
72
73 protected override bool OnSetValue(object newValue)
74 {
75 return false;
76 }
77
78 public override Type Type()
79 {
80 return MethodInfo.ReturnType;
81 }
82
83 public override bool CanSetValue()
84 {
85 return false;
86 }
87
88 public override bool CanEnterValue()
89 {
90 return _valueCache == null || base.CanEnterValue();
91 }
92 }
93}
string Name()
override Type Type()
override object GetValueToCache()
override bool CanEnterValue()
override bool OnSetValue(object newValue)
override bool CanSetValue()
MethodInfo MethodInfo
override object GetValue()
override object EnterValue()
Get object that is entered when variable name is clicked in inspector
MethodCacheEntry(object ins, MethodInfo m)
void Log(LogLevel logLogLevel, object content)