dotNet + MXS


#481

EDIT. Got it working!


 local assembly = (dotnetclass "System.Reflection.Assembly").Load ((dotnetclass "System.IO.File").ReadAllBytes "c:\\myDll.dll")
 local type = assembly.GetType "myNameSpace.myClass"
 local classInstance = (dotNetClass "activator").CreateInstance type

Now i just need to figure out how to use it with dotNetControls.


#482

Looks like it works if i just create normal dotNetControl in rollout clause and then replace it with the classinstance. Something like this:

(
 	fn createUi =
 	(
 		rollout myRoll "myRoll"
 		(
 			dotNetControl myListView "myNameSpace.myListView" 
 		)
 		createDialog myRoll
 	)
 	
 	fn runScript =
 	(
 		local assembly = (dotnetclass "System.Reflection.Assembly").Load ((dotnetclass "System.IO.File").ReadAllBytes "myDll.dll")
 		local type = assembly.GetType "myNameSpace.myListView" 
 		myRoll.myListView = (dotNetClass "activator").CreateInstance type
 		createUi()
 	)
 )

But is this good way to do it?


#483

let’s start from the end of you post… the loading of DLL (assembly) as bytes array allows you to override the DLL because the DLL (as file) is not captured by MAX. That’s the goal. It makes debugging process much easier. My code above creates new assembly and you can use CreateInstance method to create a class from this assembly.
So it has to go as:


 my_assemby = (see code above)
 my_class = my_assembly.CreateInstance "MyClass"
 

after that you can use any method from the class…

you can create you own object from of your class (or any type defined in the class) but not a dotnetcontrol. Dotnetcontrol is not really .NET object, it makes sense in MAX only. Dotnetcontrol is MAX Script UI control wrapper and it uses .net object from first loaded assembly. So you can load your assembly using byte method and can create dotnetcontrol but only from assembly that was loaded first.


#484

myRoll.myListView = (dotNetClass "activator").CreateInstance type

does it work? if it works it’s a good way.


#485

Damn i thought it worked but it doesn’t, I get “Cannot directly assign rollout controls” error. Oh well, it’s still a step forward if we can use dynamic dotnetclasses and objects, thanks a lot denisT :slight_smile:


#486

And dot net controls. IS there any known problems with this approach?

I will start to try some things, and read further into the information presented on t’web but just in case I am on a massive wild goose chase, please feel free to give me any info to stop me in my tracks! :smiley:

Cheers,

cw


#487

is there any idea how to get
Enumerable Class in max 2010/64 ?


#488

I could only check 2009x64 and 2011x64, but the following “doesn’t crash”…

dotnet.loadassembly "System.Core.dll"
enum = dotnetclass "System.Linq.Enumerable"

r = enum.range 1 10
showproperties r
showmethods r

getProperty r "<>3__count"
getProperty r "<>3__start"

It generates:

dotNetObject:System.Linq.Enumerable+<RangeIterator>d__b8
.<>3__count : <System.Int32>
.<>3__start : <System.Int32>
.<i>5__b9 : <System.Int32>
.count : <System.Int32>
.start : <System.Int32>
true
.<System.Boolean>Equals <System.Object>obj
.[static]<System.Boolean>Equals <System.Object>objA <System.Object>objB
.<System.Int32>GetHashCode()
.<System.Type>GetType()
.[static]<System.Boolean>ReferenceEquals <System.Object>objA <System.Object>objB
.<System.String>ToString()
true
10
1


#489

thank you biddle.
I just don’t understand why I can load “System.Core.dll” as assembly, I can add it to CompilerParams.ReferencedAssemblies, but compiler can’t find it.


fn CreateTestAssembly =
(
	source = ""
	source += "using System;
"
	source += "using System.Linq;
"
	source += "public class testClass
"
	source += "{
"
	source += "}
"

	csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
	compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"

	compilerParams.ReferencedAssemblies.Add "System.dll"
    compilerParams.ReferencedAssemblies.Add "System.Core.dll"

	compilerParams.GenerateInMemory = true
	compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
	
	if (compilerResults.Errors.Count > 0 ) then
	(
		errs = stringstream ""
		for i = 0 to (compilerResults.Errors.Count-1) do
		(
			err = compilerResults.Errors.Item[i]
			format "Error:% Line:% Column:% %
" err.ErrorNumber err.Line err.Column err.ErrorText to:errs 
		)
		MessageBox (errs as string) title: "Errors encountered while compiling C# code"
		format "%
" errs
		undefined
	)
	else
	(
		compilerResults.CompiledAssembly.CreateInstance "testClass"
	)
)
global testClass = CreateTestAssembly()


#490

The stock mxsdotnet.dll in Max 2010 was compiled for .NET 2.0 and doesn’t know how to locate the 3.5 assembly.

I was able to run your code in Max2009x64 by explicitly telling it where to find a version of System.Core.dll on my system.

compilerParams.ReferencedAssemblies.Add "C:\Windows\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll"


#491

that’s right. i do the same. but… why the assembly can be loaded right without the fullpath? of course i don’t have any problem to use dll compiled in the vstudio. that means MAX fully supports .net 3.5. i’m really puzzled.


#492

Adding audio prompts is reasonably simple:

dotnet.loadassembly "System.Speech.dll"
myPrompt = dotnetobject "System.Speech.Synthesis.PromptBuilder"

myPrompt.AppendAudio @"c:\windows\media
otify.wav"
myPrompt.AppendText "Something important has just occurred in"
myPrompt.AppendTextWithHint "3DS" (dotnetclass "system.speech.synthesis.SayAs").SpellOut
myPrompt.AppendText "Max"

synth = dotnetobject "system.speech.synthesis.SpeechSynthesizer" 
synth.SpeakAsync myPrompt

keywords: speech prompt


#493

Hej guys I am having a bit of problem, I cant get the proper encoding/buffer param done for my streamwriter. I wonder maybe somebody could help me out with it.

strwriterclass = dotnetclass “system.io.streamwriter”
strwriter = dotnetobject strwriterclass “d:/finale.txt” “UTF-8” 100000

It doesnt accept it for some reason. I also tried to put the dontnet encoder class into the encoding but I couldnt figured it out.


#494

there is no constructor that you use. it might be:
dotnetobject strwriterclass “d:/finale.txt” false (dotnetclass “System.Text.Encoding”).UTF8 100000

check the list of constructors: http://msdn.microsoft.com/en-us/library/system.io.streamwriter.streamwriter.aspx


#495

I was referering to this one > http://msdn.microsoft.com/en-us/library/72d9f8d5.aspx and it is a dontnet 2.0 constructor. Well In max 2010 dotnet 2.0 isnt it? There is only 3 params.
Anyway, thank you very much, this will do well too!


#496

the constructor that you tried to use needs Stream, but you used string (path).


#497

3.5 actually I think :slight_smile:


#498

Does anyone know how to convert an IEnumerable to an array, and vice versa, using MaxScript?


#499

havn’t tested but this post seems to have the answer:
http://stackoverflow.com/questions/268671/best-way-to-convert-ilist-or-ienumerable-to-array
1st result after googling ‘ienumerable to array’ :slight_smile:

as for converting back, i’m not sure why you would need to?


#500

I know how to do it in C#, but I need to do it in Max using MaxScript. Just like one of the posters said, IEnumerable doesn’t have a .ToArray() extension. So it’s not as simple as that.