我在 C# 類別庫中使用 Numpy.NET NuGet 包,它使發布版本的 .exe 檔案太大(從 ~10 到 ~30 MB)。確切的原因是什么,這個問題有什么解決方案嗎?
我使用 ILDASM 來獲取 .exe 檔案的統計資訊,這就是它所顯示的:
File size : 31318016
PE header size : 512 (472 used) ( 0.00%)
PE additional info : 366928 ( 1.17%)
Num.of PE sections : 2
CLR header size : 72 ( 0.00%)
CLR meta-data size : 157972 ( 0.50%)
CLR additional info : 30706176 (-39.09%)
CLR method headers : 8741 ( 0.03%)
Managed code : 77296 ( 0.25%)
Data : 367104 ( 1.17%)
Unaccounted : -366785 (-1.17%)
Num.of PE sections : 2
.text - 30950400
.rsrc - 367104
CLR meta-data size : 157972
Module - 1 (10 bytes)
TypeDef - 275 (3850 bytes) 8 interfaces, 0 explicit layout
TypeRef - 436 (2616 bytes)
MethodDef - 1598 (22372 bytes) 29 abstract, 0 native, 1537 bodies
FieldDef - 1013 (6078 bytes) 20 constant
MemberRef - 1210 (7260 bytes)
ParamDef - 1212 (7272 bytes)
MethodImpl - 65 (390 bytes)
Constant - 189 (1134 bytes)
CustomAttribute- 1041 (6246 bytes)
StandAloneSig - 210 (420 bytes)
InterfaceImpl - 77 (308 bytes)
PropertyMap - 104 (416 bytes)
Property - 400 (2400 bytes)
MethodSemantic- 731 (4386 bytes)
TypeSpec - 255 (510 bytes)
Assembly - 1 (22 bytes)
AssemblyRef - 27 (540 bytes)
ManifestResource- 48 (576 bytes)
NestedClass - 68 (272 bytes)
EventMap - 19 (76 bytes)
Event - 28 (168 bytes)
GenericParam - 12 (96 bytes)
MethodSpec - 173 (692 bytes)
GenericParamConstraint- 1 (4 bytes)
Strings - 50364 bytes
Blobs - 18320 bytes
UserStrings - 20924 bytes
Guids - 16 bytes
Uncategorized - 234 bytes
CLR additional info : 30706176
Resources - 30706176
CLR method headers : 8741
Num.of method bodies - 1537
Num.of fat headers - 413
Num.of tiny headers - 1124
Num.of fat sections - 14
Num.of small sections - 81
Managed code : 77296
Ave method size - 50
uj5u.com熱心網友回復:
原來問題出在.csproj檔案中。它具有以下幾行,將所有 .dll 嵌入“AfterResolveReferences”事件(不知道它是 VS 默認設定還是以前的開發人員故意這樣做)。
<Target Name="AfterResolveReferences">
<ItemGroup>
<EmbeddedResource Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.Extension)' == '.dll'">
<LogicalName>%(ReferenceCopyLocalPaths.DestinationSubDirectory)%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName>
</EmbeddedResource>
</ItemGroup>
</Target>
我的 sln 有多個專案,主專案不需要此解決方案使用的所有庫,因此我添加了條件以排除所有與 python 相關的庫,它就成功了。這并沒有傷害到程式,因為使用 Numpy.net 的代碼庫專案在安裝程式構建和 .exe 檔案現在輕了 20 MB 之后仍然可以正常作業。修改后的代碼下方:
<Target Name="AfterResolveReferences">
<ItemGroup>
<EmbeddedResource Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.Extension)' == '.dll' And '%(ReferenceCopyLocalPaths.Filename)' != 'Numpy' And '%(ReferenceCopyLocalPaths.Filename)' != 'Python.Runtime' And '%(ReferenceCopyLocalPaths.Filename)' != 'Python.Included'
And '%(ReferenceCopyLocalPaths.Filename)' != 'Python.Deployment'">
<LogicalName>%(ReferenceCopyLocalPaths.DestinationSubDirectory)%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName>
</EmbeddedResource>
</ItemGroup>
</Target>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/444811.html
上一篇:讓孩子們向上冒出一個事件
