面向对象的反射框架UnityCsReflection——简单示例
跳转至专题目录专题推荐文章:
[*]温故知新——RectTransform成员属性的再认识
[*]unity Scene View扩展之编辑器扩展总结
[*]Unity获取鼠标点击ui GameObject
[*]添加Inspector上的Add Component回调功能
本系列目录
[*]UnityCsReflection面向对象的反射框架介绍
[*]面向对象的反射框架UnityCsReflection用途介绍
[*]面向对象的反射框架UnityCsReflection——简单示例
<hr/>现在假如dll里面有这么两个类,ATest,BTest,我们需要调用里面的私有成员和私有函数。
namespace Hvak.Editor.Refleaction
{
class ATest
{
int a = 2;
void Log()
{
Debug.Log(&#34;Log ATest&#34;);
}
}
class BTest
{
string str = &#34;hello world!&#34;;
ATest aTest
{
get; set;
} = new ATest();
}
}
1、首先我们设置反射生成代码的参数,然后因为生成的代码比较长,所以放到了最后。
static void GenerateNewWay()
{
// 0、如果需要用到dll的别名,可以在这里注册。(因为不知道怎么反射dll的alias名,先用笨方法,手动注册了)
ModuleAliasConfig.Set(&#34;&#34;, &#34;&#34;);
// 1、指定生成代码的文件夹路径
GenerateRtype.UnityCSReflectionPath = $&#34;{Application.dataPath}/UnityCsReflection/&#34;;
// 2、添加需要生成的类,重载了三种接口,这是其中传入类型名字的接口,其余两种是传类型列表,传实例列表
GenerateRtype.Generate(new List<string> { &#34;UnityType&#34;, &#34;ComponentDropdownItem&#34;, &#34;AddComponentWindow&#34;, &#34;PackageManagerWindow&#34;, &#34;BTest&#34; });
}
2、生成了RType之后,可以按照以下方法调用ATest,BTest相关成员变量和方法。
static void TestRType()
{
// 1、这里虽然是直接new了一个BTest,但是现实情况可能通过其他手段拿到的BTest实例
BTest bTest = new BTest();
// 2、new一个RBTest,并把BTest的实例传进去
RBTest rBTest = new RBTest(bTest);
// 3、尝试调用通过RBTest来调用BTest里面的private field
Debug.Log(rBTest.RFstr.Value);
// 4、尝试调用通过RBTest来调用BTest里面的private property
Debug.Log(rBTest.RPaTest.RFa.Value);
// 5、尝试调用通过RBTest来调用BTest里面的private method
rBTest.RPaTest.Log();
}
3、输出结果
3、github
跳转中... (zhihu.com)中的GenerateRClassTest.cs
<hr/>
自动生成的代码模样
using Hvak.Editor.Refleaction;
using System;
using System.Reflection;
namespace Hvak.Editor.Refleaction.RHvak.REditor.RRefleaction
{
/// <summary>
/// Hvak.Editor.Refleaction.BTest
/// </summary>
public partial class RBTest : RMember //
{
/// <summary>
/// System.String str
/// </summary>
protected Hvak.Editor.Refleaction.RSystem.RString r_Fstr;
public virtual Hvak.Editor.Refleaction.RSystem.RString RFstr
{
get
{
if(r_Fstr == null)
{
r_Fstr = new(this, &#34;str&#34;);
}
return r_Fstr;
}
}
/// <summary>
/// Hvak.Editor.Refleaction.ATest <aTest>k__BackingField
/// </summary>
protected Hvak.Editor.Refleaction.RHvak.REditor.RRefleaction.RATest r_F__0__aTest__1__k__BackingField;
public virtual Hvak.Editor.Refleaction.RHvak.REditor.RRefleaction.RATest RF__0__aTest__1__k__BackingField
{
get
{
if(r_F__0__aTest__1__k__BackingField == null)
{
r_F__0__aTest__1__k__BackingField = new(this, &#34;<aTest>k__BackingField&#34;);
}
return r_F__0__aTest__1__k__BackingField;
}
}
/// <summary>
/// Hvak.Editor.Refleaction.ATest aTest
/// </summary>
protected Hvak.Editor.Refleaction.RHvak.REditor.RRefleaction.RATest r_PaTest;
public virtual Hvak.Editor.Refleaction.RHvak.REditor.RRefleaction.RATest RPaTest
{
get
{
if(r_PaTest == null)
{
r_PaTest = new(this, &#34;aTest&#34;, -1);
}
return r_PaTest;
}
}
/// <summary>
/// Boolean Equals(System.Object)
/// </summary>
protected RMethod r_MEquals_Object;
public virtual RMethod RMEquals_Object
{
get
{
if(r_MEquals_Object == null)
{
r_MEquals_Object = new(this, &#34;Equals&#34;, 0, typeof(System.Object));
}
return r_MEquals_Object;
}
}
/// <summary>
/// Void Finalize()
/// </summary>
protected RMethod r_MFinalize;
public virtual RMethod RMFinalize
{
get
{
if(r_MFinalize == null)
{
r_MFinalize = new(this, &#34;Finalize&#34;, 0);
}
return r_MFinalize;
}
}
/// <summary>
/// Int32 GetHashCode()
/// </summary>
protected RMethod r_MGetHashCode;
public virtual RMethod RMGetHashCode
{
get
{
if(r_MGetHashCode == null)
{
r_MGetHashCode = new(this, &#34;GetHashCode&#34;, 0);
}
return r_MGetHashCode;
}
}
/// <summary>
/// System.Type GetType()
/// </summary>
protected RMethod r_MGetType;
public virtual RMethod RMGetType
{
get
{
if(r_MGetType == null)
{
r_MGetType = new(this, &#34;GetType&#34;, 0);
}
return r_MGetType;
}
}
/// <summary>
/// System.Object MemberwiseClone()
/// </summary>
protected RMethod r_MMemberwiseClone;
public virtual RMethod RMMemberwiseClone
{
get
{
if(r_MMemberwiseClone == null)
{
r_MMemberwiseClone = new(this, &#34;MemberwiseClone&#34;, 0);
}
return r_MMemberwiseClone;
}
}
/// <summary>
/// System.String ToString()
/// </summary>
protected RMethod r_MToString;
public virtual RMethod RMToString
{
get
{
if(r_MToString == null)
{
r_MToString = new(this, &#34;ToString&#34;, 0);
}
return r_MToString;
}
}
public RBTest() : base(&#34;Hvak.Editor.Refleaction.BTest&#34;)
{
}
public RBTest(System.Object instance) : base(&#34;Hvak.Editor.Refleaction.BTest&#34;)
{
SetInstance(instance);
}
public RBTest(RMember belongMember, string name, int genericCount = -1, params Type[] types) : base(belongMember, name, genericCount, types)
{
}
public RBTest(Type belongType, string name, int genericCount = -1, params Type[] types) : base(belongType, name, genericCount, types)
{
}
public virtual System.Boolean Equals(System.Object @obj)
{
var ___genericsType = new Type[] {};
var ___parameters = new object[]{@obj};
var ___result = RMEquals_Object.Invoke(___genericsType, ___parameters);
return (System.Boolean)___result;
}
public virtual void Finalize()
{
var ___genericsType = new Type[] {};
var ___parameters = new object[]{};
var ___result = RMFinalize.Invoke(___genericsType, ___parameters);
}
public virtual System.Int32 GetHashCode()
{
var ___genericsType = new Type[] {};
var ___parameters = new object[]{};
var ___result = RMGetHashCode.Invoke(___genericsType, ___parameters);
return (System.Int32)___result;
}
public virtual System.Type GetType()
{
var ___genericsType = new Type[] {};
var ___parameters = new object[]{};
var ___result = RMGetType.Invoke(___genericsType, ___parameters);
return (System.Type)___result;
}
public virtual System.Object MemberwiseClone()
{
var ___genericsType = new Type[] {};
var ___parameters = new object[]{};
var ___result = RMMemberwiseClone.Invoke(___genericsType, ___parameters);
return (System.Object)___result;
}
public virtual System.String ToString()
{
var ___genericsType = new Type[] {};
var ___parameters = new object[]{};
var ___result = RMToString.Invoke(___genericsType, ___parameters);
return (System.String)___result;
}
}
}
using Hvak.Editor.Refleaction;
using System;
using System.Reflection;
namespace Hvak.Editor.Refleaction.RHvak.REditor.RRefleaction
{
/// <summary>
/// Hvak.Editor.Refleaction.ATest
/// </summary>
public partial class RATest : RMember //
{
/// <summary>
/// System.Int32 a
/// </summary>
protected Hvak.Editor.Refleaction.RSystem.RInt32 r_Fa;
public virtual Hvak.Editor.Refleaction.RSystem.RInt32 RFa
{
get
{
if(r_Fa == null)
{
r_Fa = new(this, &#34;a&#34;);
}
return r_Fa;
}
}
/// <summary>
/// Void Log()
/// </summary>
protected RMethod r_MLog;
public virtual RMethod RMLog
{
get
{
if(r_MLog == null)
{
r_MLog = new(this, &#34;Log&#34;, 0);
}
return r_MLog;
}
}
/// <summary>
/// Boolean Equals(System.Object)
/// </summary>
protected RMethod r_MEquals_Object;
public virtual RMethod RMEquals_Object
{
get
{
if(r_MEquals_Object == null)
{
r_MEquals_Object = new(this, &#34;Equals&#34;, 0, typeof(System.Object));
}
return r_MEquals_Object;
}
}
/// <summary>
/// Void Finalize()
/// </summary>
protected RMethod r_MFinalize;
public virtual RMethod RMFinalize
{
get
{
if(r_MFinalize == null)
{
r_MFinalize = new(this, &#34;Finalize&#34;, 0);
}
return r_MFinalize;
}
}
/// <summary>
/// Int32 GetHashCode()
/// </summary>
protected RMethod r_MGetHashCode;
public virtual RMethod RMGetHashCode
{
get
{
if(r_MGetHashCode == null)
{
r_MGetHashCode = new(this, &#34;GetHashCode&#34;, 0);
}
return r_MGetHashCode;
}
}
/// <summary>
/// System.Type GetType()
/// </summary>
protected RMethod r_MGetType;
public virtual RMethod RMGetType
{
get
{
if(r_MGetType == null)
{
r_MGetType = new(this, &#34;GetType&#34;, 0);
}
return r_MGetType;
}
}
/// <summary>
/// System.Object MemberwiseClone()
/// </summary>
protected RMethod r_MMemberwiseClone;
public virtual RMethod RMMemberwiseClone
{
get
{
if(r_MMemberwiseClone == null)
{
r_MMemberwiseClone = new(this, &#34;MemberwiseClone&#34;, 0);
}
return r_MMemberwiseClone;
}
}
/// <summary>
/// System.String ToString()
/// </summary>
protected RMethod r_MToString;
public virtual RMethod RMToString
{
get
{
if(r_MToString == null)
{
r_MToString = new(this, &#34;ToString&#34;, 0);
}
return r_MToString;
}
}
public RATest() : base(&#34;Hvak.Editor.Refleaction.ATest&#34;)
{
}
public RATest(System.Object instance) : base(&#34;Hvak.Editor.Refleaction.ATest&#34;)
{
SetInstance(instance);
}
public RATest(RMember belongMember, string name, int genericCount = -1, params Type[] types) : base(belongMember, name, genericCount, types)
{
}
public RATest(Type belongType, string name, int genericCount = -1, params Type[] types) : base(belongType, name, genericCount, types)
{
}
public virtual void Log()
{
var ___genericsType = new Type[] {};
var ___parameters = new object[]{};
var ___result = RMLog.Invoke(___genericsType, ___parameters);
}
public virtual System.Boolean Equals(System.Object @obj)
{
var ___genericsType = new Type[] {};
var ___parameters = new object[]{@obj};
var ___result = RMEquals_Object.Invoke(___genericsType, ___parameters);
return (System.Boolean)___result;
}
public virtual void Finalize()
{
var ___genericsType = new Type[] {};
var ___parameters = new object[]{};
var ___result = RMFinalize.Invoke(___genericsType, ___parameters);
}
public virtual System.Int32 GetHashCode()
{
var ___genericsType = new Type[] {};
var ___parameters = new object[]{};
var ___result = RMGetHashCode.Invoke(___genericsType, ___parameters);
return (System.Int32)___result;
}
public virtual System.Type GetType()
{
var ___genericsType = new Type[] {};
var ___parameters = new object[]{};
var ___result = RMGetType.Invoke(___genericsType, ___parameters);
return (System.Type)___result;
}
public virtual System.Object MemberwiseClone()
{
var ___genericsType = new Type[] {};
var ___parameters = new object[]{};
var ___result = RMMemberwiseClone.Invoke(___genericsType, ___parameters);
return (System.Object)___result;
}
public virtual System.String ToString()
{
var ___genericsType = new Type[] {};
var ___parameters = new object[]{};
var ___result = RMToString.Invoke(___genericsType, ___parameters);
return (System.String)___result;
}
}
}
页:
[1]