首页 > 代码库 > [转]Dynamics AX and Generic collections of .Net

[转]Dynamics AX and Generic collections of .Net

转自:http://blogs.msdn.com/b/emeadaxsupport/archive/2009/04/23/dynamics-ax-and-generic-collections-of-net.aspx

 

EMEADAXSupport 
23 Apr 2009 9:54 AM 
  • 3

I‘m using Dynamics AX 2009 and want to instantiate the .NET class "System.Collections.Generic.List" in X++.

Intellisense expands the class to "System.Collections.Generic.List`1" however the code cannot be compiled as the class as well as all other classes in the "System.Collections.Generic" namespace are not accepted.

Actually this type as well as every type defined in the namespace System.Collections.Generic are no regular types (are classes) but they are so called "Generics". You can see this for example when you are using a Generic in C#, where you have to specify the type of the Generic at declaration / definition (here the for example for string):

System.Collections.Generic.List<string> list;

More information about .NET Generics can be found here:
Generics (C# Programming Guide)

For Dynamics AX 4.0 Generics are not supported by the X++ language. This is outlined in the Microsoft Press book "Inside Dynamics AX" on page 111: The following feature of CLR cannot be used with X++:

  • Public fields (These can be accessed by using CLR reflection classes.)
  • Events and delegates
  • The ref parameter modifier
  • The out parameter modifier
  • Generics
  • Inner classes
  • The Container composite type
  • The Array composite type
  • Namespace declarations

Besides the "swapping" of the Generic collection into an own Assembly that is referenced back in to Dynamics AX, the only recommendation is to use the Non-Generic counterpart of the collection (see table below).

Generic Non-Generic
System.Collections.Generic System.Collections
Collection<T> CollectionBase
Comparer<T> Comparer
Dictionary<K, V> Hashtable
List<T> ArrayList
Queue<T> Queue
SortedDictionary<K, V> SortedList
Stack<T> Stack
ReadOnlyCollection<T> ReadOnlyCollectionBase

So in our case the Non-Generic counterpart of "System.Collection.Generic.List<T>" would be "System.Collections.ArrayList".

--author:Alexander Lachner
--editor:Alexander Lachner
--date:23/04/2009