C# and -Path

This topic contains 4 replies, has 2 voices, and was last updated by  Richard 2 weeks, 6 days ago.

  • Author
    Posts
  • #80395

    Richard
    Participant

    I have an issue when trying to load an assembly -Path is coming back as Null. Not sure why but here is my code snippet

    Add-Type -Path "C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.XML.dll"
    $assemblies=@(
    "C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.XML.dll"
    )
    Add-Type $code = @'
    using System;
    using System.Xml;
    using system.Text;
    
    public class testItem
    {
         public static string searchMachine()
          {
            return "TEST";
          }
    '@
    Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $code -Language CSharpVersion3
    [testItem]::searchMachine()
    
  • #80402

    Rick
    Participant

    try this:

    $Source = @"
    using System;
    using System.Xml;
        public class PS
        {
          public static string searchMachine()
          {
            return "TEST";
          }
        }
    "@
    
    $referencingassemblies = ("C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.XML.dll")
    Add-Type  -TypeDefinition $Source -ReferencedAssemblies $referencingassemblies -Language CSharp
    [PS]::searchMachine()
    
  • #80611

    Richard
    Participant

    Tried that Rick. Did not work. keeps saying same error Path is null.

  • #80615

    Richard
    Participant

    I did a test-path and it came back true. Not sure why its still erroring

  • #80636

    Richard
    Participant

    GOT IT....noticed I had ADD TYPE for the code part. So instead of ADD-TYPE $code = @" I only need $code = @"

You must be logged in to reply to this topic.