Search code examples
c#.net.net-assembly

How to force the project to use a specific assembly, in C#?


Suppose I have a class library with some conditional compilation, that eventually gets built to MyPortable.dll and MyPortable.X.dll, where the latter is the version compiled with the conditional enabled.

Then I have my "Core" project which references "MyPortable.dll". So far so good.

However, my problem lies in the third project (the "App"), which has a reference to "Core", but needs to use "MyPortable.X.dll" (which is a different build that "Core" uses), but because "Core" is linked against to "MyPortable.dll", my "App" ends using that same version as well, instead of "MyPortable.X.dll".

Is there any way to do that? The code is something like this:

MyPortable

namespace MyPortable
{
    public class Person {
        public string Name { get; set; }
    }

    public class Something {
        public List<Person> GetPersons() {
            List<Person> l = new List<Person>();
            l.Add(new Person { Name = "Name 1" });

#if PLATFORM_X
            l.Add(new Person { Name = "Name 2" });
#endif

            return l;
        }
    }
}

I first compile MyPortable without "PLATFORM_X" enabled, and then I compile again, this time with the flag turned ON. File references are below (note that I am referencing Core.csproj directly):

Core\References\MyPortable.dll
App\
    \References\Core.csproj
    \References\MyPortable.X.dll

Solution

  • If I understand you correctly then yes. You edit your project file to include conditional MSBUILD statements for the references/run times you need in which ever version.

    I had a simillar answer here : Visual Studio loading the right (x86 or x64) dll!

    Thought that in particular used the Build Platform/Target as the variable. I assume that you're using different Targets for setting the compilation conditions?