我试图做一些事情有点不寻常...
I'm trying to do something a bit unusual...
我有这个类Foo:
public class Foo
{
public Foo(string name)
{
this.Name = name;
}
internal Foo()
{
}
public string Name { get; internal set; }
public int Age { get; set; }
}
注意内部制定者名称,内部默认的构造函数。这通常是prevent XML序列化,但我也标志着XML序列化程序集为朋友与InternalsVisibleTo:
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 :
[assembly: InternalsVisibleTo("TestXML2008.XmlSerializers")]
和我添加了一个MSBuild任务,以pre-生成序列化程序集:
And I added a MSBuild task to pre-generate the serialization assembly :
<Target Name="AfterBuild" DependsOnTargets="AssignTargetPaths;Compile;ResolveKeySource" Inputs="$(MSBuildAllProjects);@(IntermediateAssembly)" Outputs="$(OutputPath)$(_SGenDllName)">
<SGen BuildAssemblyName="$(TargetFileName)" BuildAssemblyPath="$(OutputPath)" References="@(ReferencePath)" ShouldGenerateSerializer="true" UseProxyTypes="false" KeyContainer="$(KeyContainerName)" KeyFile="$(KeyOriginatorFile)" DelaySign="$(DelaySign)" ToolPath="$(SGenToolPath)">
<Output TaskParameter="SerializationAssembly" ItemName="SerializationAssembly" />
</SGen>
</Target>
这工作得很好:Name属性是正确序列化和反序列化
This works fine : the Name property is correctly serialized and deserialized.
现在,我想签我的组装。所以我定义我的程序集密钥文件,我修改InternalsVisibleTo声明相匹配的关键是:
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 :
[assembly: InternalsVisibleTo("TestXML2008.XmlSerializers, PublicKey=c5cd51bf2cc4ed49")]
但现在SGEN失败:
But now SGEN fails :
无法生成临时类(结果= 1)。 属性或索引器TestXML2008.Foo.Name'不能被分配到 - 它是只读的。
Unable to generate a temporary class (result=1). Property or indexer 'TestXML2008.Foo.Name' cannot be assigned to -- it is read only
在SGEN任务应通过宏挑密钥文件,但显然这还不够......我也试图在SGEN任务明确指定密钥文件,但没有成功。我有同样的结果,当我使用sgen.exe在命令行...
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...
我缺少的东西?我不明白为什么它不工作时,我签了装配...
Am I missing something ? I can't understand why it doesn't work when I sign the assembly...
在InternalsVisibleTo属性,你需要指定完整的公钥,不只是公钥标记。
In the InternalsVisibleTo attribute, you need to specify the full public key, not just the public key token.
请参阅this回答过类似的问题和这个MSDN文章信息如何让公共密钥。
See this answer to a similar question and this MSDN article for info on how to get the public key.