Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

c# - What use has the default (assembly).dll.config file for .NET-Assemblies?

I have a question regarding AppSettings in C#. First I′ll describe my situation.

My solution consists of an executable program program.exe and an assembly.dll.

The program references the assembly and works with it. The assembly-project has application settings set up with the Visual Studio project settings manager. Now when I compile my solution in my assemblyin elease folder there is an assembly.dll.config file which contains the settings I have set up earlier.

Now the thing I don′t understand : in my program-project where I reference the assembly.dll I have checked CopyLocal=True, but in my programin elease folder there is only the assembly.dll but not the assembly.dll.config file BUT STILL the assembly.dll knows the settings I have set up in the assembly-project application settings.

Now I have read several times that assemblies always access the settings of the executable program but the program has no corresponding settings, so why does the assembly know the correct settings when there is no assembly.dll.config file present?

I assume the settings are compiled into the assembly at compiletime (of course), but then it makes no sense that in my assemblyin elease folder there actually IS an assembly.dll.config file.

I tried copying this file into my programin elease folder where the assembly.dll is copied to on build action, but the assembly.dll just ignores if there is an assembly.dll.config file present in the same folder. It always uses the settings from compiletime. I just don′t understand the use of the assembly.dll.config file. Why is it created when it never has impact on the assembly.dll′s behaviour ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The default values are built into the .dll file. You can of course still change those settings, but you do that in the program.exe config instead by referring to the assembly settings with a section in configSeconds/sectionGroup. The assembly settings can then be changed in the applicationSettings by creating a XML block with the same name as the section.

The section tag in the section group can simply be copied from the app.config file of your assembly project. That way the token, name, etc. will be correct. The same goes for the applicationSettings part. Just copy it from the app.config in the assembly project and into the app.config file of the program.exe project.

example program.exe.config:

<configuration>
  <configSections>
    ... references to all dll settings ...
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="MyAssemblyNamespace.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  ... more config stuff...
  <applicationSettings>
    ... override your dll settings ...
    <MyAssemblyNamespace.Properties.Settings>
       <setting name="MaxUserNameLength" serializeAs="String">
          <value>100</value>
       </setting>
    </MyAssemblyNamespace.Properties.Settings>
  </applicationSettings>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...