none
some problem for referenceassemblies ,CodeDom and csharp compiler RRS feed

  • Question

  • hello anyone!

    i have some code following below

                AssemblyName _aname = new AssemblyName("__Temps");
                _aname.Version = new Version("1.0.0.0");
    
                AssemblyBuilder _asmbuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(_aname, System.Reflection.Emit.AssemblyBuilderAccess.Run);
    
                System.CodeDom.CodeSnippetCompileUnit _unit = new CodeSnippetCompileUnit();
                Microsoft.CSharp.CSharpCodeProvider _cp = new Microsoft.CSharp.CSharpCodeProvider();
                System.CodeDom.Compiler.CompilerParameters _param = new System.CodeDom.Compiler.CompilerParameters();
                _param.ReferencedAssemblies.Add(@"MyPowerToies.dll");
                var _result = _cp.CompileAssemblyFromDom(_param, _unit);
    
                var _alist = _result.CompiledAssembly.GetReferencedAssemblies();
    
                foreach (var item in _alist)
                {
                    Console.WriteLine(item.FullName);
                }

    when CodeSnippetCompileUnit has nothing code, the "MyPowerToies.dll" reference assembly will be filter out in CompliedAssembly's ReferenceAssemblies.

    How can i prevent compiler from doing that?

    thanks andvance.


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    Tuesday, September 30, 2014 2:26 AM

Answers

All replies

  • It's by design, we cannot prevent this.
    Wednesday, October 1, 2014 2:15 AM
  • To further clarify, it is by design in the C# compiler and not specific to the CodeDOM.  The IDE works this exact same way.  It is an optimization.
    Friday, October 3, 2014 2:32 PM
    Moderator
  • thanks very much.

    can i disable the optimization?


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    Wednesday, October 8, 2014 1:02 AM
  • No you cannot disable it.  I'm curious as to why you would ever want to.  Why would you want to reference an assembly that you will never use (other than perhaps for dynamic loading, but then why not just statically load)?

    If you really want to make sure it is referenced all you need to do is use a type in the assembly in your code.  The compiler is smart enough to detect the usage and add a reference to the metadata but it isn't smart enough to know whether that code is actually ever used.  You could create an internal static class that has a static method that does nothing but create an instance of a type in the assembly and that would be sufficient.  Of course even though the reference is there, if you never call the method then the assembly will never be loaded at runtime.

    Wednesday, October 8, 2014 1:55 AM
    Moderator
  • HI.

    sorry ,i come back.

    now i explain why i want to refence an assembly that i never use.

    let i give a sample i has  meet with..

    I have a assembly A include Class A.Person difinition.

    yes.it is right the main Excutable Assembly don't reference the Assembly A.

    but there is a Assembly B reference Assembly A, 

    and now Main Executable Assembly load Assembly B from file dynamic and do somewhat Serialization operting through Assemlby B reflection by binanryFormater., in this time ,it works fine.

    but .when i call Deserializer from file after serialization , it would throw a exception if not reference the Assembly A in my MainExecutable Assembly that is never use the it explicitly.

    it is because ,I reference Assembly A in design time .but after complied ,the reference will remove for optimization.

    So....that is why i ask this question.

    ....

    by the way. 

    it seem as it is patched in vs 2015 .

    thanks 


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    Saturday, November 14, 2015 11:05 AM