开发者

Getting rid of the `My` namespace

I've been developing a Vb.Net app lately, and I'm trying to make it as lightweight as possible (ie make the binaries as small as possible).

I've done all开发者_JAVA技巧 the trivial stuff, but while browsing the binary with ILDasm, I noticed that it has a My namespace, with a lot of methods, although I don't use any of these in my program. It seems that there are default Get/Set methods for every form, and other methods.

Is there a way to get rid of those?

Or, can you show me a use case for the methods bundled by default in the binary?

PS: I guess it's not going to make a huge difference in binary size: I'm just asking this out of curiosity; why would the compiler bundle useless methods in every binaries? Perhaps I'll learn that these methods are actually used somewhere under the hood.

PPS: Here's a minimal example:

Module Test
Sub Main()
End Sub
End Module

The output is:

Getting rid of the `My` namespace


Follow these steps:

  1. Click Show All Files in the solution explorer
  2. Open the My Project branch
  3. Open the Settings.settings branch
  4. Open Settings.designer.vb
  5. Edit the My namespace as you wish

Getting rid of the `My` namespace

There is an additional My sub-namespace called My.Resources which is hiding under the Resources.resx branch.


After furious head-scratching...:

How much is included depends on the value of the _MYTYPE define. It's all documented in MSDN, I failed to find it at first.


In order to remove most of the generated types in the My namespace you will have to set the _MYTYPE define to Empty. To do so you will have to open the .vbproj file in a text editor (using Visual Studio you can do this by first unloading the project and than open it).

Look for the element <MyType> in the first <PropertyGroup> and change its value to Empty:

<PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{C6DC5A64-1E50-48B0-97A5-649D2D323E5E}</ProjectGuid>
    <OutputType>Library</OutputType>
    <RootNamespace>Some.Component</RootNamespace>
    <AssemblyName>Some.Component</AssemblyName>
    <FileAlignment>512</FileAlignment>
    <MyType>Empty</MyType>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <SccProjectName>SAK</SccProjectName>
    <SccLocalPath>SAK</SccLocalPath>
    <SccAuxPath>SAK</SccAuxPath>
    <SccProvider>SAK</SccProvider>
</PropertyGroup>

I have not found a way of removing the InternalXmlHelper that is generated by Visual Studio.

As Clément pointed out it is possible to configure the My namespace using other values of the _MYTYPE define.

This procedure will work for Visual Studio 2012.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜