D365 用反射来调用调用Class的私有变量和方法
using System.Reflection; [ExtensionOf(classStr(VyaReflectionClassBase))] final class VyaReflectionClass_Extension { /// <summary> /// 获取基类的私有变量 /// </summary> public void getPrivateVar() { int getPrivateVar; var bindFlags = BindingFlags::NonPublic | BindingFlags::Instance; var field = this.GetType().GetField(identifierStr(privateVar), bindFlags); if (field) { getPrivateVar = field.GetValue(this); } } /// <summary> /// 设置基类私有变量的值 /// </summary> /// <param name = "_newValue"></param> public void setPrivateVar(int _newValue) { var bindFlags = BindingFlags::NonPublic | BindingFlags::Instance; var field = this.GetType().GetField(identifierStr(privateVar), bindFlags); if (field) { field.SetValue(this, _newValue); } } /// <summary> /// 调用基类的私有方法 /// </summary> public void callPrivateMehtod() { //设置方法参数 System.Object[] parametersArray = new System.Object[2](); parametersArray.SetValue(5, 0); parametersArray.SetValue(24, 1); var bindFlags = BindingFlags::Instance | BindingFlags::NonPublic; var methodInfo = this.GetType().GetMethod(methodStr(VyaReflectionClassBase, privateMethod), bindFlags); if (methodInfo) { int returnValue; returnValue = methodInfo.Invoke(this, parametersArray); } } }
/// <summary> /// 定义基类 /// </summary> class VyaReflectionClassBase { //定义私有变量 private int privateVar; public void new() { privateVar = 5; } /// <summary> /// 定义私有方法 /// </summary> /// <param name = "_qty"></param> /// <param name = "_price"></param> /// <returns></returns> private CostAmount privateMethod(InventQty _qty, CostPrice _price) { CostAmount costAmount; ; return _qty * _price; } }
using System.Reflection;
[ExtensionOf(classStr(VyaReflectionClassBase))]
finalclassVyaReflectionClass_Extension
{
/// <summary>
/// 获取基类的私有变量
/// </summary>
publicvoid getPrivateVar()
{
int getPrivateVar;
var bindFlags = BindingFlags::NonPublic | BindingFlags::Instance;
var field = this.GetType().GetField(identifierStr(privateVar), bindFlags);
if (field)
{
getPrivateVar = field.GetValue(this);
}
}
/// <summary>
/// 设置基类私有变量的值
/// </summary>
/// <param name = "_newValue"></param>
publicvoid setPrivateVar(int _newValue)
{
var bindFlags = BindingFlags::NonPublic | BindingFlags::Instance;
var field = this.GetType().GetField(identifierStr(privateVar), bindFlags);
if (field)
{
field.SetValue(this, _newValue);
}
}
/// <summary>
/// 调用基类的私有方法
/// </summary>
publicvoid callPrivateMehtod()
{
//设置方法参数
System.Object[] parametersArray = new System.Object[2]();
parametersArray.SetValue(5, 0);
parametersArray.SetValue(24, 1);
var bindFlags = BindingFlags::Instance | BindingFlags::NonPublic;
var methodInfo = this.GetType().GetMethod(methodStr(VyaReflectionClassBase, privateMethod), bindFlags);
if (methodInfo)
{
int returnValue;
returnValue = methodInfo.Invoke(this, parametersArray);
}
}
}