[Closed] dotNet type "List"
Hi!
I found an open source delaunay triangulator in c# and try to access a class that needs a "List" as a parameter. If I try to send an array or other parameters ist says: "Runtime error: No method found which matched argument list" which is logical because it needs a "List". How do I get this sort of thing into that function?
Something like
type = dotnetClass "System.Collections.Generic.List"
L = dotnet.ValueToDotNetObject #(p1, p2, p3) type
did not work. It returns “undefined”…
It would be great to get help! ;)
Best regards,
Fabian
Hmm, i think you have to specify the type-parameter of List<T> somehow where T is the type of the List.
Not sure how this works in maxscript.
Thanks for your fast reply!
List<Point> triangulationPoints
This is what the function needs as a parameter in c#… :shrug:
<Point> is a class also defined in the code. Can’t “List” be loaded without that?
the syntax in maxscript for creating a generic List is a bit weird but anyway this is how you do it:
-- create a 32 bit integer List
dotnetobject "System.Collections.Generic.List`1[System.Int32]"
-- create a 32 bit float List
dotnetobject "System.Collections.Generic.List`1[System.Single]"
-- create a 64 bit double List
dotnetobject "System.Collections.Generic.List`1[System.Double]"
EDIT: so for your Point class it would be something like:
dotnetobject "System.Collections.Generic.List`1[SomeNamespace.Point]"
My answer was a little bit too fast (…you still rock! ;))
type = dotnetObject "System.Collections.Generic.List`1[ceometric.DelaunayTriangulator.Point]"
returns
Cannot resolve type: System.Collections.Generic.List`1[ceometric.DelaunayTriangulator.Point]
EDIT:
I found an interesting thread and thought I post it, even if it did not fix my problem.
http://tech-artists.org/forum/showthread.php?t=638
without having the assembly myself I can only guess that you are not loading the dll before trying to construct the List. If all else fails you could write a class with a method that returns the List that you need in few lines and compile it dynamically in your script to save on external dependencies
I loaded the DLL before, of couse.
Thanks, that would be possible, I think.
We now rewrote the c# class to get rid of the list problem, which is good enough for the moment.
Anyway, thank you!
Although it sounds like you’ve already worked around the problem, one sure-fire way to instantiate oddball types is to define them dynamically and then use a System.Activator to create an instance.
(
-- Load in the assembly that contains the types we need
dotnet.LoadAssembly "ceometric.DelaunayTriangulator.dll"
-- Dynamically create the specific type we want
genericType = dotnet.GetType "System.Collections.Generic.List`1"
innerType = dotnet.GetType "ceometric.DelaunayTriangulator.Point"
specificType = genericType.MakeGenericType #(innerType)
-- Use the system.activator to create an instance of our type
points = (dotnetclass "System.Activator").CreateInstance specificType
-- Now we can test out the new list of points...
-- Create a set of points to triangulate
for i = 1 to 10 do
(
x = random 0.0 10.0
y = random 0.0 10.0
points.Add (dotnetObject "ceometric.DelaunayTriangulator.Point" x y 0)
)
-- Triangulate them...
triangulator = dotnetobject "ceometric.DelaunayTriangulator.DelaunayTriangulation2d"
triangles = triangulator.triangulate points
-- ...and display the triangles using a splineshape
ss = SplineShape()
iter = triangles.GetEnumerator()
while iter.MoveNext() do
(
p0 = [iter.current.Vertex1.X, iter.current.Vertex1.Y, iter.current.Vertex1.Z]
p1 = [iter.current.Vertex2.X, iter.current.Vertex2.Y, iter.current.Vertex2.Z]
p2 = [iter.current.Vertex3.X, iter.current.Vertex3.Y, iter.current.Vertex3.Z]
idx = addNewSpline ss
addKnot ss idx #corner #line p0
addKnot ss idx #corner #line p1
addKnot ss idx #corner #line p2
close ss idx
)
updateShape ss
)
Calling System.Activator.CreateInstance() on a type you’ve defined with a MakeGenericType call is magic.
.biddle
wow! That’s cool! :oD
A lot of code to do this, but… great! ^^
Thank you! I hope this also helps some other people to develop cool stuff!