Click here to Skip to main content
15,896,526 members
Articles / Programming Languages / C#

Grandiose Projects 3. Compatibility with Simulink

Rate me:
Please Sign up or sign in to vote.
4.27/5 (11 votes)
8 Feb 2010CPOL23 min read 47.8K   5.9K   38  
Import of Simulink files
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Reflection;


using CategoryTheory;

namespace DiagramUI
{
    /// <summary>
    /// Wrapper of library arrow
    /// </summary>
    [Serializable()]
    public class LibraryArrowWrapper : CategoryArrowWrapper, ISerializable
    {
        #region Fields
        /// <summary>
        /// The name
        /// </summary>
        private string name;


        /// <summary>
        /// The properties
        /// </summary>
        protected object properties;

        /// <summary>
        /// bytes of assembly
        /// </summary>
        protected byte[] bytes;

        /// <summary>
        /// Assemblies
        /// </summary>
        static private Dictionary<string, Assembly> assemblies = new Dictionary<string, Assembly>();

        #endregion

        #region Ctor

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="filename">File name</param>
        /// <param name="name">Name</param>
        private LibraryArrowWrapper(string filename, string name)
        {
            Load(filename);
            this.name = name;
            Load();
        }

        /// <summary>
        /// Deserialization constructor
        /// </summary>
        /// <param name="info">Serialization info</param>
        /// <param name="context">Streaming context</param>
        protected LibraryArrowWrapper(SerializationInfo info, StreamingContext context)
        {
            bytes = info.GetValue("Bytes", typeof(byte[])) as byte[];
            name = info.GetValue("Name", typeof(string)) as string;
            try
            {
                properties = info.GetValue("Properties", typeof(object));
            }
            catch (Exception)
            {
            }
            Load();
        }

        #endregion

        #region ISerializable Members

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Bytes", bytes, typeof(byte[]));
            info.AddValue("Name", name, typeof(string));
            if (theArrow != null)
            {
                IPropertiesEditor pe = CategoryOperations.GetObject < IPropertiesEditor>(theArrow);
                if (pe != null)
                {
                    object prop = pe.Properties;
                    info.AddValue("Properties", prop, typeof(object));
                }
            }
        }

        #endregion

        #region Specific Membes

        /// <summary>
        /// Creates object from string
        /// </summary>
        /// <param name="str">The string</param>
        /// <returns>The object</returns>
        static public LibraryArrowWrapper Create(string str)
        {
            int n = str.IndexOf("::");
            string fn = str.Substring(0, n);
            string na = str.Substring(n + 2);
            return new LibraryArrowWrapper(fn, na);
        }

        /// <summary>
        /// Gets factory from assembly
        /// </summary>
        /// <param name="ass">The assembly</param>
        /// <returns>The factory</returns>
        static public IArrowFactory GetFactory(Assembly ass)
        {
            Type[] types = ass.GetTypes();

            foreach (Type type in types)
            {
                if (type.GetInterface("IArrowFactory") != null)
                {
                    FieldInfo fi = type.GetField("Object");
                    if (fi != null)
                    {
                        return fi.GetValue("Object") as IArrowFactory;
                    }
                    else
                    {
                        return type.GetConstructor(new Type[] { }).Invoke(new object[] { }) as IArrowFactory;
                    }
                }
            }
            return null;
        }


        void Load()
        {

            Assembly ass = LibraryObjectWrapper.Load(bytes);
            IArrowFactory f = GetFactory(ass);
            theArrow = f[name];
            ICategoryArrow arr = this;
            CategoryOperations.SetAssociatedObject(theArrow, arr.Object);
        }

        void Load(string filename)
        {
            Stream stream = File.OpenRead(filename);
            bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            stream.Close();
        }


        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Russian Federation Russian Federation
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:

1) Noncommutative geometry

http://front.math.ucdavis.edu/author/P.Ivankov

2) Literary work (Russian only)

http://zhurnal.lib.ru/editors/3/3d_m/

3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1

Comments and Discussions