Foo.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using XLua;
  6. namespace XLuaTest
  7. {
  8. [LuaCallCSharp]
  9. public class Foo1Parent
  10. {
  11. }
  12. [LuaCallCSharp]
  13. public class Foo2Parent
  14. {
  15. }
  16. [LuaCallCSharp]
  17. public class Foo1Child : Foo1Parent
  18. {
  19. }
  20. [LuaCallCSharp]
  21. public class Foo2Child : Foo2Parent
  22. {
  23. }
  24. [LuaCallCSharp]
  25. public class Foo
  26. {
  27. #region Supported methods
  28. public void Test1<T>(T a) where T : Foo1Parent
  29. {
  30. Debug.Log(string.Format("Test1<{0}>", typeof(T)));
  31. }
  32. public T1 Test2<T1, T2>(T1 a, T2 b, GameObject c) where T1 : Foo1Parent where T2 : Foo2Parent
  33. {
  34. Debug.Log(string.Format("Test2<{0},{1}>", typeof(T1), typeof(T2)), c);
  35. return a;
  36. }
  37. #endregion
  38. #region Unsupported methods
  39. /// <summary>
  40. /// 不支持生成lua的泛型方法(没有泛型约束)
  41. /// </summary>
  42. public void UnsupportedMethod1<T>(T a)
  43. {
  44. Debug.Log("UnsupportedMethod1");
  45. }
  46. /// <summary>
  47. /// 不支持生成lua的泛型方法(缺少带约束的泛型参数)
  48. /// </summary>
  49. public void UnsupportedMethod2<T>() where T : Foo1Parent
  50. {
  51. Debug.Log(string.Format("UnsupportedMethod2<{0}>", typeof(T)));
  52. }
  53. /// <summary>
  54. /// 不支持生成lua的泛型方法(泛型约束必须为class)
  55. /// </summary>
  56. public void UnsupportedMethod3<T>(T a) where T : IDisposable
  57. {
  58. Debug.Log(string.Format("UnsupportedMethod3<{0}>", typeof(T)));
  59. }
  60. #endregion
  61. }
  62. [LuaCallCSharp]
  63. public static class FooExtension
  64. {
  65. public static void PlainExtension(this Foo1Parent a)
  66. {
  67. Debug.Log("PlainExtension");
  68. }
  69. public static T Extension1<T>(this T a) where T : Foo1Parent
  70. {
  71. Debug.Log(string.Format("Extension1<{0}>", typeof(T)));
  72. return a;
  73. }
  74. public static T Extension2<T>(this T a, GameObject b) where T : Foo1Parent
  75. {
  76. Debug.Log(string.Format("Extension2<{0}>", typeof(T)), b);
  77. return a;
  78. }
  79. public static void Extension2<T1, T2>(this T1 a, T2 b) where T1 : Foo1Parent where T2 : Foo2Parent
  80. {
  81. Debug.Log(string.Format("Extension2<{0},{1}>", typeof(T1), typeof(T2)));
  82. }
  83. public static T UnsupportedExtension<T>(this GameObject obj) where T : Component
  84. {
  85. return obj.GetComponent<T>();
  86. }
  87. }
  88. }