MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: NETLink - CREATING a new class?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg45125] Re: [mg45085] NETLink - CREATING a new class?
  • From: "ToolmakerSteve" <ToolmakerSteve at shawstudio.com>
  • Date: Wed, 17 Dec 2003 07:54:40 -0500 (EST)
  • References: <200312161121.GAA24769@smc.vnet.net> <4546.129.27.203.80.1071594228.bloek@pwebmail.utanet.at>
  • Sender: owner-wri-mathgroup at wolfram.com

Here is my first attempt at "Hello, .NET World" - a Microsoft .NET executable
created from Mathematica,
using NET/CodeDOM.

Based on a C# example that IS working on my PC.

*** Doesn't work yet. ***

There are two known problems:

1. I haven't succeeded in declaring Main() to be both "public and static" --
necessary to run it.
Because:
I haven't found a way to merge two MemberAttributes, and get the correct NET
type back.

2. I haven't compiled it, because I get error:
"NET::nomethod: No public instance method named CompileAssemblyFromDom exists
for the .NET type Microsoft.CSharp.CSharpCodeGenerator."

NOTE: my NEXT message will contain a "Notebook Expression" version -
save as a ".m" file, and open in Mathematica, to get properly formatted input,
as well as my output.

----------------------------------------------------
In[154]:=
Needs["NETLink`"]
InstallNET[]

(* "net1 | net2" *)
NetBitOr[ net1_, net2_ ] :=
    BitOr[ NETObjectToExpression[ net1 ], NETObjectToExpression[ net2 ] ];

(* TBD : haven't found a way to do this yet. TBD *)
NetMergeAttributes[ net1_, net2_ ] := net1 (* | net2 *);

NETBlock[
  Module[ {assemblyName, className, netSpace1, spaceTypes1, netType1, main, \
snippet1, statement1, compUnit1, compParams, refAssemblies, csharp,
 cscompiler, result},

    assemblyName = "MyNamespace";
    className = "HelloWorldMsgApp";
    outputPath = "c:\\Temp\\HelloWorldMsg.exe";

    (* Load type(s) to call static members.*)
    (LoadNETType[ "System.CodeDom.MemberAttributes" ]) // Print;

    (netSpace1 = NETNew[ "System.CodeDom.CodeNamespace" ]) // Print;
    netSpace1@Name = assemblyName;
    netSpace1@Name // Print;
    (spaceTypes1 = netSpace1@Types) // Print;
    spaceTypes1@Count // Print;

    netType1 = NETNew[ "System.CodeDom.CodeTypeDeclaration" ];
    netType1@Name = className;
    netType1@IsClass = True;
    (MemberAttributes`Public) // Print;
    netType1@Attributes = MemberAttributes`Public;
    spaceTypes1@Add[ netType1 ];
    spaceTypes1@Count // Print;
    spaceTypes1@Item[ 0 ] // Print;
    (spaceTypes1@Item[ 0 ])@Name // Print;

    (main = NETNew[ "System.CodeDom.CodeEntryPointMethod" ]) // Print;
    main@Name = "Main";
    (NetBitOr[ MemberAttributes`Public, MemberAttributes`Static ]) // Print;
    main@Attributes = NetMergeAttributes[ MemberAttributes`Public,
      MemberAttributes`Static ];
    (main@Attributes) // Print;
    (netType1@Members)@Add[ main ];
    ((netType1@Members)@Count) // Print;

    snippet1 = NETNew[ "System.CodeDom.CodeSnippetExpression",
        "MessageBox.Show( \"Hello, .NET World!\" )" ];
    statement1 = NETNew[ "System.CodeDom.CodeExpressionStatement",
    snippet1 ];
    statement1 // Print;
    (statement1@Expression)@UserData // Print;
    (main@Statements)@Add[ statement1 ];
    ((main@Statements)@Count) // Print;

    compUnit1 = NETNew[  "System.CodeDom.CodeCompileUnit" ];
    (compUnit1@Namespaces)@Add[ netSpace1 ];
    compParams = NETNew[  "System.CodeDom.Compiler.CompilerParameters" ];
    refAssemblies = compParams@ReferencedAssemblies;
    refAssemblies@Add[ "mscorlib.dll" ];
    refAssemblies@Add[ "System.dll" ];
    refAssemblies@Add[ "System.Drawing.dll" ];
    refAssemblies@Add[ "System.Windows.Forms.dll" ];
    refAssemblies@Count // Print;
    compParams@GenerateInMemory = False;
    compParams@GenerateExecutable = True;
    compParams@MainClass = assemblyName <> "." <> className;
    (compParams@MainClass) // Print;
    (compParams@OutputAssembly = outputPath) // Print;

    (* Code snippets were given in c#, so use c# compiler. *)
    csharp = NETNew[  "Microsoft.CSharp.CSharpCodeProvider" ];
    cscompiler = csharp@CreateCompiler[];
    result = cscompiler@CompileAssemblyFromDom[ compParams, compUnit1 ];
    result // Print;
    ]
  ]



  • Prev by Date: Re: NETLink - CREATING a new class?
  • Next by Date: Re: NETLink - CREATING a new class?
  • Previous by thread: Re: NETLink - CREATING a new class?
  • Next by thread: Re: NETLink - CREATING a new class?