eSniff
eSniff

Reputation: 5875

Determining .Net Framework at runtime from a library

I'm somewhat new to .Net, so go easy on me ;-). Anyway.

I working on my first WP7 library project which I hope will be compatible with both XNA and SilverLight applications. Based on whether I'm in XNA or Silverlight one of my factory classes needs to load different config class. Whats the best way to determine this at runtime, from a library.

I know I could do this with the "SILVERLIGHT+WINDOWS_PHONE" preprocessor directives at compile time. But that would mean building two DLLs, which isn't ideal.

~Sniff

Upvotes: 2

Views: 175

Answers (2)

Jim Mischel
Jim Mischel

Reputation: 133975

I suspect that the information you're looking for can be found in the Environment.Version property or in the OperatingSystem.Version property.

Upvotes: 3

myermian
myermian

Reputation: 32505

The best I could think of is setting up your library like so:

[Conditional(#XNA),
 Conditional(#WINDOWS_PHONE)]
public void DoSomeWork()
{
    var x = null;
    x = DoSomeXNAWork();
    x = DoSomeWP7Work();

    if (x != null)
    {
        ...
    }
}

[Conditional(#XNA)]
private ?? DoSomeXNAWork()
{
    return ??;
}

[Conditional(#WINDOWS_PHONE)]
private ?? DoSomeWP7Work()
{
    return ??;
}

Then, just make sure the project referencing this library has the directive set. Kind of like how Microsoft uses the Debug conditional classes such Debug.WriteLine(...). I'm not sure how you could get it to use 2 different config files. I'm sure there is a way because when you create a new Web Project (ASP.NET) there is a config file that is split into Web.Debug.config and Web.Release.config. I couldn't find an answer as to how to do it outside of ASP.NET though.

Upvotes: 0

Related Questions