none
How do that .Net project runtime determine load which assembly RRS feed

  • Question

  • I have a .Net 2.0 prject , and need call .Net3.5 assembly. If the machine set up .net3.5, it can work;  otherwise it will throw exception. Now I want to prepair two aseembly, .net2.0 and .net3.5. They are same source and IO, but .net2.0 disable some .net3.5 functions. When I found the machine doesn't support .net3.5, I call the .net2.0 aseembly. How do it? 
    Thursday, May 19, 2011 4:05 AM

Answers

  • You can simply use Assembly.Load(...) to dynamically load the assemblies. Refer to http://msdn.microsoft.com/en-us/library/system.reflection.assembly.load.aspx.

    Riquel, i agree that using try/catch we can catch the exception and take necessary actions. But i thought when we are aware of the possibilities it would be a better programming practice to proactively avoid the exception being raised.

     

    Friday, May 20, 2011 5:04 AM
  • Step1: Check the framework installed. Just grab the value from the registry

     

    RegistryKey installed_versions = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP");

    string[] version_names = installed_versions.GetSubKeyNames();

    //version names start with 'v', eg, 'v3.5' which needs to be trimmed off before conversion

    double Framework = Convert.ToDouble(version_names[version_names.Length - 1].Remove(0, 1), CultureInfo.InvariantCulture);

    int SP = Convert.ToInt32(installed_versions.OpenSubKey(version_names[version_names.Length - 1]).GetValue("SP", 0));

     

    Step2: Dynamically load the assembly as per the framework version using Assembly.Load(...)

    Monday, May 23, 2011 10:39 AM

All replies

  • You need to check if the folder "C:\WINDOWS\Microsoft.NET\Framework\v3.0" exists, if it does then .Net 3.0 is installed. You could use IO.Directory.Info to check the same.
    Thursday, May 19, 2011 8:23 AM
  • Karry,

     

    How do you load .NET assembly? I think that you also can use Try/Catch exception mechanism here. If the application fails to load .NET3.5 assembly, then write the code to try to load .NET2.0 assembly in your application. You don't need to check whether the corresponding version is setup in target computer. 

     

    Rgs,

    Riquel


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, May 20, 2011 1:36 AM
  • Hi Riquel,

        Do you know how dynamic load the assembly?

     

    Friday, May 20, 2011 2:02 AM
  • You can simply use Assembly.Load(...) to dynamically load the assemblies. Refer to http://msdn.microsoft.com/en-us/library/system.reflection.assembly.load.aspx.

    Riquel, i agree that using try/catch we can catch the exception and take necessary actions. But i thought when we are aware of the possibilities it would be a better programming practice to proactively avoid the exception being raised.

     

    Friday, May 20, 2011 5:04 AM
  • Step1: Check the framework installed. Just grab the value from the registry

     

    RegistryKey installed_versions = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP");

    string[] version_names = installed_versions.GetSubKeyNames();

    //version names start with 'v', eg, 'v3.5' which needs to be trimmed off before conversion

    double Framework = Convert.ToDouble(version_names[version_names.Length - 1].Remove(0, 1), CultureInfo.InvariantCulture);

    int SP = Convert.ToInt32(installed_versions.OpenSubKey(version_names[version_names.Length - 1]).GetValue("SP", 0));

     

    Step2: Dynamically load the assembly as per the framework version using Assembly.Load(...)

    Monday, May 23, 2011 10:39 AM
  • Hi Karry,

    I am marking this issue as "Answered". If you have any new questions or concerns about this issue, please feel free to let me know.

    Thank you and have a nice day!


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 26, 2011 3:36 AM
    Moderator