title: SGEN, InternalsVisibleTo and assembly signing
body: <p>I'm trying to do something a bit unusual...</p>
  
  <p>I have this class Foo :</p>
  
  <pre><code>public class Foo
  {
      public Foo(string name)
      {
          this.Name = name;
      }
  
      internal Foo()
      {
      }
  
      public string Name { get; internal set; }
      public int Age { get; set; }
  }
  </code></pre>
  
  <p>Notice the internal setter for Name, and the internal default constructor. This would normally prevent the XML serialization, but I also marked the XML serialization assembly as "friend" with InternalsVisibleTo :</p>
  
  <pre><code>[assembly: InternalsVisibleTo("TestXML2008.XmlSerializers")]
  </code></pre>
  
  <p>And I added a MSBuild task to pre-generate the serialization assembly :</p>
  
  <pre><code>&lt;Target Name="AfterBuild" DependsOnTargets="AssignTargetPaths;Compile;ResolveKeySource" Inputs="$(MSBuildAllProjects);@(IntermediateAssembly)" Outputs="$(OutputPath)$(_SGenDllName)"&gt;
    &lt;SGen BuildAssemblyName="$(TargetFileName)" BuildAssemblyPath="$(OutputPath)" References="@(ReferencePath)" ShouldGenerateSerializer="true" UseProxyTypes="false" KeyContainer="$(KeyContainerName)" KeyFile="$(KeyOriginatorFile)" DelaySign="$(DelaySign)" ToolPath="$(SGenToolPath)"&gt;
      &lt;Output TaskParameter="SerializationAssembly" ItemName="SerializationAssembly" /&gt;
    &lt;/SGen&gt;
  &lt;/Target&gt;
  </code></pre>
  
  <p>This works fine : the Name property is correctly serialized and deserialized.</p>
  
  <p>Now, I want to sign my assembly... So I define a key file for my assembly, and I modify the InternalsVisibleTo declaration to match the key :</p>
  
  <pre><code>[assembly: InternalsVisibleTo("TestXML2008.XmlSerializers, PublicKey=c5cd51bf2cc4ed49")]
  </code></pre>
  
  <p>But now SGEN fails :</p>
  
  <blockquote>
    <p>Unable to generate a temporary class (result=1).<br/>
    Property or indexer 'TestXML2008.Foo.Name' cannot be assigned to -- it is read only</p>
  </blockquote>
  
  <p>The SGEN task should pick the key file through the macros, but apparently that's not enough... I also tried to specify the key file explicitly in the SGEN task, without success. I have the same result when I use sgen.exe on the command line...</p>
  
  <p>Am I missing something ? I can't understand why it doesn't work when I sign the assembly...</p>