开发者

Changing a project's files based on solution in Visual Studio 2008

In one C# solution I have several projects that share a CommonAssemblyInfo.cs file, so that certain assembly attributes are shared across all projects. This works fine so long as I only have one CommonAssemblyInfo.cs file. However I have several solutions (applications) which use these projects, and e开发者_Go百科ach solution has its own version of CommonAssemblyInfo.cs.

How can I make the projects use a different CommonAssemblyInfo.cs file depending on which solution they are in?

In the end I want my assemblies to have attributes specific to the solution they were compiled from.

I don't think I can make them files Solution files because they can't all be in the same directory with the same filename. I can't use pre-build events because I don't have a particular project which is always built first. I would prefer not to use a build script because I would like to be able to build and run the solutions through the Visual Studio environment.


No, I don't believe you can't do this - and to me, it sounds like a really bad idea in the first place. If two binaries are built from exactly the same source, with exactly the same configuration, why would it make sense for them to have different attributes?

What are you trying to achieve with this? IMO you should have a really good reason before you make a build more complicated and go against the normal way of doing things.

I'd also suggest that you only specify genuinely common things in the CommonAssemblyInfo.cs file - things like the company name. Then each project can have its own AssemblyInfo.cs with project-specific settings, as normal. I'm personally not a big fan of sharing any source files between projects, but I can see how it makes a certain amount of sense in this case.


if you were building a simple library here that all of your other assemblies are to reference, then I'd say add a Class library project to your solution with the CommonAssemblyInfo.cs file in it and add a reference from each other project.

Since you need each project to have a little different version of it though, that leaves you with two options:

  1. you need to make an inheritance model where the referred project contains the parent, generic class, and then each project implements the generic and wraps it in a new class that you will use.
  2. You just need to simply write the class in each project to make it specific enough.

Without knowing a bit more about your solution space, these would be my recommendations.


You can sort of hack it by using conditional compilation (#if). I've done this on rush jobs, but it's terrible. Just refactor your projects better, as Jon Skeet explains.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜