就像新建一个winForm,Form1.Designer.cs会挂在Form1.cs的下面一样。我想自己建2个文件也达到这个效果。
我用codesmith自动产生的代码会有这个效果,但是把2个文件复制到新的项目里,再引进项目后就变成2个独立的文件了。
实在是想不明白 这个隶属关系是怎么建立的。

解决方案 »

  1.   

    //物理文件名BrowserForm.Designer.cs
    partial class BrowserForm
    {
    }
    //物理文件名BrowserForm.cs
    public partial class BrowserForm : Form
    {
    }
    看上面的关系你就明白了,有partial关键字,表示局部类。IDE会把同名的partial当作一个类。
      

  2.   

    [Quote=引用 2 楼 zhoufoxcn 的回复:]
    C# code//物理文件名BrowserForm.Designer.cs
    partial class BrowserForm
    {
    }
    //物理文件名BrowserForm.cs
    public partial class BrowserForm : Form
    {
    }
    如果自己创建2个这样的文件的话,IDE会把这2个文件当同等级的文件,是没有隶属关系的。和用ide创建出来的效果是不一样的。这个效果是说在解决方案资源管理器里看到的效果,而不是代码执行的效果。
      

  3.   

    打开项目里的“File.csproj”看看
    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProductVersion>8.0.50727</ProductVersion>
        <SchemaVersion>2.0</SchemaVersion>
        <ProjectGuid>{61F421EC-6FA1-4386-9CB9-BD1176EFFA32}</ProjectGuid>
        <OutputType>WinExe</OutputType>
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <RootNamespace>File</RootNamespace>
        <AssemblyName>File</AssemblyName>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <OutputPath>bin\Debug\</OutputPath>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <OutputPath>bin\Release\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
      </PropertyGroup>
      <ItemGroup>
        <Reference Include="System" />
        <Reference Include="System.Data" />
        <Reference Include="System.Deployment" />
        <Reference Include="System.Drawing" />
        <Reference Include="System.Windows.Forms" />
        <Reference Include="System.Xml" />
      </ItemGroup>
      <ItemGroup>
        <Compile Include="Form1.cs">
          <SubType>Form</SubType>
        </Compile>
        <Compile Include="Form1.Designer.cs">
          <DependentUpon>Form1.cs</DependentUpon>
        </Compile>
        <Compile Include="Program.cs" />
        <Compile Include="Properties\AssemblyInfo.cs" />
        <EmbeddedResource Include="Form1.resx">
          <SubType>Designer</SubType>
          <DependentUpon>Form1.cs</DependentUpon>
        </EmbeddedResource>
        <EmbeddedResource Include="Properties\Resources.resx">
          <Generator>ResXFileCodeGenerator</Generator>
          <LastGenOutput>Resources.Designer.cs</LastGenOutput>
          <SubType>Designer</SubType>
        </EmbeddedResource>
        <Compile Include="Properties\Resources.Designer.cs">
          <AutoGen>True</AutoGen>
          <DependentUpon>Resources.resx</DependentUpon>
        </Compile>
        <None Include="Properties\Settings.settings">
          <Generator>SettingsSingleFileGenerator</Generator>
          <LastGenOutput>Settings.Designer.cs</LastGenOutput>
        </None>
        <Compile Include="Properties\Settings.Designer.cs">
          <AutoGen>True</AutoGen>
          <DependentUpon>Settings.settings</DependentUpon>
          <DesignTimeSharedInput>True</DesignTimeSharedInput>
        </Compile>
      </ItemGroup>
      <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
      <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
           Other similar extension points exist, see Microsoft.Common.targets.
      <Target Name="BeforeBuild">
      </Target>
      <Target Name="AfterBuild">
      </Target>
      -->
    </Project>你会发现
    <Compile Include="Form1.Designer.cs">
    <DependentUpon>Form1.cs</DependentUpon> 
    </Compile><EmbeddedResource Include="Form1.resx">
    <SubType>Designer</SubType> 
    <DependentUpon>Form1.cs</DependentUpon> 
    </EmbeddedResource>
    其中<DependentUpon>就是你要附属的文件名。