none
CompileAssemblyFromFile method using CSharpCodeProvider with parameter "CompilerVersion" set to 4.0 with reference to mscorlib.dll will use mscorlib.dll of .NET 4.5 RRS feed

  • Question

  • Hi all,

     I've got a compiler application that creates proxies on the run then compiles it to generate a .NET assembly.

     On the generated proxy, I've got a line of code using WindowsIdentity, that according to this article New identity management in net 45 by , changed its base type from System.Object (v4.0) to System.Security.Claims.ClaimsIdentity (v4.5).

     We are running this proxy generator in a machine with Visual Studio 2012 and .NET 4.5 installed.

     On the compilation code, I'm forcing the Compiler version to v4.0, but when we run the proxy it keeps telling me that it cannot find the type System.Security.Claims.ClaimsIdentity (that only exist in v4.5 of .NET).

     When I use a reference to the mscorlib.dll must I specify the path to the place where the .NET assemblies of 4.0 resides (C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0)? Isn't it enough to indicate the version of .NET I want to use using the CompilerVersion parameter?

    Thanks for all the help,

    Luís Pinho


    Luis Pinho

    • Moved by Bob Shen Monday, April 8, 2013 8:19 AM
    Friday, April 5, 2013 1:46 PM

Answers

All replies

  • Hi LPinho,

    I would like to redirect you to appropriate forum for better responses.  


    Bob Shen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 8, 2013 8:19 AM
  • Ok, thanks for forwarding this post and sorry for extra work.


    Luis Pinho

    Monday, April 8, 2013 8:39 AM
  • Hi Luis,

    I tried this code:

                var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } });
                var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "foo.exe", true);
                parameters.GenerateExecutable = true;
                CompilerResults results = csc.CompileAssemblyFromSource(parameters,
                @"class Program {
                  public static void Main(string[] args) {
                    System.Security.Claims.ClaimsIdentity claims = new System.Security.Claims.ClaimsIdentity();
                  }
                }");
                results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));

    It runs fine, without any error on Win8.

    Would you like to check this?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 8, 2013 8:53 AM
    Moderator
  • Hi Mike,

      1st, thanks for the reply but I may have not explain myself correctly.

      The code that I've got is for running in environments that don't have .NET 4.5 installed, so, they don't have mscorlib.dll with the namespace System.Security.Claims.

     Our "build machine" was updated and Visual Studio 2012 was installed, with it came .NET 4.5. and we started getting an error in machines without .NET 4.5 installed (client machines with .NET 4.0 and Visual Studio 2010).

     So I tried to fix this by setting the CompilerVersion to v4.0, I though that forcing the CompilerVersion to v4.0 the compiler would use the v4.0 assemblies, but it seems that it doesn't, its compiling with version 4.5 (proved by your example - you are using a namespace that only exists in version 4.5 even using the CompilerVersion set to 4.0).

     For the moment, and to unblock this, I'm forcing the path of the referenced assemblies to:

    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v<.net version>

     But I don't like this solution because Microsoft can change this path to anything else in the future.

     Thanks again,

    Luís Pinho


    Luis Pinho

    Monday, April 8, 2013 9:10 AM
  • Hi Luis,

    >>I though that forcing the CompilerVersion to v4.0 the compiler would use the v4.0 assemblies, but it seems that it doesn't, 

    I suggest you post this issue here: https://connect.microsoft.com/visualstudio 

    Microsoft will check this issue. When you finished, post the link back, please.

    Thank you.


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 9, 2013 2:16 PM
    Moderator
  • Hi Mike,

     here it is, the Microsoft Connect link

    Regards,

    Luís Miguel Pinho


    Luis Pinho

    Tuesday, April 9, 2013 2:46 PM
  • Hi Luis,

    Thanks.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 10, 2013 4:41 PM
    Moderator