Click here to Skip to main content
15,892,746 members
Articles / Programming Languages / C#

Universal Framework for Science and Engineering - Part 4: Space elevator

Rate me:
Please Sign up or sign in to vote.
4.56/5 (6 votes)
14 Aug 20066 min read 36.6K   2.2K   37  
An article on framework applications to the space elevator.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Reflection;

using CategoryTheory;

namespace DiagramUI
{
    /// <summary>
    /// Wrapper of object loaded from dll
    /// </summary>
    [Serializable()]
    public class LibraryObjectWrpapper : CategoryObjectWrapper, ISerializable
    {

        #region Fields

        /// <summary>
        /// The name
        /// </summary>
        private string name;

        /// <summary>
        /// The dll path
        /// </summary>
        private string filename;

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


        #endregion

        #region Constructors

        public LibraryObjectWrpapper()
        {
        }

        public LibraryObjectWrpapper(SerializationInfo info, StreamingContext context)
        {
            filename = info.GetValue("FileName", typeof(string)) + "";
            name = info.GetValue("Name", typeof(string)) + "";
            try
            {
                properties = info.GetValue("Properties", typeof(object));
            }
            catch (Exception)
            {
            }
            init();
        }


        #endregion

        #region ISerializable Members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("FileName", filename);
            info.AddValue("Name", name);
            if (theObject != null)
            {
                if (theObject is IPropertiesEditor)
                {
                    IPropertiesEditor pe = theObject as IPropertiesEditor;
                    object prop = pe.Properties;
                    info.AddValue("Properties", prop);
                }
            }
        }

        #endregion

        #region Specific members

        static public string[] GetNames(string filename)
        {
            Assembly ass = null;
            if (BinaryLoader.Object != null)
            {
                byte[] b = BinaryLoader.Object[filename];
                if (b != null)
                {
                    ass = Assembly.Load(b);
                }
            }
            if (ass == null)
            {
                ass = Assembly.LoadFile(filename);
            }
            IObjectFactory factory = GetFactory(ass);
            return factory.Names;
        }


        public void Set(string filename, string name)
        {
            this.filename = filename;
            this.name = name;
            init();
        }

        public string FileName
        {
            get
            {
                return filename;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
        }



        protected void init()
        {
            Assembly ass = null;
            if (BinaryLoader.Object != null)
            {
                byte[] b = BinaryLoader.Object[filename];
                if (b != null)
                {
                    ass = Assembly.Load(b);
                }
            }
            if (ass == null)
            {
                ass = Assembly.LoadFile(filename);
            }
            IObjectFactory factory = GetFactory(ass);
            theObject = factory[name];
            if (theObject is IPropertiesEditor)
            {
                if (properties != null)
                {
                    IPropertiesEditor pe = theObject as IPropertiesEditor;
                    pe.Properties = properties;
                }
            }
        }

        static public IObjectFactory GetFactory(Assembly ass)
        {
            Type[] types = ass.GetTypes();

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

        }

        #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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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