Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Loading assemblies dynamically


  • Please log in to reply
1 reply to this topic

#1 magarcan

magarcan

    Advanced Member

  • Members
  • PipPipPip
  • 43 posts

Posted 10 October 2011 - 11:13 AM

I'm having some space issues so I want to try this. Could be possible load some of my assemblies dynamically in order to leave some ROM free??

I'm working over this code:
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using Microsoft.SPOT;
//using SecretLabs.NETMF.IO;

namespace MonoNetduinoBootloader
{
    public class Program
    {
        public static void Main()
        {
            FileStream assemblyStream;
            Assembly assembly;
            object netduinoAppClassInstance;
            MethodInfo startupMethod;

            // for Netduino and Netduino Mini: mount the SD card
            try
            {
                // uncomment for netduino
                //StorageDevice.MountSD("SD", Microsoft.SPOT.Hardware.SPI.SPI_module.SPI1, (Microsoft.SPOT.Hardware.Cpu.Pin)54);

                // uncomment for netduino mini
                //StorageDevice.MountSD("SD", Microsoft.SPOT.Hardware.SPI.SPI_module.SPI1, (Microsoft.SPOT.Hardware.Cpu.Pin)12);

                // netduino plus will auto-mount SD card
            }
            catch
            {
                Debug.Print("SD card could not be mounted.  Please verify that SD card is inserted.");
                return;
            }

            // make sure the assembly exists
            try
            {
                // open the netduino app assembly, built using Mono.
                assemblyStream = new FileStream(@"\SD\MonoNetduinoApp.pe", FileMode.Open, FileAccess.Read, FileShare.None);
            }
            catch
            {
                Debug.Print("Netduino app not found.  Please copy MonoNetduinoApp.pe to root folder of SD card.");
                return;
            }

            // attempt to load the assembly into RAM
            try
            {
                // read the assembly into RAM
                byte[] assemblyData = new byte[assemblyStream.Length];
                assemblyStream.Read(assemblyData, 0, assemblyData.Length);

                // load the assembly
                assembly = Assembly.Load(assemblyData);
            }
            catch
            {
                Debug.Print("Netduino app corrupted.  Please check MetaDataProcessor settings.");
                return;
            }
            finally
            {
                assemblyStream.Dispose();
            }


            // using reflection: get the type of the MonoNetduinoApp's startup class; get the statup class's type; using the startup class's, get a reference to the type's Main method.
            try
            {
                netduinoAppClassInstance = AppDomain.CurrentDomain.CreateInstanceAndUnwrap("MonoNetduinoApp, Version=1.0.0.0", "MonoNetduinoApp.Program");
                Type netduinoAppClassType = assembly.GetType("MonoNetduinoApp.Program");
                startupMethod = netduinoAppClassType.GetMethod("Main");
            }
            catch
            {
                Debug.Print("Netduino app invalid.  Please verify that class name is MonoNetduinoApp and startup method is Main.");
                return;
            }

            // run the MonoNetduinoApp's startup routine (MonoNetduinoApp.Program's Main method)...starting our app.
            startupMethod.Invoke(netduinoAppClassInstance, new string[] { });
        }
    }
}
As you can see, this is the code of Mono Bootloader. My idea is load some assemblies like:
  • using Microsoft.SPOT.Native
  • using Microsoft.SPOT.Hardware
  • System.IO
And some others...

The problem comes with this:
netduinoAppClassInstance = AppDomain.CurrentDomain.CreateInstanceAndUnwrap("MonoNetduinoApp, Version=1.0.0.0", "MonoNetduinoApp.Program");
Type netduinoAppClassType = assembly.GetType("MonoNetduinoApp.Program");
startupMethod = netduinoAppClassType.GetMethod("Main");

Can I add more than one assembly?? If I want to load an entire class, what have I to write in netduinoAppClassType.GetMethod??

Thanks!!!

#2 AlfredBr

AlfredBr

    Advanced Member

  • Members
  • PipPipPip
  • 138 posts
  • LocationConnecticut, USA

Posted 10 October 2011 - 04:42 PM

Check out this thread. http://forums.netdui...ch__1#entry6144




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.