Re: NETLink - CREATING a new class?
- To: mathgroup at smc.vnet.net
- Subject: [mg45126] Re: [mg45085] NETLink - CREATING a new class?
- From: "ToolmakerSteve" <ToolmakerSteve at shawstudio.com>
- Date: Wed, 17 Dec 2003 07:54:41 -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
Notebook[{
Cell[CellGroupData[{
Cell[BoxData[
\(\((FileType /.
FileInformation[
ToFileName[
Environment["\<WINDIR\>"], "\<assembly\>"]])\)\)], \
"Input"],
Cell[BoxData[
\(Directory\)], "Output"]
}, Open ]],
Cell[CellGroupData[{
Cell[BoxData[{
\(Needs["\<NETLink`\>"]\), "\[IndentingNewLine]",
\(InstallNET[]\[IndentingNewLine]\[IndentingNewLine] (*\ "\<net1 \
| net2\>"\ *) \), "\[IndentingNewLine]",
\(\(NetBitOr[\ net1_, net2_\ ] := \[IndentingNewLine]BitOr[\
NETObjectToExpression[\ net1\ ],
NETObjectToExpression[\
net2\ ]\ ];\)\[IndentingNewLine]\[IndentingNewLine] (*\
TBD : \ haven'
t\ found\ a\ way\ to\ do\ this\ yet . \
TBD\ *) \), "\[IndentingNewLine]",
\(\(NetMergeAttributes[\ net1_, net2_\ ] :=
net1\ (*\ \(|\)\(\ \)\(net2\)\ *) ;\)\[IndentingNewLine]\), \
"\[IndentingNewLine]",
\(NETBlock[\[IndentingNewLine]Module[\ {assemblyName, className,
netSpace1, spaceTypes1, netType1, main, snippet1,
statement1, compUnit1, compParams, refAssemblies, csharp,
cscompiler,
result}, \[IndentingNewLine]\[IndentingNewLine]assemblyName \
= "\<MyNamespace\>"; \[IndentingNewLine]className = \
"\<HelloWorldMsgApp\>"; \[IndentingNewLine]outputPath = \
"\<c:\\Temp\\HelloWorldMsg.exe\>"; \[IndentingNewLine]\
\[IndentingNewLine] (*\
Load\ type \((s)\)\ to\ call\ static\ \(\(members\)\(.\)\)\
*) \[IndentingNewLine]\((LoadNETType[\ \
"\<System.CodeDom.MemberAttributes\>"\ ])\) //
Print; \[IndentingNewLine]\[IndentingNewLine]\((netSpace1 =
NETNew[\ "\<System.CodeDom.CodeNamespace\>"\ ])\) //
Print; \[IndentingNewLine]netSpace1@Name =
assemblyName; \[IndentingNewLine]netSpace1@Name //
Print; \[IndentingNewLine]\((spaceTypes1 =
netSpace1@Types)\) //
Print; \[IndentingNewLine]spaceTypes1@Count //
Print; \[IndentingNewLine]\[IndentingNewLine]netType1 =
NETNew[\ "\<System.CodeDom.CodeTypeDeclaration\>"\ ]; \
\[IndentingNewLine]netType1@Name =
className; \[IndentingNewLine]netType1@IsClass =
True; \[IndentingNewLine]\((MemberAttributes`Public)\) //
Print; \[IndentingNewLine]netType1@Attributes =
MemberAttributes`Public; \[IndentingNewLine]spaceTypes1@
Add[\ netType1\ ]; \[IndentingNewLine]spaceTypes1@Count //
Print; \[IndentingNewLine]spaceTypes1@Item[\ 0\ ] //
Print; \[IndentingNewLine]\((spaceTypes1@Item[\ 0\ ])\)@
Name //
Print; \[IndentingNewLine]\[IndentingNewLine]\((main =
NETNew[\ "\<System.CodeDom.CodeEntryPointMethod\>"\ \
])\) // Print; \[IndentingNewLine]main@
Name = "\<Main\>"; \[IndentingNewLine]\((NetBitOr[\
MemberAttributes`Public, MemberAttributes`Static\ ])\) //
Print; \[IndentingNewLine]main@Attributes =
NetMergeAttributes[\ MemberAttributes`Public,
MemberAttributes`Static\ ]; \[IndentingNewLine]\((main@
Attributes)\) //
Print; \[IndentingNewLine]\((netType1@Members)\)@
Add[\ main\ ]; \[IndentingNewLine]\((\((netType1@Members)\)@
Count)\) //
Print; \[IndentingNewLine]\[IndentingNewLine]snippet1 =
NETNew[\ "\<System.CodeDom.CodeSnippetExpression\>", \
\[IndentingNewLine]"\<MessageBox.Show( \"Hello, .NET World!\" )\>"\ \
]; \[IndentingNewLine]statement1 =
NETNew[\ "\<System.CodeDom.CodeExpressionStatement\>",
snippet1\ ]; \[IndentingNewLine]statement1 //
Print; \[IndentingNewLine]\((statement1@Expression)\)@
UserData //
Print; \[IndentingNewLine]\((main@Statements)\)@
Add[\ statement1\ ]; \[IndentingNewLine]\((\((main@
Statements)\)@Count)\) //
Print; \[IndentingNewLine]\[IndentingNewLine]compUnit1 =
NETNew[\ \ "\<System.CodeDom.CodeCompileUnit\>"\ ]; \
\[IndentingNewLine]\((compUnit1@Namespaces)\)@
Add[\ netSpace1\ ]; \[IndentingNewLine]compParams =
NETNew[\ \ "\<System.CodeDom.Compiler.CompilerParameters\>"\
\ ]; \[IndentingNewLine]refAssemblies =
compParams@
ReferencedAssemblies; \[IndentingNewLine]refAssemblies@
Add[\ "\<mscorlib.dll\>"\ ]; \
\[IndentingNewLine]refAssemblies@
Add[\ "\<System.dll\>"\ ]; \[IndentingNewLine]refAssemblies@
Add[\ "\<System.Drawing.dll\>"\ ]; \
\[IndentingNewLine]refAssemblies@
Add[\ "\<System.Windows.Forms.dll\>"\ ]; \
\[IndentingNewLine]refAssemblies@Count //
Print; \[IndentingNewLine]compParams@GenerateInMemory =
False; \[IndentingNewLine]compParams@GenerateExecutable =
True; \[IndentingNewLine]compParams@MainClass =
assemblyName <> "\<.\>" <>
className; \[IndentingNewLine]\((compParams@MainClass)\) //
Print; \[IndentingNewLine]\((compParams@OutputAssembly =
outputPath)\) //
Print; \[IndentingNewLine]\[IndentingNewLine] (*\
Code\ snippets\ were\ given\ in\ c #, \
so\ use\ c #\ \(\(compiler\)\(.\)\)\ \
*) \[IndentingNewLine]csharp =
NETNew[\ \ "\<Microsoft.CSharp.CSharpCodeProvider\>"\ ]; \
\[IndentingNewLine]cscompiler =
csharp@CreateCompiler[]; \[IndentingNewLine]result =
cscompiler@
CompileAssemblyFromDom[\ compParams,
compUnit1\ ]; \[IndentingNewLine]result //
Print;\[IndentingNewLine]]\[IndentingNewLine]]\)}], \
"Input"],
Cell[BoxData[
\(LinkObject[
"C:\\Program Files\\Wolfram \
Research\\Mathematica\\5.0\\AddOns\\NETLink\\InstallableNET.exe", 2,
2]\)], "Output"],
Cell[BoxData[
\(NETType["System.CodeDom.MemberAttributes", 1]\)], "Print"],
Cell[BoxData[
InterpretationBox[\(\[LeftGuillemet]NETObject[
"System.CodeDom.CodeNamespace"] \[RightGuillemet]\),
NETLink`Objects`NETObject$788529153]], "Print"],
Cell[BoxData[
\("MyNamespace"\)], "Print"],
Cell[BoxData[
InterpretationBox[\(\[LeftGuillemet]NETObject[
"System.CodeDom.CodeTypeDeclarationCollection"] \
\[RightGuillemet]\),
NETLink`Objects`NETObject$771751937]], "Print"],
Cell[BoxData[
\(0\)], "Print"],
Cell[BoxData[
InterpretationBox[\(\[LeftGuillemet]NETObject[
"System.CodeDom.MemberAttributes"] \[RightGuillemet]\),
NETLink`Objects`NETObject$412316860417]], "Print"],
Cell[BoxData[
\(1\)], "Print"],
Cell[BoxData[
InterpretationBox[\(\[LeftGuillemet]NETObject[
"System.CodeDom.CodeTypeDeclaration"] \[RightGuillemet]\),
NETLink`Objects`NETObject$754974721]], "Print"],
Cell[BoxData[
\("HelloWorldMsgApp"\)], "Print"],
Cell[BoxData[
InterpretationBox[\(\[LeftGuillemet]NETObject[
"System.CodeDom.CodeEntryPointMethod"] \[RightGuillemet]\),
NETLink`Objects`NETObject$738197505]], "Print"],
Cell[BoxData[
\(24579\)], "Print"],
Cell[BoxData[
InterpretationBox[\(\[LeftGuillemet]NETObject[
"System.CodeDom.MemberAttributes"] \[RightGuillemet]\),
NETLink`Objects`NETObject$412316860417]], "Print"],
Cell[BoxData[
\(1\)], "Print"],
Cell[BoxData[
InterpretationBox[\(\[LeftGuillemet]NETObject[
"System.CodeDom.CodeExpressionStatement"] \[RightGuillemet]\
\),
NETLink`Objects`NETObject$687865857]], "Print"],
Cell[BoxData[
InterpretationBox[\(\[LeftGuillemet]NETObject[
"System.Collections.Specialized.ListDictionary"] \
\[RightGuillemet]\),
NETLink`Objects`NETObject$671088641]], "Print"],
Cell[BoxData[
\(1\)], "Print"],
Cell[BoxData[
\(4\)], "Print"],
Cell[BoxData[
\("MyNamespace.HelloWorldMsgApp"\)], "Print"],
Cell[BoxData[
\("c:\\Temp\\HelloWorldMsg.exe"\)], "Print"],
Cell[BoxData[
\(NET::"nomethod" \(\(:\)\(\ \)\)
"No public instance method named CompileAssemblyFromDom exists \
for the .NET type Microsoft.CSharp.CSharpCodeGenerator."\)], \
"Message"],
Cell[BoxData[
\($Failed\)], "Print"]
}, Open ]]
},
FrontEndVersion->"5.0 for Microsoft Windows",
ScreenRectangle->{{0, 1280}, {0, 971}},
WindowSize->{686, 781},
WindowMargins->{{Automatic, 6}, {Automatic, 4}}
]
- References:
- NETLink - CREATING a new class?
- From: ToolmakerSteve@shawstudio.com (ToolmakerSteve)
- NETLink - CREATING a new class?