在AX 2009中使用嵌套的.NET枚举
我有一个类为TestService.TestServiceWebClient的DLL在AX 2009中使用嵌套的.NET枚举,.net,axapta,dynamics-ax-2009,.net,Axapta,Dynamics Ax 2009,我有一个类为TestService.TestServiceWebClient的DLL 它的构造函数有两个参数,一个是类型string,另一个是类型TestService.testservicewclient.MODE,它是一个枚举 在C#中,我很容易将其与下面的代码结合使用 string secret; secret = "asdfasdf"; TestService.TestServiceWebClient test = new TestService.TestServiceWebClien
它的构造函数有两个参数,一个是类型string
,另一个是类型TestService.testservicewclient.MODE
,它是一个枚举
在C#中,我很容易将其与下面的代码结合使用
string secret;
secret = "asdfasdf";
TestService.TestServiceWebClient test = new TestService.TestServiceWebClient(secret, TestService.TestServiceWebClient.MODE.TEST);
test.setGroupForCustomer("test@foo.com", "12345", "TestService.TestServiceWebClient.CUST_GROUPS.MILITARY);
在AX中,我遇到了一些麻烦。我知道DLL被正确引用,因为我在创建类的实例时看到了参数,但是我得到了如下错误
TestService类不包含此函数
当我使用关键字new
时:
TestService test;
;
test = new TestService.TestServiceWebClient(secret, "");
我也试过
TestService.TestServiceWebClient test;
;
test = new TestService.TestServiceWebClient(secret, "");
第二个参数不是字符串
,因此当我尝试添加枚举时,我得到
枚举不存在
我是否需要在AX中创建一个从DLL映射到枚举的枚举
C#代码
在DynamicsAX 2009中使用嵌套的.NET枚举有点糟糕-如果您可以控制程序集的源代码,并且可以在不影响其他用户的情况下对其进行更改,我建议将其上移到与类相同的级别。另一种方法是编写代理方法或类,以便为您进行对象构造
也就是说,有一种方法可以使用反射与他们一起工作。
下面的内容应该可以让您了解如何基于框架enumSystem.Environment.SpecialFolder
来实现这一点,该框架也是嵌套的
static void NestedEnumDemo1(Args _args)
{
System.Type classType;
System.Type enumType;
System.Enum enumValue;
System.Reflection.MethodInfo methodInfo;
System.Object[] methodArgs;
System.String specialFolderPath;
str msg;
;
classType = System.Type::GetType('System.Environment');
enumType = System.Type::GetType('System.Environment+SpecialFolder');
enumValue = System.Enum::Parse(enumType, 'System');
methodInfo = classType.GetMethod('GetFolderPath');
methodArgs = new System.Object[1]();
methodArgs.SetValue(enumValue, 0);
specialFolderPath = methodInfo.Invoke(null, methodArgs);
msg = specialFolderPath;
info(msg);
}
因此,对于您的情况,它可能看起来类似于此,尽管我无法测试它,因为我没有您的组件,但它至少应该为您指出正确的方向:
static void NestedEnumDemo2(Args _args)
{
System.Type classType;
System.Type enumType;
System.Enum enumValue;
System.Type[] constructorParmTypes;
System.Reflection.ConstructorInfo constructorInfo;
System.String secret;
System.Object[] constructorArgs;
System.Object object;
TestService.TestServiceWebClient test;
;
classType = System.Type::GetType('TestService.TestServiceWebClient');
enumType = System.Type::GetType('TestService.TestServiceWebClient+MODE');
enumValue = System.Enum::Parse(enumType, 'TEST');
constructorParmTypes = new System.Type[2]();
constructorParmTypes.SetValue(System.Type::GetType('System.String'), 0);
constructorParmTypes.SetValue(enumType, 1);
constructorInfo = classType.GetConstructor(constructorParmTypes);
secret = 'xxxxx';
constructorArgs = new System.Object[2]();
constructorArgs.SetValue(secret, 0);
constructorArgs.SetValue(enumValue, 1);
object = constructorInfo.Invoke(constructorArgs);
test = object;
// ...
}
希望能有所帮助谢谢。我们的“VAR”有一些类似的代码。我来试试你的。你有机会看看吗?抱歉耽搁了。。。我被拉上了其他项目。今天我又回来了。我用C代码更新了我的问题。当我使用这个的时候;enumType=System.Type::GetType('ProductService.TestWebClient+MODE');它打印“Class”。它不应该是“枚举”吗?另外,当我添加时,enumValue=System.Enum::Parse(enumType,'TEST');我得到“ClrObject静态方法调用错误”。我没有太多成功。。。也许我应该把枚举改成字符串,这样我就可以从AX传递它们了@达斯霍利克
static void NestedEnumDemo1(Args _args)
{
System.Type classType;
System.Type enumType;
System.Enum enumValue;
System.Reflection.MethodInfo methodInfo;
System.Object[] methodArgs;
System.String specialFolderPath;
str msg;
;
classType = System.Type::GetType('System.Environment');
enumType = System.Type::GetType('System.Environment+SpecialFolder');
enumValue = System.Enum::Parse(enumType, 'System');
methodInfo = classType.GetMethod('GetFolderPath');
methodArgs = new System.Object[1]();
methodArgs.SetValue(enumValue, 0);
specialFolderPath = methodInfo.Invoke(null, methodArgs);
msg = specialFolderPath;
info(msg);
}
static void NestedEnumDemo2(Args _args)
{
System.Type classType;
System.Type enumType;
System.Enum enumValue;
System.Type[] constructorParmTypes;
System.Reflection.ConstructorInfo constructorInfo;
System.String secret;
System.Object[] constructorArgs;
System.Object object;
TestService.TestServiceWebClient test;
;
classType = System.Type::GetType('TestService.TestServiceWebClient');
enumType = System.Type::GetType('TestService.TestServiceWebClient+MODE');
enumValue = System.Enum::Parse(enumType, 'TEST');
constructorParmTypes = new System.Type[2]();
constructorParmTypes.SetValue(System.Type::GetType('System.String'), 0);
constructorParmTypes.SetValue(enumType, 1);
constructorInfo = classType.GetConstructor(constructorParmTypes);
secret = 'xxxxx';
constructorArgs = new System.Object[2]();
constructorArgs.SetValue(secret, 0);
constructorArgs.SetValue(enumValue, 1);
object = constructorInfo.Invoke(constructorArgs);
test = object;
// ...
}