Click here to Skip to main content
11,482,039 members (68,382 online)
Click here to Skip to main content
Add your own
alternative version

Plug-in Framework

, 14 Sep 2010 CPOL 36.9K 1.2K 103
Basic framework for building desktop plug-in applications.
PluginFramework.zip
PluginFramework
Demo
bin
Debug
Plugins
Release
Plugins
Forms
Properties
Settings.settings
DemoFormPlugin
Properties
Settings.settings
DemoUserControlPlugin
Properties
Settings.settings
MBG.Extensions
Collections
Controls
Core
Data
Math
Properties
Xml
MBG.IO
Properties
PluginFramework.Attributes
Properties
PluginFramework.Controls
Properties
Resources
Icons
SettingsIcon.ico
TreeIcon.ico
Images
SettingsImage.png
TreeImage.png
PluginFramework.Interface
Properties
PluginFramework.Utilities
Properties
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace MBG.Extensions.Core
{
    public static class ObjectExtensions
    {
        /// <summary>
        /// Determines whether this System.Object is contained in the specified IEnumerable
        /// </summary>
        /// <param name="o">The System.Object</param>
        /// <param name="enumerable">The IEnumerable to check</param>
        /// <returns>true if enumerable contains this System.Object, otherwise false.</returns>
        public static bool In(this object o, IEnumerable enumerable)
        {
            foreach (object item in enumerable)
            {
                if (item.Equals(o))
                { return true; }
            }
            return false;
        }

        /// <summary>
        /// Determines whether this T is contained in the specified 'IEnumerable of T'
        /// </summary>
        /// <typeparam name="T">This System.Object's type</typeparam>
        /// <param name="t">This item</param>
        /// <param name="enumerable">The 'IEnumerable of T' to check</param>
        /// <returns>true if enumerable contains this item, otherwise false.</returns>
        public static bool In<T>(this T t, IEnumerable<T> enumerable)
        {
            foreach (T item in enumerable)
            {
                if (item.Equals(t))
                { return true; }
            }
            return false;
        }

        /// <summary>
        /// Determines whether this System.Object is contained in the specified values
        /// </summary>
        /// <param name="o">The System.Object</param>
        /// <param name="items">The values to compare</param>
        /// <returns>true if values contains this System.Object, otherwise false.</returns>
        public static bool In(this object o, params object[] items)
        {
            foreach (object item in items)
            {
                if (item.Equals(o))
                { return true; }
            }
            return false;
        }

        /// <summary>
        /// Determines whether this T is contained in the specified values
        /// </summary>
        /// <typeparam name="T">This System.Object's type</typeparam>
        /// <param name="t">This item</param>
        /// <param name="items">The values to compare</param>
        /// <returns>true if values contains this item, otherwise false.</returns>
        public static bool In<T>(this T t, params T[] items)
        {
            foreach (T item in items)
            {
                if (item.Equals(t))
                { return true; }
            }
            return false;
        }

        /// <summary>
        /// Determines whether this collections contains any of the specified values
        /// </summary>
        /// <typeparam name="T">The type of the values to compare</typeparam>
        /// <param name="t">This collection</param>
        /// <param name="items">The values to compare</param>
        /// <returns>true if the collection contains any of the specified values, otherwise false</returns>
        public static bool ContainsAnyOf<T>(this T t, params T[] items) where T : ICollection<T>
        {
            foreach (T item in items)
            {
                if (t.Contains(item))
                { return true; }
            }
            return false;
        }

        /// <summary>
        /// <para>Serializes the specified System.Object and writes the XML document</para>
        /// <para>to the specified file.</para>
        /// </summary>
        /// <typeparam name="T">This item's type</typeparam>
        /// <param name="item">This item</param>
        /// <param name="fileName">The file to which you want to write.</param>
        /// <returns>true if successful, otherwise false.</returns>
        public static bool XmlSerialize<T>(this T item, string fileName)
        {
            object o = new object();
            FileStream f = null;
            bool isSaved = false;
            try
            {
                XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
                xmlns.Add(string.Empty, string.Empty);

                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.OmitXmlDeclaration = true;

                lock (o)
                {
                    using (XmlWriter writer = XmlWriter.Create(fileName, settings))
                    {
                        xmlSerializer.Serialize(writer, item, xmlns);
                        writer.Close();
                    }
                    isSaved = true;
                }
            }
            catch (Exception x)
            {
                Console.WriteLine("ERROR: " + x.Message);
                return false;
            }
            finally
            {
                if (f != null)
                { f.Close(); }
            }

            return isSaved;
        }

        public static string XmlSerialize<T>(this T item)
        {
            object o = new object();
            try
            {
                XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
                xmlns.Add(string.Empty, string.Empty);

                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.OmitXmlDeclaration = true;

                lock (o)
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    using (StringWriter stringWriter = new StringWriter(stringBuilder))
                    {
                        using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, settings))
                        {
                            xmlSerializer.Serialize(xmlWriter, item, xmlns);
                            return stringBuilder.ToString();
                        }
                    }
                }
            }
            catch (Exception x)
            {
                return string.Empty;
            }
        }   
    }
}

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)

Share

About the Author

gordon_matt
Software Developer VortexSoft
Vietnam Vietnam
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 14 Sep 2010
Article Copyright 2010 by gordon_matt
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid