Click here to Skip to main content
15,886,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
CSS
Hi , I'm trying to execute C# code in Power Shell 2.0 .

My problem is for the references  at the beginning of the code:


$code = @"

using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Xml;<code></code>
using System.Xml.Linq;

   public class Program
    {
        static void Main()
        {  /* the code wich works*/


        }


    }


"@
Add-Type  -TypeDefinition $code -Language "CSharpVersion3"

Powershell gives me an error saying that " The type or namespace Linq doesn't exist" , and the same for Xml. I have Visual studio express 2013 .

How can add or import correctly those references? or what is my error?
Posted
Updated 16-Apr-14 21:43pm
v5

1 solution

You're not providing a whole lot of information. What errors are you seeing?
Anywho, try this for your Add-Type line and see how far that takes ya:

C#
Add-Type -TypeDefinition $source -Language CSharp -ReferencedAssemblies System.Xml, System.Xml.Linq
 
Share this answer
 
Comments
Kinna-10626331 21-Mar-14 8:07am    
I just added more info , I tried with your but the errors still the same.
notjustme 21-Mar-14 9:01am    
What PowerShell version are you using and as what user are you trying to run this? As long as the referenced assemblies are in the GAC this should be possible.

Do you get any useful information if you run the following line in PowerShell;

[System.Reflection.Assembly]::LoadWithPartialName("System.Collections")
Kinna-10626331 21-Mar-14 9:16am    
Its 2.0, with the user administrator and when I run your line nothing happens.
notjustme 21-Mar-14 9:34am    
If you find the full path for the dlls you should be able to load them.
notjustme 17-Apr-14 10:24am    
Editing your original question and not adding any more information won't really help you.

By finding the exact path to the actual DLLs I can get your code to load;

Add-Type -TypeDefinition $source -Language CSharp -ReferencedAssemblies "C:\Here\Are\My\dlls\System.Xml.dll", "C:\Here\Are\My\dlls\System.Linq.dll" etc, etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900