Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (70 sloc) 5.28 KB

platform   license   download   website   mirror

Si13n7 Dev.™ CSharp Library

I was tired of repeating the same steps over and over again when I started developing a new program. So I started writing some classes that I could easily use here and there. This got more and more over time, which made the whole thing rather cluttered and less useful. So I had to start documenting functions cleanly. The chaos became a useful little library, which contains many useful functions. Everything is documented within the classes, so I do not bother to explain this extra. Just browse the classes and take with you, whatever you need, everything here is free.


Requirements:


Example of using separate DLLs for the Any CPU platform target:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Xml;

    internal static class Program
    {
        [STAThread]
        private static void Main()
        {
            AppDomain.CurrentDomain.AssemblyResolve += DynamicAssemblyResolve;
            /*
                some code ...
            */
        }

        private static Assembly DynamicAssemblyResolve(object sender, ResolveEventArgs args)
        {
            var name = args.Name;
            if (!name.StartsWith("SilDev."))
                return null;
            var length = name.IndexOf(',');
            name = name.Substring(0, length);
            return (from dir in GetProbingDirs()
                    select Environment.Is64BitProcess ?
                        Path.Combine(dir, $"{name}64.dll") :
                        Path.Combine(dir, $"{name}32.dll")
                    into path
                    where File.Exists(path)
                    select Assembly.LoadFile(path)).FirstOrDefault();
        }

        private static IEnumerable<string> GetProbingDirs()
        {
            var baseDir = AppDomain.CurrentDomain.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar);
            yield return baseDir;

            var configPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            if (!File.Exists(configPath))
                yield break;

            string entry;
            try
            {
                const string xpath = "/*[name()='configuration']/*[name()='runtime']/*[name()='assemblyBinding']/*[name()='probing']/@privatePath";
                var xmlDoc = new XmlDocument();
                xmlDoc.Load(configPath);
                entry = (xmlDoc.SelectSingleNode(xpath) as XmlAttribute)?.Value;
                if (string.IsNullOrWhiteSpace(entry))
                    throw new ArgumentNullException();
            }
            catch
            {
                yield break;
            }

            if (entry.Contains(";"))
                foreach (var item in entry.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                    yield return Path.Combine(baseDir, item);
            else
                yield return Path.Combine(baseDir, entry);
        }
    }
You can’t perform that action at this time.