[Closed] Generic methods from .Net – How to fiddle with them in MXS
Hei folks,
I came to the point where I had to deal with my own additional dictionary functions at work, so I decided to create a static class in a DotNet DLL. One of the methods was used to create Generic dictionary with a generic list and a generic key so I could reuse it wherever possible. Something like this:
public static Dictionary<T, List<U>> createDictionaryWithList<T, U>()
{
return new Dictionary<T,List<U>>();
}
This is pretty handy in C# but I don’t need it there. I need it in MaxScript. When I try to execute this somehow MXS does not like it so the question is (before I rewrite everything to deal with the object-class) can you execute generic functions in MXS and if yes, how do you do that?
Best regards
Mirko
You can use the following methods to create generic instances in maxscript. The usage is self-explanatory.
fn getGenericType strType genericParams =
(
if classof genericParams != array do genericParams = #(genericParams)
local genTypeStr = strType + "`" + (genericParams.count as string)
local genType = (dotnetClass "System.Type").GetType genTypeStr
local paramTypes = dotnetObject "System.Type[]" (genericParams.count)
for p = 1 to genericParams.count do
(
if (classof genericParams[p] == string) do
(
genericParams[p] = (dotnetClass "System.Type").GetType genericParams[p]
)
paramTypes.set (p - 1) genericParams[p]
)
return genType.MakeGenericType paramTypes
)
fn createGenericInstance strType genericParams =
(
return (dotnetClass "System.Activator").CreateInstance (getGenericType strType genericParams)
)
usage:
listInt = createGenericInstance "System.Collections.Generic.List" "System.Int32"
listInt.add 123
dictionaryIntStr = createGenericInstance "System.Collections.Generic.Dictionary" #("System.Int32", "System.String")
dictionaryIntStr.add 456 "foo"
or your specific example
dictionaryIntListStr = createGenericInstance "System.Collections.Generic.Dictionary" \
#("System.Int32", \
(getGenericType "System.Collections.Generic.List" "System.String"))
Hey Io,
wow I did not think that it would really be possible. Very nice. Thanks. I’m going to add it to my code.
Best regards
kogen