Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit c39e097

Browse files
committed
Add support for methods with ref/in/out args
1 parent 129a7e3 commit c39e097

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

src/CachedObjects/CacheObjectBase.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ private static CacheObjectBase GetCacheObjectImpl(object obj, MemberInfo memberI
114114
var pi = memberInfo as PropertyInfo;
115115
var mi = memberInfo as MethodInfo;
116116

117-
// if PropertyInfo, check if can process args
118-
if (pi != null && !CanProcessArgs(pi.GetIndexParameters()))
117+
// Check if can process args
118+
if ((pi != null && !CanProcessArgs(pi.GetIndexParameters()))
119+
|| (mi != null && !CanProcessArgs(mi.GetParameters())))
119120
{
120121
return null;
121122
}
@@ -127,14 +128,7 @@ private static CacheObjectBase GetCacheObjectImpl(object obj, MemberInfo memberI
127128

128129
if (mi != null)
129130
{
130-
if (CacheMethod.CanEvaluate(mi))
131-
{
132-
holder = new CacheMethod();
133-
}
134-
else
135-
{
136-
return null;
137-
}
131+
holder = new CacheMethod();
138132
}
139133
else if (valueType == typeof(GameObject) || valueType == typeof(Transform))
140134
{
@@ -211,7 +205,18 @@ public static bool CanProcessArgs(ParameterInfo[] parameters)
211205
{
212206
foreach (var param in parameters)
213207
{
214-
if (!param.ParameterType.IsPrimitive && param.ParameterType != typeof(string))
208+
var pType = param.ParameterType;
209+
210+
if (pType.IsByRef && pType.HasElementType)
211+
{
212+
pType = pType.GetElementType();
213+
}
214+
215+
if (pType.IsPrimitive || pType == typeof(string))
216+
{
217+
continue;
218+
}
219+
else
215220
{
216221
return false;
217222
}
@@ -241,6 +246,11 @@ public object[] ParseArguments()
241246
var input = m_argumentInput[i];
242247
var type = m_arguments[i].ParameterType;
243248

249+
if (type.IsByRef)
250+
{
251+
type = type.GetElementType();
252+
}
253+
244254
if (!string.IsNullOrEmpty(input))
245255
{
246256
// strings can obviously just be used directly

src/CachedObjects/Other/CacheMethod.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ public class CacheMethod : CacheObjectBase
1818

1919
public string[] GenericArgInput = new string[0];
2020

21-
public static bool CanEvaluate(MethodInfo mi)
22-
{
23-
// primitive and string args supported
24-
return CanProcessArgs(mi.GetParameters());
25-
}
26-
2721
public override void Init()
2822
{
2923
var mi = (MemInfo as MethodInfo);

src/Tests/TestClass.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections;
22
using System.Collections.Generic;
3+
using System;
34
using UnityEngine;
45

56
namespace Explorer.Tests
@@ -21,10 +22,16 @@ public TestClass()
2122
public static int StaticField = 5;
2223
public int NonStaticField;
2324

24-
// test a generic method
2525
public static string TestGeneric<C, T>(string arg0) where C : Component
2626
{
27-
return "C: " + typeof(C).FullName + ", T: " + typeof(T).FullName + ", arg0: " + arg0;
27+
return $"C: '{typeof(C).FullName}', T: '{typeof(T).FullName}', arg0: '{arg0}'";
28+
}
29+
30+
public static string TestRefInOutGeneric<T>(ref string arg0, in int arg1, out string arg2)
31+
{
32+
arg2 = "this is arg2";
33+
34+
return $"T: '{typeof(T).FullName}', ref arg0: '{arg0}', in arg1: '{arg1}', out arg2: '{arg2}'";
2835
}
2936

3037
//// this type of generic is not supported, due to requiring a non-primitive argument.

0 commit comments

Comments
 (0)