c# - Doesn't load assembly in powershell -
i try execute c# in powershell. wrote script, doesn't work. script contain in c# hello simple class test. in future need work xml. , that's why add system.xml.
source = @" using system; using system.io; using system.xml; using system.xml.serialization; namespace convertertrx { public class ps { public static string convert() { xmlserializer xmlser = new xmlserializer(typeof(string)); return "convert1"; } public static string convert1(string path) { return "convert2="+path+"/123"; } } } "@ if (-not ([system.management.automation.pstypename]'ps').type) { add-type -assemblyname microsoft.csharp add-type -assemblyname system add-type -assemblyname system.core add-type -assemblyname system.data add-type -assemblyname system.data.datasetextensions add-type -assemblyname system.xml add-type -assemblyname system.xml.linq add-type -typedefinition $source -language csharp } "call function" [convertertrx.ps]::convert() this script throw exception:
add-type : *\temp\zktiv5n4.0.cs(3) : type or namespace name 'xml' not exist in namespace 'system' (are missing assembly reference?) *\temp\zktiv5n4.0.cs(2) : using system.io; *\temp\zktiv5n4.0.cs(3) : >>> using system.xml; *\temp\zktiv5n4.0.cs(4) : using system.xml.serialization;
so, how it's fix?
you need add -referencedassemblies parameter add-type cmdlet call, otherwise you'll not namespaces.
$referencingassemblies = ("c:\windows\microsoft.net\framework\v4.0.30319\system.xml.dll") add-type -typedefinition $source -referencedassemblies $referencingassemblies -language csharp
Comments
Post a Comment