SGEN、InternalsVisibleTo 和程序集签名
我正在尝试做一些有点不寻常的事情...
我有这个类 Foo :
public class Foo
{
public Foo(string name)
{
this.Name = name;
}
internal Foo()
{
}
public string Name { get; internal set; }
public int Age { get; set; }
}
注意 Name 的内部设置器和内部默认构造函数。 这通常会阻止 XML 序列化,但我还使用 InternalsVisibleTo 将 XML 序列化程序集标记为“朋友”:
[assembly: InternalsVisibleTo("TestXML2008.XmlSerializers")]
并且我添加了一个 MSBuild 任务来预生成序列化程序集:
<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 属性已正确序列化和反序列化。
现在,我想签署我的程序集...所以我为我的程序集定义一个密钥文件,并修改 InternalsVisibleTo 声明以匹配该密钥:
[assembly: InternalsVisibleTo("TestXML2008.XmlSerializers, PublicKey=c5cd51bf2cc4ed49")]
但现在 SGEN 失败:
无法生成临时类(结果=1)。
无法分配属性或索引器“TestXML2008.Foo.Name”——它是只读的
SGEN 任务应该通过宏选择密钥文件,但显然这还不够......我还尝试在中显式指定密钥文件SGEN任务,没有成功。 当我在命令行上使用 sgen.exe 时,我得到了相同的结果......
我错过了什么吗? 我不明白为什么当我签署程序集时它不起作用......
I'm trying to do something a bit unusual...
I have this class Foo :
public class Foo
{
public Foo(string name)
{
this.Name = name;
}
internal Foo()
{
}
public string Name { get; internal set; }
public int Age { get; set; }
}
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")]
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>
This works fine : the Name property is correctly serialized and deserialized.
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")]
But now SGEN fails :
Unable to generate a temporary class (result=1).
Property or indexer 'TestXML2008.Foo.Name' cannot be assigned to -- it is read only
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在InternalsVisibleTo 属性中,您需要指定完整的公钥,而不仅仅是公钥令牌。
请参阅类似问题的答案和这篇 MSDN 文章 了解有关如何获取公钥的信息。
In the InternalsVisibleTo attribute, you need to specify the full public key, not just the public key token.
See this answer to a similar question and this MSDN article for info on how to get the public key.